본문 바로가기
Problem Solving/BOJ

[Python] 3190. 뱀

by 부르르 2021. 4. 3.

https://www.acmicpc.net/problem/3190

 

3190번: 뱀

 'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임

www.acmicpc.net


완탐실시하였다.

방향이 전환될 때의 좌표와 방향을 Queue에 저장하는 것이 포인트.

 

Task Score

100%

def solve(MAP): ​​​​global N ​​​​curr_head_y = curr_head_x = 0 ​​​​curr_tail_y = curr_tail_x = 0 ​​​​curr_head_di = 0 ​​​​curr_tail_di = 0 ​​​​turn_point = [] ​​​​time = 0 ​​​​while True: ​​​​​​​​if time in dic.keys(): ​​​​​​​​​​​​if dic[time] == "D": ​​​​​​​​​​​​​​​​curr_head_di = (curr_head_di+1)%4 ​​​​​​​​​​​​else: ​​​​​​​​​​​​​​​​curr_head_di = (curr_head_di-1)%4 ​​​​​​​​​​​​turn_point.append((curr_head_y, curr_head_x, curr_head_di)) ​​​​​​​​next_head_y = curr_head_y + di[curr_head_di][0] ​​​​​​​​next_head_x = curr_head_x + di[curr_head_di][1] ​​​​​​​​if 0<=next_head_y<N and 0<=next_head_x<N: ​​​​​​​​​​​​if MAP[next_head_y][next_head_x] == 0: ​​​​​​​​​​​​​​​​if turn_point: ​​​​​​​​​​​​​​​​​​​​if (curr_tail_y, curr_tail_x) == turn_point[0][:2]: ​​​​​​​​​​​​​​​​​​​​​​​​hd = turn_point.pop(0) ​​​​​​​​​​​​​​​​​​​​​​​​curr_tail_di = hd[2] ​​​​​​​​​​​​​​​​MAP[next_head_y][next_head_x] = 1 ​​​​​​​​​​​​​​​​MAP[curr_tail_y][curr_tail_x] = 0 ​​​​​​​​​​​​​​​​curr_tail_y += di[curr_tail_di][0] ​​​​​​​​​​​​​​​​curr_tail_x += di[curr_tail_di][1] ​​​​​​​​​​​​elif MAP[next_head_y][next_head_x] == 1: ​​​​​​​​​​​​​​​​return time + 1 ​​​​​​​​​​​​else: ​​​​​​​​​​​​​​​​MAP[next_head_y][next_head_x] = 1 ​​​​​​​​​​​​curr_head_y = next_head_y ​​​​​​​​​​​​curr_head_x = next_head_x ​​​​​​​​​​​​time += 1 ​​​​​​​​else: ​​​​​​​​​​​​return time + 1 # MAP 생성 N = int(input()) MAP = [[0]*N for _ in range(N)] # 초기 뱀의 좌표 = 꼬리의 좌표 MAP[0][0] = 1 # MAP 에 사과 표시 for _ in range(int(input())): ​​​​y, x = map(int, input().split()) ​​​​MAP[y-1][x-1] = 2 # 방향 di = ((0,1),(1,0),(0,-1),(-1,0)) # 시간 및 방향 입력 dic = dict() for _ in range(int(input())): ​​​​t, d = input().split() ​​​​dic[int(t)] = d print(solve(MAP))

 

문제를 푸는데 도움이 된 테스트 케이스

 

 

728x90
반응형

'Problem Solving > BOJ' 카테고리의 다른 글

[Python] 14499. 주사위 굴리기  (0) 2021.04.07
[Python] 13458. 시험 감독  (0) 2021.04.04
[Python]3678. 카탄의 개척자  (0) 2019.05.11
[Python]13460. 구슬 탈출 2  (0) 2019.05.10
[Python]14502. 연구소  (0) 2019.05.09

댓글