개발자 톡
dfs 풀이 오류좀 찾아봐 주실 수 있을까요
- 등록일
- 2024-02-21 14:50:13
- 조회수
- 310
- 작성자
- ehdxor0922
n,T = map(int,input().split())
signals = [list(map(int,input().split())) for _ in range(n**2)]
# print(signals)
L = [0,-1]
R = [0,1]
U = [-1,0]
D = [1,0]
## 1번 리스트 [R U R D] 에서 제일 앞의 R은 해당 신호에 통과하기 위한 올바른 방향입니다. R U L D 가 순서대로 순환하며 들어 가 있습니다
signal_dict = [[],[R,U,R,D],[U,L,U,R],[L,U,L,D],[D,L,D,R],
[R,U,R],[U,L,U],[L,L,D],[D,D,R],
[R,R,D],[U,U,R],[L,L,U],[D,L,D]]
# dir = signal_dict[1]
# print(dir[0])
# print(dir[1])
answer = [[0,0]]
import sys
n,T = map(int,sys.stdin.readline().split())
signals = [list(map(int,sys.stdin.readline().split())) for _ in range(n**2)]
# print(signals)
L = [0,-1]
R = [0,1]
U = [-1,0]
D = [1,0]
signal_dict = [[],[R,U,R,D],[U,L,U,R],[L,U,L,D],[D,L,D,R],
[R,U,R],[U,L,U],[L,L,D],[D,D,R],
[R,R,D],[U,U,R],[L,L,U],[D,L,D]]
# dir = signal_dict[1]
# print(dir[0])
# print(dir[1])
answer = [[0,0]]
def dfs(t,prev_move,x,y) :
if t == T :
return
signal = signals[3*x+y]## signal = [2,6,12,9]
# print("signal:",signal)
# print("Current Pos:",x,y)
##dir = [U,L,U,R]
dir = signal_dict[signal[t%4]]
# print(f"Time is {t}, so t%4 = {t%4}, dir = {dir[1:]} ")
prev_dir = dir[0]
# print(f"previos_move:{prev_move}, prev_dir :{prev_dir}")
if prev_dir == prev_move :
# print("Direction O")
for move in dir[1:] :
nx = x + move[0]
ny = y + move[1]
if 0<=nx<n and 0<=ny<n :
# print("from:",x,y)
# print("move:",move)
if [nx,ny] not in answer :
answer.append([nx,ny])
# print(f"append {nx},{ny}")
# print()
dfs(t+1,move,nx,ny)
# else :
# print("Direction X")
# print()
dfs(0,U,0,0)
print(len(answer))