Advertisement
namemkazaza

Q

Nov 28th, 2020
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. fin = open('input.txt', 'r')
  2. fout = open('output.txt', 'w')
  3.  
  4. n, m = map(int, fin.readline().rstrip().split())
  5. A = []
  6. Q = []
  7. x0, y0 = map(int, fin.readline().rstrip().split())
  8. New_color = int(fin.readline().rstrip())
  9. for i in range(n):
  10.     A.append(list(map(int, fin.readline().rstrip().split())))
  11.  
  12. color = A[y0][x0]
  13. Q.append((y0, x0))
  14. count = 0
  15. if A[y0][x0] != New_color:
  16.     while len(Q) != 0:
  17.         y, x = Q.pop(0)
  18.         if A[y][x] == color:
  19.             count += 1
  20.             A[y][x] = New_color
  21.             if x > 0:
  22.                 Q.append((y, x - 1))
  23.             if x < m - 1:
  24.                 Q.append((y, x + 1))
  25.             if y > 0:
  26.                 Q.append((y - 1, x))
  27.             if y < n - 1:
  28.                 Q.append((y + 1, x))
  29. fout.write(str(count) + '\n')
  30.  
  31. for i in range(n):
  32.     fout.write(' '.join(list(map(str, A[i]))) + '\n')
  33.  
  34. fin.close()
  35. fout.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement