Start with an infinite two dimensional grid filled with zeros, indexed from (1,1) at the bottom left corner with coordinates increasing toward the top and right. Given a series of coordinates (r, c), where ris the ending row and cis the ending column, add 1 to each element in the range from (1,1) to (r, C) inclusive. Once all coordinates are processed, determine how many cells contain the maximal value in the grid. Example upRight = ["14", "23", "4 1"] The two space-separated integers within each string represent rand c respectively. The following diagrams show each iteration starting at zero. The maximal value in the grid is 3, and there is 1 occurrence at cell (1, 1). Initial Grid 40 0 0 0 30 0 0 0 20 0 0 0 1 0 0 0 0 1 2 3 4 Step 0:r=1, c = 4 Step 1: r = 2, c = 3 Step 2:r =4,c= 1 4000 0 4 0 0 0 0 4 100 0 30 000 3000 0 3 1000 2 0 0 0 0 2 1 1 1 0 2 2 1 0 1 1 1 1 1 1 2 2 2 1 1 3 2 2 1 1 2 3 4 1 2 3 4 1 2 3 4
row = 5
col = 5
def isPath(arr):
Dir = [[0, 1], [0, -1], [1, 0], [-1, 0]]
q = []
q.append((0, 0))
while (len(q) > 0):
p = q[0]
q.pop(0)
arr[p[0]][p[1]] = -1
if (p == (row - 1, col - 1)):
return True
for i in range(4):
a = p[0] + Dir[i][0]
b = p[1] + Dir[i][1]
if (a >= 0 and b >= 0 and a < row and b < col and arr[a][b] != -1):
q.append((a, b))
return False
arr = [[0, 0, 0, -1, 0],
[-1, 0, 0, -1, -1],
[0, 0, 0, -1, 0],
[-1, 0, -1, 0, -1],
[0, 0, -1, 0, 0]]
if (isPath(arr)):
print("Yes")
else:
print("No")
Comments
Leave a comment