Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Battlegrounds - Interview Question

Battlegrounds is a game which has a map denoted by an N x M matrix. The map can have open areas (where players can move) denoted by 0’s,1’s where there are people or closed areas (where players can’t move) denoted by X’s. Each player should be able to move around the map and should be able to reach every other player's location. Consider you are the game’s developer. Given the map you need to find if they all can reach each other. Each player can move up, down, left and right on the map.

'''i
4 4
X00X
10X0
XX00
0X1X
'''

n,m=4,4
a='''X00X
10X0
XX00
0X1X
'''
a='''X01X
10X0
XX00
0X0X
'''
a=a.splitlines()
b=[]
b2=[]
pl=[]
for k,i in enumerate(a):
x=list(i)
b.append(x)
if '1' in x :
pl.append((k,x.index('1')))
b2.append(['0']*4)
p=[]
c=1
def s3(i,j):return 0
def s2(i,j):
if s3(i,j) and b2[i][j]=='0':
b2[i][j]=str(c);s(i,j)

def s(i,j):
s2(i+1,j)
s2(i-1,j)
s2(i,j+1)
s2(i,j-1)

def print2(a):
for i in a:
print(''.join(i))

for k1,i in enumerate(b):
for k2,j in enumerate(i):
if j!='X' and b2[k1][k2]=='0':
s(k1,k2)
b2[k1][k2]=str(c)
c+=1

v=set()
for (i,j) in pl:
v.add(b2[i][j])

print('Y' if len(v)==1 else 'N')


This post first appeared on SMARTMANOJ, please read the originial post: here

Share the post

Battlegrounds - Interview Question

×

Subscribe to Smartmanoj

Get updates delivered right to your inbox!

Thank you for your subscription

×