아직 한참 많이 부족하다는 것을 느낀다.
구현을 위해 생각하는 훈련을 계속 해야겠다.
입력을 받는 것은 그냥 그렇다치고 map을 하나 더 만들어서
간 곳을 체크하는 것이 문제의 핵심이었음.
왼쪽으로 돌면서 turn left와 direction을 체크해야하고 문제의 조건을 잘 따라가면 풀리는 문제. 그냥 deep.copy해서 맵을 하나 더 만들어도 괜찮을 듯?
import sys
sys.stdin = open('test.txt', 'r')
N, M = map(int, input().split())
x, y, direction = map(int, input().split())
card = []
d = [[0] * M for _ in range(N)]
d[x][y] = 1
for _ in range(N):
card.append(list(map(int, input().split())))
directions = [(-1, 0), (0, 1), (1, 0), (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 + directions[direction][0]
ny = y + directions[direction][1]
# 안 가봤고 육지!
if d[nx][ny] == 0 and card[nx][ny] == 0:
d[nx][ny] = 1
x = nx
y = ny
count += 1
turn_time = 0
continue
# 가봤거나 바다!
else:
turn_time += 1
# 4번 돌았는데 갈 수가 없어요 ㅠㅠ 뒤로 갈 수 있는지 체크!
if turn_time == 4:
nx = x - directions[direction][0]
ny = y - directions[direction][1]
if card[nx][ny] == 0:
x = nx
y = ny
turn_time = 0
else:
break
print(count)
++오늘 복습하면서 code를 다시 짜봤음 ㅋㅋ
import sys
import copy
sys.stdin = open('test.txt', 'r')
n, m = map(int, input().split())
x, y, direction = map(int, input().split())
card = []
for _ in range(n):
card.append(list(map(int, input().split())))
copied_map = copy.deepcopy(card)
copied_map[x][y] = 'X'
# 북 동 남 서
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
turn_left = 0
cnt = 1
while True:
# 왼쪽으로 90도 회전
direction -= 1
if direction < 0:
direction = 3
temp_x = x + dx[direction]
temp_y = y + dy[direction]
# 갈 수 있고 안 가봤구나!
if copied_map[temp_x][temp_y] != 'X' and card[temp_x][temp_y] == 0:
x = temp_x
y = temp_y
turn_left = 0
copied_map[x][y] = 'X'
cnt += 1
# 가봤거나 바다구나!
else:
turn_left += 1
if turn_left == 4:
x = x - dx[direction]
y = y - dx[direction]
if card[x][y] == 0:
turn_left = 0
else:
print(cnt)
break