fork download
  1. import sys
  2. input = sys.stdin.readline
  3. N, K = map(int, input().split())
  4. move = [(0, 0), (0, 1), (0, -1), (-1, 0), (1, 0)]
  5. chess = [list(map(list, zip(map(int, input().split()), [[] for _ in range(N)])))
  6. for _ in range(N)]
  7. where = []
  8. for i in range(K):
  9. x, y, dist = map(int, input().split())
  10. where.append([x-1, y-1, dist])
  11. chess[x-1][y-1][1].append(i)
  12. # for line in chess:
  13. # print(*line)
  14. turn = 0
  15. while turn < 1000:
  16. turn += 1
  17. # print(turn)
  18. for i in range(K):
  19. x, y, dist = where[i][0], where[i][1], where[i][2]
  20. tmpx = x+move[dist][0]
  21. tmpy = y+move[dist][1]
  22. if tmpx >= N or tmpx < 0 or tmpy >= N or tmpy < 0 or chess[tmpx][tmpy][0] == 2:
  23. if where[i][2] % 2 == 1:
  24. where[i][2] += 1
  25. else:
  26. where[i][2] -= 1
  27. tmpx = x-move[dist][0]
  28. tmpy = y-move[dist][1]
  29. if tmpx >= N or tmpx < 0 or tmpy >= N or tmpy < 0 or chess[tmpx][tmpy][0] == 2:
  30. continue
  31. idx = chess[x][y][1].index(i)
  32. horse = chess[x][y][1][idx:]
  33. for num in horse:
  34. where[num][0] = tmpx
  35. where[num][1] = tmpy
  36. chess[x][y][1] = chess[x][y][1][:idx]
  37. if chess[tmpx][tmpy][0] == 0:
  38. chess[tmpx][tmpy][1].extend(horse)
  39. else:
  40. chess[tmpx][tmpy][1].extend(horse[::-1])
  41. if len(chess[tmpx][tmpy][1]) >= 4:
  42. print(turn)
  43. for line in chess:
  44. print(*line)
  45. print()
  46. exit()
  47. for line in chess:
  48. print(*line)
  49. print()
  50. print(-1)
  51.  
Success #stdin #stdout 0.02s 9308KB
stdin
6 10
0 1 2 0 1 1
1 2 0 1 1 0
2 1 0 1 1 0
1 0 1 1 0 2
2 0 1 2 0 1
0 2 1 0 2 1
1 1 1
2 2 2
3 3 4
4 4 1
5 5 3
6 6 2
1 6 3
6 1 2
2 4 3
4 2 1
stdout
[0, []] [1, [0]] [2, []] [0, [8]] [1, []] [1, []]
[1, [1]] [2, []] [0, []] [1, []] [1, []] [0, [6]]
[2, []] [1, []] [0, []] [1, []] [1, []] [0, []]
[1, []] [0, []] [1, [2, 9]] [1, []] [0, [3, 4]] [2, []]
[2, []] [0, []] [1, []] [2, []] [0, []] [1, []]
[0, [7]] [2, []] [1, []] [0, []] [2, []] [1, [5]]

[0, [0]] [1, []] [2, []] [0, []] [1, []] [1, []]
[1, [1]] [2, []] [0, []] [1, [8]] [1, []] [0, []]
[2, []] [1, []] [0, []] [1, [3, 4]] [1, []] [0, [6]]
[1, []] [0, []] [1, []] [1, []] [0, []] [2, []]
[2, []] [0, [9, 2]] [1, []] [2, []] [0, []] [1, []]
[0, [7]] [2, []] [1, []] [0, []] [2, []] [1, [5]]

[0, []] [1, [0]] [2, []] [0, []] [1, []] [1, []]
[1, [1]] [2, []] [0, [4]] [1, []] [1, []] [0, [6]]
[2, []] [1, []] [0, [3]] [1, [8]] [1, []] [0, []]
[1, []] [0, [2]] [1, []] [1, []] [0, []] [2, []]
[2, []] [0, []] [1, [9]] [2, []] [0, []] [1, []]
[0, [7]] [2, []] [1, []] [0, []] [2, []] [1, [5]]

[0, [0]] [1, []] [2, []] [0, []] [1, []] [1, [6]]
[1, [1]] [2, []] [0, []] [1, []] [1, []] [0, []]
[2, []] [1, [2, 3]] [0, [4]] [1, []] [1, []] [0, []]
[1, []] [0, []] [1, []] [1, [8]] [0, []] [2, []]
[2, []] [0, [9]] [1, []] [2, []] [0, []] [1, []]
[0, [7]] [2, []] [1, []] [0, []] [2, []] [1, [5]]

[0, []] [1, [0]] [2, []] [0, []] [1, []] [1, []]
[1, [1]] [2, []] [0, []] [1, []] [1, []] [0, [6]]
[2, []] [1, []] [0, []] [1, [8]] [1, []] [0, []]
[1, [3]] [0, [2]] [1, [4]] [1, []] [0, []] [2, []]
[2, []] [0, []] [1, [9]] [2, []] [0, []] [1, []]
[0, [7]] [2, []] [1, []] [0, []] [2, []] [1, [5]]

[0, [0]] [1, []] [2, []] [0, []] [1, []] [1, []]
[1, [1]] [2, []] [0, []] [1, [8]] [1, []] [0, []]
[2, []] [1, []] [0, []] [1, []] [1, []] [0, [6]]
[1, []] [0, [3]] [1, []] [1, []] [0, []] [2, []]
[2, []] [0, [2, 9, 4]] [1, []] [2, []] [0, []] [1, []]
[0, [7]] [2, []] [1, []] [0, []] [2, []] [1, [5]]

7
[0, []] [1, [0]] [2, []] [0, []] [1, []] [1, []]
[1, [1]] [2, []] [0, []] [1, [8]] [1, []] [0, []]
[2, []] [1, []] [0, []] [1, []] [1, []] [0, [6]]
[1, []] [0, [3, 2, 9, 4]] [1, []] [1, []] [0, []] [2, []]
[2, []] [0, []] [1, []] [2, []] [0, []] [1, []]
[0, [7]] [2, []] [1, []] [0, []] [2, []] [1, [5]]