-
Notifications
You must be signed in to change notification settings - Fork 0
/
2146) 다리 만들기.py
90 lines (63 loc) · 2.19 KB
/
2146) 다리 만들기.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import sys
from collections import deque
def bfs_groupping(r,c): # 그룹짓기
global map_
q = deque()
q.append((r,c))
while q:
front = q[0]
row = front[0]
col = front[1]
q.popleft()
for i in range(4):
nrow = row + drow[i]
ncol = col + dcol[i]
if nrow >= N or ncol >= N or nrow < 0 or ncol < 0: continue
if map_[nrow][ncol] == 1 and not visited[nrow][ncol]:
map_[nrow][ncol] = map_[row][col] #같은 색으로 칠하기
visited[nrow][ncol] = True
q.append((nrow,ncol))
def bfs_calcdist(q):
global map_, dist
ret = 999999
while q:
front = q[0]
row = front[0]
col = front[1]
q.popleft()
for i in range(4):
nrow = row + drow[i]
ncol = col + dcol[i]
if nrow >= N or ncol >= N or nrow < 0 or ncol < 0: continue
if map_[nrow][ncol] == 0 and dist[nrow][ncol] == 0: #바다 건너기
dist[nrow][ncol] = dist[row][col]+1 #거리 계산
map_[nrow][ncol] = map_[row][col] #어떤 섬에서 출발했는지 표시
q.append((nrow,ncol))
if map_[nrow][ncol] != 0: #건너다가 땅(다리) 발견
if map_[nrow][ncol] != map_[row][col]: #각각 다른 섬에서부터 뻗어나온 다리라면
distance = dist[nrow][ncol] + dist[row][col]
ret = min(ret,distance) #찾은 다리가 최소가 아닐 수도 있다
return ret
drow = (1,0,-1,0)
dcol = (0,1,0,-1)
N = int(sys.stdin.readline())
map_ = [[] for _ in range(N)]
visited = [[False]*N for _ in range(N)]
dist = [[0]*N for _ in range(N)]
for i in range(N):
map_[i] = list(map(int,sys.stdin.readline().split()))
cnt = 1
for i in range(N):
for j in range(N):
if map_[i][j] == 1 and not visited[i][j]:
map_[i][j] = cnt
visited[i][j] = True
bfs_groupping(i,j)
cnt += 1
que = deque()
for i in range(N):
for j in range(N):
if map_[i][j] != 0 and dist[i][j] == 0:
que.append((i,j))
mindist = bfs_calcdist(que)
print(mindist)