2021.06.15 정리
맵 크기 N * M
북쪽에서 떨어진 칸의 개수 A 서쪽에서 떨어진 칸의 개수 B
# 맵 크기 입력
n, m = map(int, input().split())
# 캐릭터 현재 위치 입력
a, b, d = map(int, input().split())
# 육지인지 바다인지 입력
array = []
for i in range(4):
array.append(list(map(int, input().split())))
move = [(-1,0), (0, 1), (1, 0), (0, -1)]
dx = [0, 1, 0, -1]
dy = [-1, 0, 1, 0]
a = 1
b = 1
d = 3
moved = []
while True:
if d == 0:
if (a,b) not in moved and array[a][b] == 0:
(a, b) = (a + move[0][0], b + move[0][1])
d += 0
moved.append((a, b))
else:
d += 1
elif d == 1:
if (a,b) not in moved and array[a][b] == 0:
(a, b) = (a + move[1][0], b + move[1][1])
d += 0
moved.append((a, b))
else:
d += 1
elif d == 2:
if (a,b) not in moved and array[a][b] == 0:
(a, b) = (a + move[2][0], b + move[2][1])
d += 0
moved.append((a, b))
else:
d += 1
elif d == 3:
if (a,b) not in moved and array[a][b] == 0:
(a, b) = (a + move[3][0], b + move[3][1])
d += 0
moved.append((a, b))
else:
d = 0
if (a,b) in moved and array[a][b] == 0
if a < 0 or a > n or b < 0 or b > m:
break
print((a, b))
print(d)
답안 예시
# 답안 예시
# N, M을 공백으로 구분하여 입력받기
n, m = map(int, input().split())
# 방문한 위치를 저장하기 위한 맵을 생성하여 0으로 초기화
d = [[0] * m for _ in range(n)]
# 현재 캐릭터의 X좌표, Y좌표, 방향을 입력받기
x, y, direction = map(int, input().split())
d[x][y] = 1 # 현재 좌표 방문 처리
# 전체 맵 정보를 입력받기
array = []
for i in range(n):
array.append(list(map(int, input().split())))
# 북, 동, 남, 서 방향 정의
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
# 왼쪽으로 회전
def turn_left():
global direction
direction -= 1
if direction == -1:
direction = 3
# 시뮬레이션 시작
count = 1
turn_time = 0
while True:
# 왼쪽으로 회전
turn_left()
nx = x + dx[direction]
ny = y + dy[direction]
# 회전한 이후 정면에 가보지 않은 칸이 존재하는 경우 이동
if d[nx][ny] == 0 and array[nx][ny] == 0:
d[nx][ny] = 1
x = nx
y = ny
count += 1
turn_time = 0
continue
# 회전한 이후 정면에 가보지 않은 칸이 없거나 바다인 경우
else:
turn_time += 1
# 네 방향 모두 갈 수 없는 경우
if turn_time == 4:
nx = x - dx[direction]
ny = y - dy[direction]
# 뒤로 갈 수 있다면 이동하기
if array[nx][ny] == 0:
x = nx
y = ny
# 뒤가 바다로 막혀있는 경우
else:
break
turn_time = 0
print(count)
제한시간 50분 문제를 2시간 넘게 보고도 못풀었다,,
그래도 접근법? 코드짜는법?을 공부하는게 재밌다!