백준 python 기록
뱀 파이썬 3190
작지
2021. 12. 16. 12:35
백준 링크 : https://www.acmicpc.net/problem/3190
3190번: 뱀
'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임
www.acmicpc.net
어릴때 많이했던 뱀 게임인데
하 진짜 어지럽다..
*!!!! 오류코드 틀림
from collections import deque
import sys
sys.setrecursionlimit(10**9)
n = int(input())
boaed = [[0]*(n+1) for i in range(n+1)]
apple = int(input())
for i in range(apple):
a,b = map(int,input().split())
boaed[a][b] = 1
snake = 1
boaed[1][1] = 2
m=int(input())
turn = deque([])
for i in range(m):
s = list(input().split())
turn.append(s)
cnt = 0
last = deque([[1,1]])
def snake_board(x,y,snake):
global cnt
if turn:
if cnt == int(turn[0][0]):
if turn[0][1] == "D":
snake = (snake+1)%4
turn.popleft()
else:
snake = (snake-1)%4
turn.popleft()
if snake == 1 and 0 < x+1 <= n and 0 < y <= n:
if boaed[y][x+1] != 2:
if boaed[y][x+1] == 1:
boaed[y][x+1] = 2
last.append([x + 1, y])
cnt += 1
snake_board(x+1,y,snake)
elif boaed[y][x+1] == 0:
boaed[y][x+1] = 2
last.append([x+1,y])
i,j = last.popleft()
boaed[j][i] = 0
cnt += 1
snake_board(x + 1, y, snake)
elif snake == 2 and 0 < x <= n and 0 < y+1 <= n:
if boaed[y+1][x] != 2:
if boaed[y+1][x] == 1:
boaed[y+1][x] = 2
last.append([x, y+1])
cnt += 1
snake_board(x, y+1, snake)
elif boaed[y+1][x] == 0:
boaed[y+1][x] = 2
last.append([x, y+1])
i, j = last.popleft()
boaed[j][i] = 0
cnt += 1
snake_board(x, y+1, snake)
elif snake == 3 and 0 < x-1 <= n and 0 < y <= n:
if boaed[y][x - 1] != 2:
if boaed[y][x - 1] == 1:
boaed[y][x - 1] = 2
last.append([x-1, y])
cnt += 1
snake_board(x + 1, y, snake)
elif boaed[y][x - 1] == 0:
boaed[y][x - 1] = 2
last.append([x-1, y])
i, j = last.popleft()
boaed[j][i] = 0
cnt += 1
snake_board(x - 1, y, snake)
elif snake == 0 and 0 < x <= n and 0 < y-1 <= n:
if boaed[y - 1][x] != 2:
if boaed[y - 1][x] == 1:
boaed[y - 1][x] = 2
last.append([x, y-1])
cnt += 1
snake_board(x, y - 1, snake)
elif boaed[y - 1][x] == 0:
boaed[y - 1][x] = 2
last.append([x, y-1])
i, j = last.popleft()
boaed[j][i] = 0
cnt += 1
snake_board(x, y - 1, snake)
snake_board(1,1,1)
print(cnt+1)
초반에는 이렇게 아무생각없이 다 꼬고 꼬와서 넣었더니 그냥 틀렸다고했다.
생각대로움직이긴했는데 어디선가 이 스파게티 코드에서 에러를 일으킨듯 하다.
그래서 일단은 이 코드를 풀어썼다.
from collections import deque
import sys
direction = [(-1, 0), (0, 1), (1, 0), (0, -1)]
sys.setrecursionlimit(10**9)
n = int(input())
board = [[0]*(n+1) for i in range(n+1)]
apple = int(input())
for i in range(apple):
a,b = map(int,input().split())
board[a][b] = 1
snake = 1
board[1][1] = 2
m=int(input())
turn = deque([])
for i in range(m):
s = list(input().split())
turn.append(s)
cnt = 0
last = deque([[1,1]])
def snake_board(x,y,snake):
global cnt
while True:
x, y = x + direction[snake][0], y + direction[snake][1]
if 0 < x <= n and 0 < y <= n and board[x][y] != 2:
if not board[x][y] == 1:
tx, ty = last.popleft()
board[tx][ty] = 0
board[x][y] = 2
last.append([x,y])
cnt += 1
if turn:
if cnt == int(turn[0][0]):
if turn[0][1] == "D":
snake = (snake + 1) % 4
elif turn[0][1] == "L":
snake = (snake - 1) % 4
turn.popleft()
else:
return cnt
print(snake_board(1,1,1)+1)
x와y가 범위에 맞고 내가 가는자리가 뱀의 몸통이 아니라면 이동
그런데 이게 위랑 솔직히 똑같다고 생각하는데 위에서 어디서 꼬인지 모르겠다
나는 버러지~
아니 왠걸 이건 통과가 된다고하네??
솔직히 왜 통과되는지 모르겠다