fork(1) download
  1. class MazePath:
  2.  
  3. ans = 0
  4. def __init__(self,A,b):
  5. self.A = A
  6. self.b = b
  7.  
  8. #flag = 0
  9. def isPath(self,i,j,flag):
  10. m,n = 4,4
  11.  
  12. if i<=m-1 and j<=n-1 and b[i][j]==0:
  13. b[i][j] = 1
  14. if A[i][j]==9:
  15. flag = 1
  16. else:
  17. if ( (j+1)<=n-1 and b[i][j+1]==0 and A[i][j+1]==1 ): #right
  18. flag = 1
  19. self.isPath(i,j+1,flag)
  20. if ( (i+1)<=m-1 and b[i+1][j]==0 and A[i+1][j]==1 ): #down
  21. flag = 1
  22. self.isPath(i+1,j,flag)
  23. if ( (j-1)>=0 and b[i][j-1]==0 and A[i][j-1]==1 ): #left
  24. flag = 1
  25. self.isPath(i,j-1,flag)
  26. if ( (i-1)>=0 and b[i-1][j]==0 and A[i-1][j]==1 ): #up
  27. flag = 1
  28. self.isPath(i-1,j,flag)
  29.  
  30. if flag==1:
  31. return 1
  32. else:
  33. return 0
  34.  
  35.  
  36. A = [
  37. [1,1,0,0],
  38. [0,1,0,1],
  39. [0,1,0,1],
  40. [1,1,9,1]
  41. ]
  42. b = [
  43. [0,0,0,0],
  44. [0,0,0,0],
  45. [0,0,0,0],
  46. [0,0,0,0]
  47. ]
  48.  
  49. o1 = MazePath(A,b)
  50. print o1.isPath(0,0,0)
  51. print b
Success #stdin #stdout 0.01s 8968KB
stdin
Standard input is empty
stdout
1
[[1, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0], [1, 1, 0, 0]]