https://www.acmicpc.net/problem/4963
4963๋ฒ: ์ฌ์ ๊ฐ์
์ ๋ ฅ์ ์ฌ๋ฌ ๊ฐ์ ํ ์คํธ ์ผ์ด์ค๋ก ์ด๋ฃจ์ด์ ธ ์๋ค. ๊ฐ ํ ์คํธ ์ผ์ด์ค์ ์ฒซ์งธ ์ค์๋ ์ง๋์ ๋๋น w์ ๋์ด h๊ฐ ์ฃผ์ด์ง๋ค. w์ h๋ 50๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์์ ์ ์์ด๋ค. ๋์งธ ์ค๋ถํฐ h๊ฐ ์ค์๋ ์ง๋
www.acmicpc.net
๋์ด๋ฌด ์ด๋ ค์ ๋น..
ํผ์ ํด๊ฒฐํ์ง ๋ชปํด ๋ค๋ฅธ ๋ถ๋ค์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํด ์ดํดํ์๋ค. ์ดํดํ๊ณ ๋์ ์๋ก ์ง๋ดค๋ค!
์ด๊ฑธ ์๊ฐํด๋ธ ์ฌ๋ฌ๋ถ ์ฒ์ฌ์ ๊ฐ์?
#<์์ฑ์ฝ๋ x>
def dfs(x,y):
global cnt
graph[x][y] = 0
for i in range(8):
nx = x + dx[i]
ny = y + dy[i]
if 0<=nx<h and 0<=ny<w:
if graph[nx][ny]: #๋ฐฉ๋ฌธํ ์ ์์ผ๋ฉด ์ฆ, ๋
์ด๋ฉด
dfs(nx,ny)
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
w, h = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(h)]
cnt = 0
for i in range(h):
for j in range(w):
if graph[i][j]: #1์ด๋ฉด
cnt += 1
dfs(i,j)
print(*graph, sep = "\n")
print(cnt)
๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ๋ค.
์ฐ์ , while๋ฌธ์ ๋ฃ๊ธฐ ์ ์ ํ๋์ ์ผ์ด์ค๋ง ์ฑ๊ณตํด๋ณด์ ๋ผ๋ ์๊ฐ์ผ๋ก ์์ฑํ์๋ค. ์ฆ, ์์ง ์์ฑ๋ ์ฝ๋๊ฐ ์๋๋ค!
dfs ์ฝ๋๋ฅผ ์์ฑํ๋๊ฑด ํฌ๊ฒ ์ด๋ ต์ง ์์๋๋ฐ, 1์ด ์ฐ๊ฒฐ๋ ๊ฒฝ์ฐ์๋ง ์ด๋ป๊ฒ cnt๋ฅผ ๋๋ฆฌ๋์ง ํด๊ฒฐํ์ง ๋ชปํ์๋ค.
๋ฐ๋ณต๋ฌธ์ ํตํด ์ฒซ ์์๋ถํฐ ๋ง์ง๋ง ์์๊น์ง dfs๋ฅผ ์คํํด์ฃผ์ด์ผ ํ๋ค.
dfs๊ฐ ์คํ๋๋ฉด, ๊ทธ ์์น๋ฅผ 0์ผ๋ก ๋ฐ๊ฟ์ฃผ๋ ๊ฑธ ์๊ฐํ์ง ๋ชปํ๋ค.. ์ด๋ผ visited ๋ฆฌ์คํธ๋ฅผ ๊ตณ์ด ์ ์ ํ์์๊ฒ ๊ตฐ..๋๋ฐ๐
์ 0์ผ๋ก ๋ฐ๊ฟ์ฃผ๋์ง๋ ๋ค๋ค ์ ๊ฒ์ด๋ค! ๋ค์ ๋ฐฉ๋ฌธํ์ง ์๊ธฐ ์ํด 0_<-๐ค
๊ฒฐ๊ณผ๋ ์์ ๊ฐ์๋ฐ, graph ๋ชจ๋ ์์๋ฅผ ๋๋ฉด์ 0์ผ๋ก ๋ฐ๊ฟ์คฌ๊ธฐ ๋๋ฌธ์ ์ ๋ ๊ฒ ์ถ๋ ฅ๋๊ณ , cnt๋ ์ ๋๋ก ์ถ๋ ฅ๋๋ค.
์ด์ bfs๋ก๋ ๊ตฌํํด๋ณด์!
#<์์ฑ ์ฝ๋ x>
def bfs(x,y):
q = deque()
q.append( (x,y) )
while q:
x,y = q.popleft()
graph[x][y]=0
for i in range(8):
nx = x + dx[i]
ny = y + dy[i]
if (0<=nx<h and 0<=ny<w):
if graph[nx][ny]: #๋
์ด๋ฉด
q.append((nx,ny))
graph[nx][ny] = 0
๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์๋ค. ์ ์ฌ๋ฐ๋คใ ... ๋ถ๋ช ํ ๋ฌธ์ ์ ํ๋ฆด ๋ ๋์ด๋ฌด ์ฌ๋ฏธ์์๋๋ฐ ใ ใ ใ ใ ใ ใ ใ
์ด์ while๋ฌธ์ ํตํด ์ ์ฒด ์ฝ๋๋ฅผ ์์ฑํด์ฃผ์.
#<์์ฑ์ฝ๋>
from collections import deque
import sys
sys.setrecursionlimit(10000)
input = sys.stdin.readline
def dfs(x,y):
global cnt
graph[x][y] = 0
for i in range(8):
nx = x + dx[i]
ny = y + dy[i]
if 0<=nx<h and 0<=ny<w:
if graph[nx][ny]: #๋ฐฉ๋ฌธํ ์ ์์ผ๋ฉด ์ฆ, ๋
์ด๋ฉด
dfs(nx,ny)
def bfs(x,y):
q = deque()
q.append( (x,y) )
while q:
x,y = q.popleft()
graph[x][y]=0
for i in range(8):
nx = x + dx[i]
ny = y + dy[i]
if (0<=nx<h and 0<=ny<w):
if graph[nx][ny]: #๋
์ด๋ฉด
q.append((nx,ny))
graph[nx][ny] = 0
#์ ์ธ
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
while True:
w, h = map(int, input().split())
graph = [list(map(int, input().split())) for i in range(h)]
cnt = 0
if w == 0 and h == 0: break #์ข
๋ฃ์กฐ๊ฑด
for i in range(h):
for j in range(w):
if graph[i][j]: #1์ด๋ฉด
cnt += 1
bfs(i,j)
#dfs(i,j)
print(cnt)
๋ต์ ๋๊ฐ์ด ๋์ค์ง๋ง, dfs์ผ ๋, ์๊ฐ์ด๊ณผ๊ฐ ๋ ์
import sys
sys.setrecursionlimit(10000)
์ ์ถ๊ฐํด์ฃผ๋ ์ ๋ต์ฒ๋ฆฌ๊ฐ ๋์๋ค..
ํ์ด์ฌ์ ์ญ์๋ ๋๋ฌด ๋ฌด๊ฒ๋ค................c++์ ๊ฐ๋ฒผ์ด๋ฐ ํํ
'์ฝํ ์ค๋น' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
<dfs bfs>๋ฐฑ์ค 2667๋ฒ. ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ (0) | 2022.07.31 |
---|---|
<DFS BFS> ๋ฐฑ์ค 1012๋ฒ. ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2022.07.31 |
<dfs bfs> ๋ฐฑ์ค 2178๋ฒ. ๋ฏธ๋ก ํ์ (์ค๋ต๋ ธํธ) (0) | 2022.07.30 |
<DFS,BFS> ๋ฐฑ์ค 1260๋ฒ. DFS์ BFS (์ค๋ต๋ ธํธ) (0) | 2022.07.30 |
<๊ตฌํ> ๋ฐฑ์ค 1764๋ฒ. ๋ฃ๋ณด์ก - set()์ด์ฉ (0) | 2022.07.30 |