728x90
๋ฌธ์
ํ์ด
์ ๋ ฅ
import sys
from collections import deque
imput = sys.stdin.readline
n = int(input())
graph = [list(map(int, input().split())) for _ in range(n)]
visited = [[False] * n for _ in range(n)]
dir = ((0, 1), (0, -1), (1, 0), (-1, 0))
์ฌ์ ๋๋๋ ๊ณผ์
0์ด ์๋ ์ขํ๋ฅผ ๋ฐ๊ฒฌํ ๊ฒฝ์ฐ ๊ทธ ์ขํ์ ์ฐ๊ฒฐ๋ ๊ฐ์ ์ ๋ถ mark๋ก ๋ฐ๊พผ๋ค.
mark = 1
for i in range(n):
for j in range(n):
if graph[i][j] and not visited[i][j]:
marking(i, j, mark)
mark += 1
marking ํจ์
๊ฐ ์ฌ์ ๋ฒํธ๋ฅผ ๋ฐ๊ฟ์ค
def marking(y, x, mark):
q = deque()
q.append((y, x))
graph[y][x] = mark
visited[y][x] = True
while q:
i, j = q.popleft()
for dy, dx in dir:
ni, nj = i + dy, j + dx
if condition(ni, nj) or not graph[ni][nj]: continue
graph[ni][nj] = mark
visited[ni][nj] = True
q.append((ni, nj))
condition ํจ์
def condition(ni, nj):
return ni < 0 or ni >= n or nj < 0 or nj >= n or visited[ni][nj]
getDist ํจ์
๊ฐ ์ขํ์์ ๋ค๋ฅธ ์ฌ์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ค.
def getDist(y, x, now):
q = deque()
q.append((y, x, 0))
while q:
i, j, cnt = q.popleft()
if graph[i][j] != 0 and graph[i][j] != now:
return cnt
for dy, dx in dir:
ni, nj = i + dy, j + dx
if condition(ni, nj) or graph[ni][nj] == now: continue
visited[ni][nj] = True
q.append((ni, nj, cnt + 1))
return 2000
๊ฐ ์ขํ์์ ๊ฐ์ฅ ๊ฐ๊น์ด ์ฌ์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ค.
ans = 2000
for i in range(n):
for j in range(n):
if graph[i][j] != 0:
visited = [[False] * n for _ in range(n)]
ans = min(ans, getDist(i, j, graph[i][j]))
์ฝ๋
import sys
from collections import deque
imput = sys.stdin.readline
n = int(input())
graph = [list(map(int, input().split())) for _ in range(n)]
visited = [[False] * n for _ in range(n)]
dir = ((0, 1), (0, -1), (1, 0), (-1, 0))
def condition(ni, nj):
return ni < 0 or ni >= n or nj < 0 or nj >= n or visited[ni][nj]
def marking(y, x, mark):
q = deque()
q.append((y, x))
graph[y][x] = mark
visited[y][x] = True
while q:
i, j = q.popleft()
for dy, dx in dir:
ni, nj = i + dy, j + dx
if condition(ni, nj) or not graph[ni][nj]: continue
graph[ni][nj] = mark
visited[ni][nj] = True
q.append((ni, nj))
def getDist(y, x, now):
q = deque()
q.append((y, x, 0))
while q:
i, j, cnt = q.popleft()
if graph[i][j] != 0 and graph[i][j] != now:
return cnt
for dy, dx in dir:
ni, nj = i + dy, j + dx
if condition(ni, nj) or graph[ni][nj] == now: continue
visited[ni][nj] = True
q.append((ni, nj, cnt + 1))
return 2000
mark = 1
for i in range(n):
for j in range(n):
if graph[i][j] and not visited[i][j]:
marking(i, j, mark)
mark += 1
ans = 2000
for i in range(n):
for j in range(n):
if graph[i][j] != 0:
visited = [[False] * n for _ in range(n)]
ans = min(ans, getDist(i, j, graph[i][j]))
print(ans - 1)
๋ง๋ฌด๋ฆฌ
๊ณจ๋ 3 bfs ๋ฌธ์ ๋ ์ด์ ์กธ์ ํด๋ ๋ ๋ฏ์ถ๋ค.
728x90