-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbj_17837.py
More file actions
79 lines (63 loc) · 2.09 KB
/
Copy pathbj_17837.py
File metadata and controls
79 lines (63 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sys
sys.stdin = open('bj_17837_in.txt', 'r')
input = sys.stdin.readline
n, k = map(int, input().strip().split())
# print(n, k)
field = [list(map(int, input().strip().split())) for _ in range(n)]
# for row in field:
# print(*row)
chesses = []
chess_field = [[[] for _ in range(n)] for _ in range(n)]
for idx in range(k):
r, c, direction = map(int, input().strip().split())
chesses.append([r-1, c-1, direction])
chess_field[r-1][c-1].append(idx)
# print(chess)
# for row in chess_field:
# print(row)
dirs = [None, (0, 1), (0, -1), (-1, 0), (1, 0)]
toggle = [None, 2, 1, 4, 3]
turn = 0
while True:
turn += 1
# 종료 조건
if turn > 1000:
print(-1)
sys.exit()
for chess_num in range(k): # idx: 몇번째 말
r, c, direction = chesses[chess_num]
# print(r, c, direction)
#direction: 1,2,3,4 = 우, 좌, 상, 하
dr, dc = dirs[direction]
nr = r + dr
nc = c + dc
if not(0 <= nr < n and 0 <= nc < n) or field[nr][nc] == 2: #장외 or 파란색
#방향 전환
direction = toggle[direction]
chesses[chess_num][2] = direction
#이동위치 재 계산
dr, dc = dirs[direction]
nr = r + dr
nc = c + dc
#체스 이동
if 0 <= nr < n and 0 <= nc < n and field[nr][nc] != 2: #이동 가능하면
# 인덱스 찾기
stack = chess_field[r][c]
pos = stack.index(chess_num)
# 이동할 덩어리
moving = stack[pos:]
if field[nr][nc] == 1: # 빨간색
moving = moving[::-1]
# 덩어리 옮기기
chess_field[nr][nc].extend(moving)
if len(chess_field[nr][nc]) >= 4:
print(turn)
sys.exit()
# 옮겨진 덩어리 체스들 위치 업데이트
for chess in moving:
chesses[chess][0] = nr
chesses[chess][1] = nc
# 기존 덩어리 지우기
del stack[pos:]
# print(chess_field)
# print()