2022.10.08
아.. 이게 안 되네..?
내일은 시뮬레이션 열심히 좀 몰아서 풀어봐야겠다.
# 구현 완성 못 한 상태!
# 방향 이동에서 8가지 경우의 수가 나오는데 일일이 구해줘야하나..
# 효율적으로 하고싶은데.. 싶어서 고민된다.
# 실전 테스트에서는 막무가내로 뭐라도 해야하니까 일단 8개 다 했을 것 같긴한데
# 연습이니까 분명 더 나은 방법이 있을 것 같아서 더 고민하고 싶다 ㅠㅠㅜㅠㅜ
import sys
input = sys.stdin.readline
n = int(input())
board = [[0] * n for _ in range(n)]
snake=[]
k = int(input())
for i in range(k):
x, y = map(int, input().split())
board[x][y] = 1 # 사과 : 1
l = int(input())
turn = deque([])
for i in range(l):
x, c = map(int, input().split())
turn.append((x, c))
def turn_direction(direction):
# 8가지 경우의 수
if direction == 90:
dx = 1
dy = 0
elif direction == -90:
dx = 0
dy = 1
playing = True
snake.append((0, 0))
time = 0
while playing:
if time == turn[0][0]:
x, c = turn.popleft()
if c == 'L':
turn_direction(90)
elif c == 'D':
turn_direction(-90)
nx = x + dx
ny = y + dy
if (nx, ny) not in snake and 0 <= nx < n and 0 <= ny < n :
snake.append((nx, ny))
if board[nx][ny] == 1: #사과 있다면
board[nx][ny] = 0
else:
snake.pop()
else:
playing: False
아따.. 로직은 맞는 것 같은데 세밀한 부분의 구현이 안 되네...;; 사실 이러면 틀린거지 뭐;; ㅠㅜㅠㅜㅜㅠ
풀이 .. 볼까..? 일단 더 부딪혀볼까..? ㅜㅠ
2022.10.09
import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
board = [[0] * n for _ in range(n)]
snake=[]
k = int(input())
for i in range(k):
x, y = map(int, input().split())
board[x][y] = 1 # 사과 : 1
l = int(input())
turn = deque([])
for i in range(l):
x, c = input().split()
turn.append((int(x), c))
change = [(-1, 0), (0, 1), (1, 0), (0, -1)]
playing = True
snake.append((0, 0))
time = 0
turn_index = 1
x, y = 0, 0
def turn_direction(direction):
global turn_index
# 8가지 경우의 수
if direction == 'L':
if turn_index != 0:
turn_index -= 1
else:
turn_index = 3
elif direction == 'D':
if turn_index != 3:
turn_index += 1
else:
turn_index = 0
return
while playing:
if turn and time == turn[0][0]:
x, c = turn.popleft()
if c == 'L':
turn_direction('L')
elif c == 'D':
turn_direction('D')
nx = x + change[turn_index][0]
ny = y + change[turn_index][1]
if (nx, ny) not in snake and 0 <= nx < n and 0 <= ny < n :
snake.append((nx, ny))
if board[nx][ny] == 1: #사과 있다면
board[nx][ny] = 0
else:
snake.pop()
x, y = nx, ny
else:
playing = False
print(time)
time += 1
방향 전환하는 부분을 구현해줬다!
while문 탈출도 잘 하고!
그런데 답이 .. 틀리다.
예상 값보다 훨씬 적은 시간 안에 끝나버린다. 왜일까 ㅠㅜ
from collections import deque
def change_direction(d, c):
# 상(0) 우(1) 하(2) 좌(3)
# 동쪽 회전: 상(0) -> 우(1) -> 하(2) -> 좌(3) -> 상(0) : +1 방향
# 왼쪽 회전: 상(0) -> 좌(3) -> 하(2) -> 우(1) -> 상(0) : -1 방향
if c == "L":
d = (d - 1) % 4
else:
d = (d + 1) % 4
return d
# 상 우 하 좌
dy = [-1, 0, 1, 0]
dx = [0, 1, 0, -1]
def game():
direction = 1 # 초기 방향
time = 1 # 시간
y, x = 0, 0 # 초기 뱀 위치
visited = deque([[y, x]]) # 방문 위치
board[y][x] = 2
while True:
y, x = y + dy[direction], x + dx[direction]
if 0 <= y < N and 0 <= x < N and board[y][x] != 2:
if not board[y][x] == 1: # 사과가 없는 경우
temp_y, temp_x = visited.popleft()
board[temp_y][temp_x] = 0 # 꼬리 제거
board[y][x] = 2
visited.append([y, x])
if time in times.keys():
direction = change_direction(direction, times[time])
time += 1
else: # 본인 몸에 부딪히거나, 벽에 부딪힌 경우
return time
# input
N = int(input())
K = int(input())
board = [[0] * N for _ in range(N)]
for _ in range(K):
a, b = map(int, input().split())
board[a - 1][b - 1] = 1 # 사과 저장
L = int(input())
times = {}
for i in range(L):
X, C = input().split()
times[int(X)] = C
print(game())
: 끝내 풀이 검색해서 찾아보고 공부해서 제출 .. 😥
: 시뮬레이션 문제들 반복해서 풀어봐야겠다.
뒤늦게 찾았는데 이 분 풀이가 나한테는 더 직관적이다.
https://ssong915.tistory.com/4
[백준] 3190번 뱀 - python (삼성 sw 기출)
https://www.acmicpc.net/problem/3190 3190번: 뱀 'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신
ssong915.tistory.com
'CodingTest > Baekjun Online Judge' 카테고리의 다른 글
[ BOJ / 파이썬 ] 14503 로봇 청소기 (0) | 2022.10.10 |
---|---|
[ BOJ / 파이썬 ] 14500 테트로미노 (0) | 2022.10.10 |
[ BOJ / 파이썬 ] 2667 단지 번호 붙이기 (0) | 2022.10.08 |
[ BOJ / 파이썬 ] 2583 영역 구하기 (0) | 2022.10.08 |
[ BOJ /파이썬 ] 11660 구간 합 (0) | 2022.10.06 |