0% found this document useful (0 votes)
16 views4 pages

Desing and Analysis of Algorithms Team 11 AP Report

Uploaded by

22i261
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Desing and Analysis of Algorithms Team 11 AP Report

Uploaded by

22i261
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Desing and Analysis of Algorithms

Assignment Presentation Report

Team No.: 11

Team members:
Naisa Varghese – 22I234
Nandikka R – 22I236
Rhakshitha S – 22I245
Sharveshwar S – 22I256
Soundar Raj – 22I261

Question 1:
In Chefland, there is a monthly robots competition. In the competition, a grid table of Nrows
and M columns will be used to place robots. A cell at row i and column j in the table is called
cell (i, j). To join this competition, each player will bring two robots to compete and each robot
will be placed at a cell in the grid table. Both robots will move at the same time from one cell to
another until they meet at the same cell in the table. Of course they cannot move outside the table.
Each robot has a movable range. If a robot has movable range K, then in a single move, it can
move from cell (x, y) to cell (i, j)provided (|i-x| + |j-y| <= K). However, there are some cells in
the table that the robots cannot stand at, and because of that, they cannot move to these cells. The
two robots with the minimum number of moves to be at the same cell will win the competition.
Chef plans to join the competition and has two robots with the movable range K1 and K2,
respectively. Chef does not know which cells in the table will be used to placed his 2 robots, but
he knows that there are 2 cells (1, 1) and (1, M) that robots always can stand at. Therefore, he
assumes that the first robot is at cell (1, 1) and the other is at cell (1, M). Chef wants you to help
him to find the minimum number of moves that his two robots needed to be at the same cell and
promises to give you a gift if he wins the competition.

Code:
from collections import deque

for _ in range(int(input())):
P, Q, k1, k2 = list(map(int, input().split()))

arena = []

for i in range(P):
arena.append(list(map(int, input().split())))

bot1 = [[-1]*Q for i in range(P)]


bot2 = [[-1]*Q for i in range(P)]
bot1[0][0] = 0
bot2[0][Q-1] = 0

queue = deque([(0, 0)])

while queue:
i, j = queue.popleft()
for x in range(max(i-k1, 0), min(i+k1+1, P)):
t = abs(i-x)
for y in range(max(0, j-k1+t), min(j+k1-t+1, Q)):
if arena[x][y] == 0 and bot1[x][y] == -1:
bot1[x][y] = bot1[i][j] + 1
queue.append((x, y))
queue = deque([(0, Q-1)])

while queue:
i, j = queue.popleft()
for x in range(max(i-k2, 0), min(i+k2+1, P)):
t = abs(i-x)
for y in range(max(0, j-k2+t), min(j+k2-t+1, Q)):
if arena[x][y] == 0 and bot2[x][y] == -1:
bot2[x][y] = bot2[i][j] + 1
queue.append((x, y))

min_moves = float("inf")

for i in range(P):
for j in range(Q):
if bot1[i][j] != -1 and bot2[i][j] != -1:
min_moves = min(min_moves, max(bot1[i][j], bot2[i][j]))
print(-1 if min_moves == float("inf") else min_moves)

Sample Input:
2
4411
0110
0110
0110
0000
4411
0110
0110
0110
1001

Sample Output:
5
-1

Key Points:
 BFS expands the search for each robot, marking moves.
 Finally, it calculates the minimum number of moves for the two robots to meet.

Time and Space Complexity:


 O(P * Q) . For both robots, the BFS traverses all reachable cells.
 O(P * Q) . Two matrices of size P * Q are used for the movements of the robots.
Question 2:
Deadpool and Wolverine go head to head in a crazy game on Crazy Country. Let’s first get some
details about Crazy country. The Crazy Country has N cities. It is possible to go from one city to
any other city in Crazy Country And there is a unique path between every pair of cities. City 1 is
the capital city and it located at level 0. Every city in this country is assigned a level which is
equal to its distance from the capital city.

Distance between cities A and B = number of cities encountered while going from
city A to city B (excluding both) + 1.

Every city in this crazy country has a certain number of aircrafts in it. Deadpool has designed
these really cool aircrafts. Every aircraft contains some Cool Buttons. Each button has a Coolness
associated with it. Initially, each aircraft has exactly 1 button of Coolness C, for all integers C ≥ 1.
When a functional button with Coolness X on an aircraft lying in city of level L is pressed, it
takes the aircraft to a city of level (L-X) which lies on the path between this city and the capital
city (obviously , there is only one such city). Of course, the buttons with Coolness > L are
dysfunctional in an aircraft which is in city of level L as the aircrafts are not allowed to go beyond
the capital city. Plus , there is something really weird about these aircrafts, whenever you press a
button with a particular Coolness X then all the buttons of this aircraft become dysfunctional that
are of Coolness (I*X) , where I >= 1. Now, Deadpool and Wolverine take turns and press a
functional Cool button on any one of the aircrafts of their choice. The Player who can’t make a
move loses. Being the smart guys that they are, they obviously play optimally. Given a crazy
country setup, you have to determine who will win this game if Deadpool makes the first move.

Code:
def calculate_grundy(n):
grundy = [0] * (n + 1)
for l in range(1, n + 1):
reachable_grundy = set()
used_buttons = [False] * (l + 1)

for x in range(1, l + 1):


if not used_buttons[x]:

reachable_grundy.add(grundy[l - x])

multiple = x
while multiple <= l:
used_buttons[multiple] = True
multiple += x

mex = 0
while mex in reachable_grundy:
mex += 1
grundy[l] = mex

return grundy

def nim_sum(grundy, levels):


xor_sum = 0
for level in levels:
xor_sum ^= grundy[level]
return xor_sum

def crazy_country_winner(n, levels):


grundy = calculate_grundy(n)
if nim_sum(grundy, levels) == 0:
return "Wolverine wins"
else:
return "Deadpool wins"

n = int(input())
if(n<0):
print("invalid input")
exit()

levels = list(map(int,input().split()))
for i in levels:
if(i<0 or i>n):
print("invalid input")
exit()
print(crazy_country_winner(n, levels))

Sample Input:
5
123

Sample output:
Wolverine wins

Key Points:
 For each city, calculate the Grundy numbers based on reachable levels.
 Determine the winner based on the nim-sum: Deadpool wins if it’s non-zero, otherwise
Wolverine wins.

Time and Space Complexity:


 O(n^2) . Calculating Grundy numbers for all levels takes O(n^2) due to the nested loop for
each level.
 O(n) . The space is used to store the Grundy numbers and levels.

You might also like