Spies, Revised
Problem Statement :
Two spies in a grid will have their covers blown if: 1 . They are both in the same row. 2. They are both in the same column. 3. They can see each other diagonally (i.e., lie in a line inclined 45° or 135° to the base of the grid). The level of danger is now increased! In addition to the conditions above, no 3 spies may lie in any straight line. This line need not be aligned 45° or 135° to the base of grid. Write a program in the language of your choice to place N spies (one spy per row) on an N x N grid without blowing anyone's cover. Your program must then print the following 2 lines describing a valid configuration: 1. The value of N. 2. A space-separated list of 1-indexed column numbers, where each value i is the column number of the spy in row i (where 1 <= i <= N ). Solve this problem for N as large as possible, up to (and including) 999. Note: Run and Custom Input are not available for this challenge; you must click Submit Code for your submission to be scored. Your score for this challenge will always be the maximum value scored by any of your submissions. Examples In the examples below, S denotes a spy and * denotes an empty cell. Input Format There is no input for this challenge. Constraints N is odd. N < 1000 (Do not submit for any value of N larger than 999 ) Output Format Print the following 2 lines of output: 1. The first line should be a single integer denoting the value of N. 2 .The second line should contain a space-separated list of integers . Each integer i (where 1 < i <= N) should be the 1-indexed column number where the spy in row i is located.
Solution :
Solution in C :
In Python3 : ( Problem has Ambiguity )
import random
n=11
list_of_numbers=[]
final_list=[]
rows=[]
colomns=[]
def check(a):
for k in final_list:
chk1=[]
chk2=[]
x=k[0]
y=k[1]
for i in range(n):
for j in range(n):
if i==j:
if [x+i,y+j] in final_list:
return 0
if [x-i,y-j] in final_list:
return 0
elif [x+i,y+j] in final_list:
chk1.append([x+i,y+j])
if len(chk1)>2:
return 0
elif [x-i,y-j] in final_list:
chk2.append([x-i,y-j])
if len(chk2)>2:
return 0
for i in range(n):
list_of_numbers.append(i)
def number_generator():
while(1):
r = random.choice(list_of_numbers)
c = random.choice(list_of_numbers)
if r not in rows and c not in colomns:
rows.append(r)
colomns.append(c)
final_list.append([c,r])
if len(final_list)==n:
break
number_generator()
while(1):
if not check(1):
final_list=[]
rows=[]
colomns=[]
number_generator()
else:
print(final_list)
new_list=[]
for i in final_list:
new_list.append(i[0]*n + (i[1]+1))
new_list.sort()
for i in new_list:
print(i, end=" ")
break
View More Similar Problems
Print in Reverse
Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing
View Solution →Reverse a linked list
Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio
View Solution →Compare two linked lists
You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis
View Solution →Merge two sorted linked lists
This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C
View Solution →Get Node Value
This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t
View Solution →Delete duplicate-value nodes from a sorted linked list
This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -
View Solution →