이번 문제는 시뮬레이션 문제로, 캐릭터의 이동 시 해당 방향으로 2칸을 확인하여 조건에 맞도록 캐릭터와 박스를 움직이는 문제이다. 이 문제에서 주의할 점은 다음과 같았다.
def move(command):
global cur
d = mapping[command]
y, x = cur
ny, nx = y+dy[d], x+dx[d]
if 0 <= ny < r and 0 <= nx < c and grid[ny][nx] != '#':
if grid[ny][nx] == 'b' or grid[ny][nx] == 'B':
nxt_y, nxt_x = ny+dy[d], nx+dx[d]
if 0 <= nxt_y < r and 0 <= nxt_x < c and grid[nxt_y][nxt_x] not in ('#', 'b', 'B'):
cur = [ny, nx]
grid[ny][nx], grid[nxt_y][nxt_x] = '.', 'b'
else:
cur = [ny, nx]
def grid_setting():
grid[cur[0]][cur[1]] = 'w'
for y, x in point:
if grid[y][x].isalpha():
grid[y][x] = grid[y][x].upper()
else:
grid[y][x] = '+'
def chk_complete():
for i in range(r):
for j in range(c):
if grid[i][j] == 'b':
return False
return True
idx = 1
while True:
r, c = map(int, input().split())
if r == 0 and c == 0:
break
grid = [list(str(input())) for _ in range(r)]
commands = list(str(input()))
dy, dx = [-1, 1, 0, 0], [0, 0, -1, 1]
mapping = {'U': 0, 'D': 1, 'L': 2, 'R': 3}
cur = []
point = []
for i in range(r):
for j in range(c):
if grid[i][j] in ('+', 'W', 'B'):
point.append((i, j))
if grid[i][j] == 'w':
cur = [i, j]
grid[i][j] = '.'
if grid[i][j] == 'W':
cur = [i, j]
grid[i][j] = '+'
flag = False
for command in commands:
if chk_complete():
grid_setting()
print("Game " + str(idx) + ": " + "complete")
for i in range(r):
print(''.join(grid[i]))
flag = True
break
grid[cur[0]][cur[1]] = '.'
move(command)
grid_setting()
if not flag:
if chk_complete():
print("Game "+str(idx)+": "+"complete")
else:
print("Game "+str(idx)+": "+"incomplete")
for i in range(r):
print(''.join(grid[i]))
idx += 1