Advertisement
Guest User

Untitled

a guest
May 12th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import os, sys
  3.  
  4. # Reading all input as array:
  5. arr = [[int (x) for x in s.split()] for s in sys.stdin.read().split('\n')]
  6.  
  7. # Copy size of array:
  8. n = arr[0][0]
  9. arr = arr[1:]
  10.  
  11.  
  12. # Struct Record:
  13. class Record:
  14. def __init__(self, dist = 10000, count = 2, cell = None):
  15. self.dist, self.count, self.cell = dist, count, cell
  16.  
  17. def inc(self):
  18. return Record(self.dist + 1, self.count, self.cell)
  19.  
  20. # Combine two records:
  21. def combine(a, b):
  22. if (a.dist < b.dist):
  23. return (a)
  24. elif (a.dist > b.dist):
  25. return (b)
  26. elif a.cell == b.cell:
  27. return (a)
  28. else:
  29. return Record(a.dist, a.count + b.count)
  30.  
  31. # 2-D arrays:
  32. best_l_u = [[Record() for _ in range(n)] for _ in range(n)]
  33. best_l_d = [[Record() for _ in range(n)] for _ in range(n)]
  34. best_r_u = [[Record() for _ in range(n)] for _ in range(n)]
  35. best_r_d = [[Record() for _ in range(n)] for _ in range(n)]
  36.  
  37. for i in range(n):
  38. for j in range(n):
  39. if (arr[i][j] != 0):
  40. best_l_u[i][j] = Record(0, 1, (i, j))
  41. best_l_d[i][j] = Record(0, 1, (i, j))
  42. best_r_u[i][j] = Record(0, 1, (i, j))
  43. best_r_d[i][j] = Record(0, 1, (i, j))
  44.  
  45. # Walk from left-up corner:
  46. for i in range(1, n):
  47. best_l_u[i][0] = Record(0, 1, (i, 0)) if (arr[i][0] != 0) else (best_l_u[i-1][0]).inc()
  48.  
  49. for j in range(1, n):
  50. best_l_u[0][j] = Record(0, 1, (0, j)) if (arr[0][j] != 0) else (best_l_u[0][j-1]).inc()
  51.  
  52. for i in range(1, n):
  53. for j in range(1, n):
  54. temp = combine(best_l_u[i-1][j], best_l_u[i][j-1]).inc()
  55. if (temp.dist < best_l_u[i][j].dist):
  56. best_l_u[i][j] = temp
  57.  
  58. # Walk from left-down corner:
  59. for i in range(n-2, -1, -1):
  60. best_l_d[i][0] = Record(0, 1, (i, 0)) if (arr[i][0] != 0) else (best_l_d[i+1][0]).inc()
  61.  
  62. for j in range(1, n):
  63. best_l_d[n-1][j] = Record(0, 1, (n-1, j)) if (arr[n-1][j] != 0) else (best_l_d[n-1][j-1]).inc()
  64.  
  65. for i in range(n-2, -1, -1):
  66. for j in range(1, n):
  67. temp = combine(best_l_d[i+1][j], best_l_d[i][j-1]).inc()
  68. if (temp.dist < best_l_d[i][j].dist):
  69. best_l_d[i][j] = temp
  70.  
  71. # Walk from right-down corner:
  72. for i in range(n-2, -1, -1):
  73. best_r_d[i][n-1] = Record(0, 1, (i, n-1)) if (arr[i][n-1] != 0) else (best_r_d[i+1][n-1]).inc()
  74.  
  75. for j in range(n-2, -1, -1):
  76. best_r_d[n-1][j] = Record(0, 1, (n-1, j)) if (arr[n-1][j] != 0) else (best_r_d[n-1][j+1]).inc()
  77.  
  78. for i in range(n-2, -1, -1):
  79. for j in range(n-2, -1, -1):
  80. temp = combine(best_r_d[i+1][j], best_r_d[i][j+1]).inc()
  81. if (temp.dist < best_r_d[i][j].dist):
  82. best_r_d[i][j] = temp
  83.  
  84. # Walk from right-up corner:
  85. for i in range(1, n):
  86. best_r_u[i][n-1] = Record(0, 1, (i, n-1)) if (arr[i][n-1] != 0) else (best_r_u[i-1][n-1]).inc()
  87.  
  88. for j in range(n-2, -1, -1):
  89. best_r_u[0][j] = Record(0, 1, (0, j)) if (arr[0][j] != 0) else (best_r_u[0][j+1]).inc()
  90.  
  91. for i in range(1, n):
  92. for j in range(n-2, -1, -1):
  93. temp = combine(best_r_u[i-1][j], best_r_u[i][j+1]).inc();
  94. if (temp.dist < best_r_u[i][j].dist):
  95. best_r_u[i][j] = temp
  96.  
  97. # Combine answers for each corners:
  98. for i in range(0, n):
  99. for j in range(0, n):
  100. if (arr[i][j] == 0):
  101. best = combine(combine(best_l_u[i][j], best_r_u[i][j]), combine(best_l_d[i][j], best_r_d[i][j]))
  102. if (best.count == 1):
  103. arr[i][j] = arr[best.cell[0]][best.cell[1]]
  104.  
  105. # Creating string for fast output:
  106. s_out = ""
  107. for row in arr:
  108. for x in row:
  109. s_out += str(x) + " ";
  110. s_out += "\n"
  111.  
  112. # Fast output:
  113. #os.write(1, s_out.encode())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement