dmkozyrev

prog.py

May 11th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 KB | None | 0 0
  1. from copy import *
  2. # Reading size of 2-dimension array:
  3. n = int(input())
  4.  
  5. # Reading input array[n][n]:
  6. arr = []
  7. for i in range(n):
  8.     arr.append([int(x) for x in input().split()])
  9.  
  10. for i in range(n):
  11.     for j in range(n):
  12.         print(arr[i][j], end = ' ')
  13.     print()
  14.    
  15. # Struct Record:
  16. class Record:
  17.    
  18.     def __init__(self, dist = 10**9, count = 2, row = -1, col = -1):
  19.         self.dist, self.count, self.row, self.col = dist, count, row, col
  20.        
  21.     def inc(self):
  22.         self.dist = self.dist + 1
  23.         return self
  24.  
  25. # Combine two records:
  26. def combine(a, b):
  27.     if (a.dist < b.dist):
  28.         return deepcopy(a)
  29.     elif (a.dist > b.dist):
  30.         return deepcopy(b)
  31.     elif a.row == b.row and a.col == b.col:
  32.         return deepcopy(a)
  33.     else:
  34.         return Record(a.dist, a.count + b.count, -1, -1)    
  35.    
  36. # 2-D DP:
  37. best_l_u = [[Record()] * n] * n
  38. best_l_d = [[Record()] * n] * n
  39. best_r_u = [[Record()] * n] * n
  40. best_r_d = [[Record()] * n] * n
  41.  
  42. print('-' * 80)
  43. for i in range(0, n):
  44.     for j in range(0, n):
  45.         if (arr[i][j] != 0): best_l_u[i][j] = deepcopy(Record(0, 1, i, j))
  46.         elif i != 0 and j != 0: best_l_u[i][j] = combine(best_l_u[i-1][j], best_l_u[i][j-1]).inc()
  47.         elif i == 0 and j != 0: best_l_u[i][j] = deepcopy(best_l_u[i][j-1]).inc()
  48.         elif i != 0 and j == 0: best_l_u[i][j] = deepcopy(best_l_u[i-1][j]).inc()
  49.         print('i = ', i, 'j = ', j, 'best_l_u = ', best_l_u[i][j].dist, best_l_u[i][j].count, best_l_u[i][j].row, best_l_u[i][j].col)
  50.        
  51. for i in range(n-1, -1, -1):
  52.     for j in range(0, n):
  53.         if (arr[i][j] != 0): best_l_d[i][j] = Record(0, 1, i, j)
  54.         elif i != n-1 and j != 0: best_l_d[i][j] = combine(best_l_d[i+1][j], best_l_d[i][j-1]).inc()
  55.         elif i == n-1 and j != 0: best_l_d[i][j] = deepcopy(best_l_d[i][j-1]).inc()
  56.         elif i != n-1 and j == 0: best_l_d[i][j] = deepcopy(best_l_d[i+1][j]).inc()
  57.        
  58. for i in range(n-1, -1, -1):
  59.     for j in range(n-1, -1, -1):
  60.         if (arr[i][j] != 0): best_r_d[i][j] = Record(0, 1, i, j)
  61.         elif i != n-1 and j != n-1: best_r_d[i][j] = combine(best_r_d[i+1][j], best_r_d[i][j+1]).inc()
  62.         elif i == n-1 and j != n-1: best_r_d[i][j] = deepcopy(best_r_d[i][j+1]).inc()
  63.         elif i != n-1 and j == n-1: best_r_d[i][j] = deepcopy(best_r_d[i+1][j]).inc()
  64.  
  65. for i in range(0, n):
  66.     for j in range(n-1, -1, -1):
  67.         if (arr[i][j] != 0): best_r_u[i][j] = Record(0, 1, i, j)
  68.         elif i != 0 and j != n-1: best_r_u[i][j] = combine(best_r_u[i-1][j], best_r_u[i][j+1]).inc()
  69.         elif i == 0 and j != n-1: best_r_u[i][j] = deepcopy(best_r_u[i][j+1]).inc()
  70.         elif i != 0 and j == n-1: best_r_u[i][j] = deepcopy(best_r_u[i-1][j]).inc()
  71.  
  72. print('-' * 80)
  73. for i in range(0, n):
  74.     for j in range(0, n):
  75.         print('i = ', i, 'j = ', j, 'best_l_u = ', best_l_u[i][j].dist, best_l_u[i][j].count, best_l_u[i][j].row, best_l_u[i][j].col)        
  76.        
  77. for i in range(0, n):
  78.     for j in range(0, n):
  79.         if (arr[i][j] == 0):
  80.             best = combine(combine(best_l_u[i][j], best_r_u[i][j]), combine(best_l_d[i][j], best_r_d[i][j]))
  81.             #print('i = ', i, 'j = ', j, 'best = ', best.dist, best.count, best.row, best.col)
  82.             #print(best.dist, best.count, best.row, best.col)
  83.             if (best.count == 1):
  84.                 arr[i][j] = arr[best.row][best.col]
  85.         print(arr[i][j], end=' ')
  86.     print()
Advertisement
Add Comment
Please, Sign In to add comment