fork download
  1. import sys
  2. import copy
  3.  
  4.  
  5. def check_board(board, N):
  6. ret = 0
  7. for i in range(N):
  8. res = 1
  9. for j in range(N - 1):
  10. if board[i][j] == board[i][j + 1]:
  11. res += 1
  12. if res > ret:
  13. ret = res
  14.  
  15. for i in range(N):
  16. res = 1
  17. for j in range(N - 1):
  18. if board[j][i] == board[j + 1][i]:
  19. res += 1
  20. if res > ret:
  21. ret = res
  22.  
  23. return ret
  24.  
  25.  
  26. N = int(sys.stdin.readline())
  27. board = []
  28. candy_cnt = 0
  29. for i in range(N):
  30. board.append(sys.stdin.readline().replace('\n', ''))
  31.  
  32. for i in range(N):
  33. for j in range(N - 1):
  34. tmp = copy.deepcopy(board)
  35. if tmp[i][j] != tmp[i][j + 1]:
  36. tmp[i] = tmp[i][:j] + tmp[i][j + 1] + tmp[i][j] + tmp[i][j + 2:]
  37. res = check_board(tmp, N)
  38. if res > candy_cnt:
  39. candy_cnt = res
  40.  
  41. for i in range(N):
  42. for j in range(N - 1):
  43. tmp = copy.deepcopy(board)
  44. if tmp[j][i] != tmp[j + 1][i]:
  45. tmp_row = tmp[j][:i] + tmp[j + 1][i] + tmp[j][i + 1:]
  46. tmp[j + 1] = tmp[j + 1][:i] + tmp[j][i] + tmp[j + 1][i + 1:]
  47. tmp[j] = tmp_row
  48. res = check_board(tmp, N)
  49. if res > candy_cnt:
  50. candy_cnt = res
  51.  
  52. print(candy_cnt)
  53.  
Success #stdin #stdout 0.02s 9388KB
stdin
5
YCPZY
CYZZP
CCPPP
YCYZC
CPPZZ
stdout
4