-문제: https://www.acmicpc.net/problem/3190
3190번: 뱀
'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임
www.acmicpc.net
문제 풀면서 부족했던 부분:
방향을 돌리는 turn()함수를 생각해 보지 못한 것
회전정보를 담고 있는 Info 리스트 생각해보지 못한 것
꼬리 위치는 q리스트로 파악 가능하다
백준에서 콛 돌리면서 while 다음 첫번째 If문에서 indexerror가 발생한다. 추후에 고칠 예정
N=int(input())
K=int(input())
board=[[0]*(N+1) for _ in range(N+1)]
for _ in range(K):
a,b=map(int,input().split())
board[a][b]=1
L=int(input())
info=[]
for _ in range(L):
x,c=input().split()
info.append((int(x),c))
dx=[0,1,0,-1]
dy=[1,0,-1,0]
def turn(direction,c):
if c=='L':
direction=(direction-1)%4
else:
direction=(direction+1)%4
return direction
def simulate():
x,y=1,1
board[x][y]=2
time=0
direction=0
index=0
q=[(x,y)]
while True:
nx,ny=x+dx[direction],y+dy[direction]
if board[nx][ny]!=2 and 1<=nx and nx<=N and 1<=ny and ny<=N:
if board[nx][ny]==1:
board[nx][ny]=2
q.append((nx,ny))
if board[nx][ny]==0:
board[nx][ny]=2
q.append((nx,ny))
qx,qy=q.pop(0)
board[qx][qy]=0
else:
time+=1
break
time+=1
x,y=nx,ny
if index<1 and time==info[index][0]:
direction=turn(direction,info[index][1])
index+=1
return time
print(simulate())
'코딩테스트 > 기출' 카테고리의 다른 글
[프로그래머스/이진탐색] 가사 검색 (0) | 2022.05.25 |
---|---|
[프로그래머스/dfs] 600048번: 괄호 변환 (0) | 2022.05.06 |
[카카오] 프로그래머스: 60062번 (0) | 2022.05.03 |
[기출/삼성전자] 백준 15686번: 치킨 배달 (0) | 2022.04.29 |
[프로그래머스] 60059번 (0) | 2022.03.31 |