반응형
https://www.acmicpc.net/problem/14503
x_lim, y_lim = map(int, input().split())
# north 0, east 1, south2, west3
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
x, y, d = map(int, input().split())
table = []
for i in range(x_lim):
table.append(list(map(int,input().split())))
count = 0
while True:
# cleaning => 2
if table[x][y]==0:
table[x][y]= 2
count+=1
stuck = True
for i in range(4):
# left direction
lx = x + dx[d-1]
ly = y + dy[d-1]
if table[lx][ly] == 0: # left is not cleaned yet
x = lx
y = ly
d = (d+3) % 4
stuck = False
break
else: # wall or already cleaned
d = (d+3) % 4
continue
if stuck:
bx = x + dx[d - 2]
by = y + dy[d - 2]
if table[bx][by] == 1: # back is also wall
break
else:
x = bx
y = by
print(count)
전형적인 구현문제로 문제의 순서에 따라 코드를 잘 작성하면 된다.
네이밍 방법에 익숙해져야겠다. 왼쪽 좌표 lx, ly 그리고 back 좌표 bx, by
댓글