Skip to content

Commit fd954ab

Browse files
authored
승연 책 숙제(5/19/수) (#78)
* 뱀 * 뱀 fix * 치킨배달 * 3190
1 parent 724e088 commit fd954ab

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

syheo/codingTest1/12-11-뱀.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#CH12 구현 기출
2+
#예제 12-11
3+
#뱀
4+
#백준 3190
5+
#골드 5
6+
7+
# 먼저 뱀은 몸길이를 늘려 머리를 다음칸에 위치시킨다.
8+
# 만약 이동한 칸에 사과가 있다면, 그 칸에 있던 사과가 없어지고 꼬리는 움직이지 않는다.
9+
# 만약 이동한 칸에 사과가 없다면, 몸길이를 줄여서 꼬리가 위치한 칸을 비워준다. 즉, 몸길이는 변하지 않는다.
10+
11+
from collections import deque
12+
13+
#N:board size, K:apple count
14+
N = int(input())
15+
K = int(input())
16+
17+
#0: 빈칸, 1:사과, 2:뱀 몸통
18+
maps = [[0]*(N+1) for _ in range(N+1)]
19+
#사과 입력
20+
for i in range(K):
21+
a,b = map(int,input().split())
22+
maps[a][b]=1
23+
24+
#방향전환 입력
25+
opCnt = int(input())
26+
ops = deque([])
27+
for i in range(opCnt):
28+
a,b = map(str,input().split())
29+
ops.append((int(a),b))
30+
31+
time = 0
32+
moves = [(0,1),(1,0),(0,-1),(-1,0)] #우하좌상
33+
direction = 0
34+
row , col = 1, 1
35+
snake = deque([(row,col)])
36+
maps[row][col]=2
37+
38+
while True:
39+
#시간 1초 증가
40+
time += 1
41+
#이동
42+
row = row+moves[direction][0]
43+
col = col+moves[direction][1]
44+
if (row<=0 or row>N) or (col<=0 or col>N) or maps[row][col]==2:
45+
gameover = True
46+
break
47+
else:
48+
snake.append((row,col))
49+
if maps[row][col]==0:
50+
a,b = snake.popleft()
51+
maps[a][b]=0
52+
maps[row][col]=2
53+
#명령 시간이 되었다면
54+
if ops and ops[0][0]==time:
55+
X,C = ops.popleft()
56+
if C=='L':
57+
direction=direction-1 if direction>0 else 3
58+
elif C=='D':
59+
direction=direction+1 if direction<3 else 0
60+
61+
62+
print(time)
63+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#CH12 구현 기출
2+
#예제 12-13
3+
#치킨배달
4+
#백준 15686
5+
#골드 5
6+
7+
from collections import deque
8+
9+
def distance(a,b,c,d):
10+
return abs(a-c)+abs(b-d)
11+
12+
def bfs(a,chickenCnt,M):
13+
rstList = []
14+
q = deque([(a,1,[a])])
15+
while q:
16+
cur,cnt,chickenList = q.popleft()
17+
if cnt == M:
18+
rst = 0
19+
for house in houses:
20+
minDis = 101
21+
for item in chickenList:
22+
minDis = min(distance(house[0],house[1],chickens[item][0],chickens[item][1]),minDis)
23+
rst+=minDis
24+
rstList.append(rst)
25+
26+
for i in range(1,chickenCnt):
27+
pos = cur +i
28+
if pos<chickenCnt:
29+
q.append((pos,cnt+1,chickenList+[pos]))
30+
else:
31+
break
32+
return min(rstList)
33+
34+
N,M = map(int,input().split())
35+
36+
maps = []
37+
38+
for i in range(N):
39+
maps.append(list(map(int,input().split())))
40+
41+
chickens = []
42+
houses = []
43+
44+
for i in range(N):
45+
for j in range(N):
46+
if maps[i][j]==2:
47+
chickens.append((i,j))
48+
if maps[i][j]==1:
49+
houses.append((i,j))
50+
51+
52+
53+
answer = int(1e9)
54+
55+
for i in range(0,len(chickens)-M+1):
56+
answer=min(bfs(i,len(chickens),M),answer)
57+
58+
print(answer)
59+
60+
61+
62+

0 commit comments

Comments
 (0)