Skip to content

Commit 6a25f66

Browse files
committed
[BOJ]#7576. 토마토/골드5/50min
https://www.acmicpc.net/problem/7576
1 parent 271191f commit 6a25f66

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

Hongjoo/백준/장군.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
import sys
3+
from collections import deque
4+
INF = 1e9
5+
# 상하좌우
6+
dy = [-1,1,0,0]
7+
dx = [0,0,-1,1]
8+
9+
dl= [[-1,1],[1,1],[1,-1],[-1,-1]] # 우상 . 우하 / 좌하 / 좌상
10+
dl_combined = [0,3,1,2,2,3,0,1]
11+
sy ,sx= map(int,sys.stdin.readline().split())
12+
ty,tx = map(int, sys.stdin.readline().split())
13+
14+
field = [[INF]*9 for _ in range(10)]
15+
field[sy][sx] = 0
16+
field[ty][tx] = -1
17+
18+
q = deque()
19+
q.append([sy,sx])
20+
21+
def check_bound(y,x) :
22+
if 0<= y <=9 and 0<= x <=8 and field[y][x]!=-1 : # 기물 x , field 안에 , 방문 상관x => 왕 위치만 안됨x
23+
return True
24+
return False
25+
def check_bound2(y,x) :
26+
if 0<= y <=9 and 0<= x <=8 : # 기물 가능
27+
return True
28+
return False
29+
30+
while q :
31+
cy,cx = q.popleft()
32+
for i in range(8):
33+
ny1, nx1 = cy +dy[i//2] , cx+dx[i//2]
34+
ny2, nx2 = ny1 + dl[dl_combined[i]][0] , nx1 + dl[dl_combined[i]][1]
35+
ny3 , nx3 = ny2 + dl[dl_combined[i]][0] , nx2 + dl[dl_combined[i]][1]
36+
37+
if check_bound(ny1, nx1) and check_bound(ny2, nx2) and check_bound2(ny3, nx3) :
38+
if ny3 == ty and nx3 == tx :
39+
# print(f"succes====")
40+
print(field[cy][cx] +1)
41+
42+
exit()
43+
elif field[cy][cx] +1 <= field[ny3][nx3] :
44+
field[ny3][nx3] =field[cy][cx] +1 # 업데이트
45+
# print(f"{cy}{cx} -> {i}: {ny1}{nx1} / {ny2}{nx2} / {ny3}, {nx3} => field{field[ny3][nx3]} ")
46+
47+
q.append([ny3,nx3])
48+
# print(f"{cy}{cx} -> {i}: {ny1}{nx2} / {ny2}{nx2} / {ny3}, {nx3} => field{field[ny3][nx3]} ")
49+
50+
print(field[ty][tx])

Hongjoo/백준/토마토.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
https://www.acmicpc.net/problem/7576
3+
# BFS, DFS
4+
(1) filed 값 중 1인 값 찾기
5+
(2) 상하좌우로 -1 빼고 확산
6+
(3) flag로 해당 이잔 turn에서 확산 유무를 판단 -> 안되면 turn 종료
7+
8+
"""
9+
# 입력
10+
import sys
11+
from collections import deque
12+
13+
input = sys.stdin.readline
14+
N,M = map(int, input().split())
15+
16+
field = [list(map(int,input().split())) for _ in range(M) ]
17+
# print(field)
18+
dy = [-1,1,0,0]
19+
dx = [-0,0,-1,1]
20+
# BFS
21+
22+
23+
lv = 0
24+
burn = []
25+
q = deque()
26+
for m in range(M) :
27+
for n in range(N) :
28+
# print(m , n)
29+
if field[m][n] == 1:
30+
# 안 익은 토마토 익히기
31+
q.append([m,n])
32+
33+
while q :
34+
# 각 turn 마다 토마도 전염시키기
35+
36+
# print(f"{t}턴 - {m}{n}")
37+
#[2] 익은 토마토 확산시키기
38+
# t+= 1
39+
y,x = q.popleft()
40+
for d in range(4):
41+
ny = y + dy[d] ; nx = x + dx[d]
42+
if 0<= ny <M and 0 <= nx < N and field[ny][nx] == 0:
43+
field[ny][nx] = field[y][x] +1
44+
q.append([ny,nx])
45+
46+
max_num = 0
47+
for i in range(M) :
48+
for j in range(N):
49+
if field[i][j]== 0 :
50+
print(-1)
51+
exit()
52+
max_num = max(max_num , field[i][j])
53+
print(max_num-1)
54+
55+
# burn.append([n,m])
56+
# q = deque()
57+
# for sy,sx in burn :
58+
# q.append([sy,sx])
59+
# while q :
60+

0 commit comments

Comments
 (0)