12장 구현 문제 뱀 문제 (pg 327)
문제 해설: n = int(input()) k = int(input()) info = [] graph = [[0] * (n + 1) for _ in range(n + 1)] for _ in range(k): a, b = map(int, input().split()) graph[a][b] = 1
l = int(input()) for _ in range(l): x, c = input().split() info.append((int(x),c))
dx = [0,1,0,-1] dy = [1,0,-1,0]
def turn_direc(direction, c): if c == 'L': direction = (direction - 1) % 4 else: direction = (direction + 1) % 4
return direction
def simulation():
direction = 0
x, y = 1, 1
graph[x][y] = 2
q = [(x,y)]
time = 0
index = 0
while True:
nx = x + dx[direction]
ny = y + dy[direction]
if 1<= nx and nx <= n and 1 <= ny and ny <= n and graph[nx][ny] != 2:
if graph[nx][ny] == 1:
graph[nx][ny] = 2
q.append((nx, ny))
if graph[nx][ny] != 1:
graph[nx][ny] = 2
q.append((nx, ny))
qx, qy = q.pop(0)
graph[qx][qy] = 0
else:
time += 1
break
x, y = nx, ny
time += 1
if index < l and time == info[index][0]:
direction = turn_direc(direction, info[index][1])
index += 1
return time
print(simulation())
에서 if 1<= nx and nx <= n and 1 <= ny and ny <= n and graph[nx][ny] != 2: if graph[nx][ny] == 1: graph[nx][ny] = 2 q.append((nx, ny))
if graph[nx][ny] == 0:
중
if graph[nx][ny] == 0 대신 if graph[nx][ny] != 1로 하면 왜 오답처리가 되는 것인지 이해가 안 됩니다.
이미 상위 if에서 graph[nx][ny] != 2가 되었고 또 다시 if graph[nx][ny] != 1이면 if graph[nx][ny] == 0: 와 같은 뜻 아닌지요?