Connected component์ ๊ฐฏ์๋ฅผ ์ฐพ๋ ๋ฌธ์ ์ด๋ค. ํ๋ก๊ทธ๋๋จธ์ค์์๋ ๋์ผํ ๋ฌธ์ ๊ฐ ์์๋๋ฐ, ํ๋ก๊ทธ๋๋จธ์ค์์ ํต๊ณผํ ์๊ณ ๋ฆฌ์ฆ๋๋ก ํ์๋๋ฐ ๊ณ์ ์๊ฐ์ด๊ณผ๊ฐ ๋ด๋ค. ์ด์ฌํ ์ฐพ์๋ณด๋ ์๊ณ ๋ฆฌ์ฆ์์ ์ค์๊ฐ ์์๊ณ ํ๋ก๊ทธ๋๋จธ์ค๋ ํ ์คํธ ์ผ์ด์ค๊ฐ ์ ์ด์ ํต๊ณผํ ๊ฒ ๋ฟ์ด์๋ค.
๐ผ ๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/1012
- ํฐ์ด: ์ค๋ฒ II
- ๋ถ๋ฅ: ๊ทธ๋ํ ์ด๋ก , ๊ทธ๋ํ ํ์, ๋๋น ์ฐ์ ํ์
โ TRIAL 1.
์ฝ๋
from collections import deque
def check_adj(farm, v, M, N):
if v[0]<M and v[1]<N:
if farm[v[0]][v[1]]:
return True
def bfs(farm, start, M, N):
queue = deque([start])
while queue:
v = queue.popleft()
farm[v[0]][v[1]] = 0
adj_right = (v[0], v[1]+1)
if check_adj(farm, adj_right, M, N):
queue.append(adj_right)
adj_bottom = (v[0]+1, v[1])
if check_adj(farm, adj_bottom, M, N):
queue.append(adj_bottom)
return farm
def solution(farm):
ans = 0
for m in range(M):
for n in range(N):
if farm[m][n]:
ans += 1
farm = bfs(farm, (m, n), M, N)
return ans
T = int(input())
for _ in range(T): #for each test case
M, N, K = map(int, input().split( ))
#prepare farm
farm = [[0 for _ in range(N)] for _ in range(M)]
for _ in range(K):
x, y = map(int, input().split( ))
farm[x][y] = 1
print(solution(farm))
ํ๋ฆฐ ์์ธ
์๊ฐ์ด๊ณผ. ๊ฐ node์ ๋ํด์ ์ํ์ข์ฐ ๋ชจ๋ ์ธ์ ํ node๋ฅผ ๊ฒ์ฌํ์ด์ผ ํ๋๋ฐ ์ค๋ฅธ์ชฝ๊ณผ ์๋๋ง ๊ฒ์ฌํ๋ค.
์์ธ ์ผ์ด์ค: https://www.acmicpc.net/board/view/93090
โ TRIAL 2.
์ฝ๋
from collections import deque
def check_adj(farm, v, M, N):
if v[0]<M and v[1]<N:
if farm[v[0]][v[1]]:
return True
def bfs(farm, start, M, N):
queue = deque([start])
farm[start[0]][start[1]] = 0
while queue:
v = queue.popleft()
farm[v[0]][v[1]] = 0
adjs = [(v[0], v[1]+1), (v[0], v[1]-1), (v[0]+1, v[1]), (v[0]-1, v[1])]
for adj in adjs:
if check_adj(farm, adj, M, N):
queue.append(adj)
return farm
def solution(farm):
ans = 0
for m in range(M):
for n in range(N):
if farm[m][n]:
ans += 1
farm = bfs(farm, (m, n), M, N)
return ans
T = int(input())
answers = []
for _ in range(T): #for each test case
M, N, K = map(int, input().split( ))
#prepare farm
farm = [[0 for _ in range(N)] for _ in range(M)]
for _ in range(K):
x, y = map(int, input().split( ))
farm[x][y] = 1
print(solution(farm))
์์ ํ ๋ถ๋ถ
- ๊ฐ node์ ๋ํด ์ํ์ข์ฐ ๊ฒ์ฌํ๋๋ก ์ถ๊ฐ
ํ๋ฆฐ ์์ธ
์๊ฐ์ด๊ณผ. ์ํ์ข์ฐ node๊ฐ ์ ํจํ ๊ฐ์ธ์ง ๊ฒ์ฌํ๋ check_adj ํจ์์์ ์ขํ๊ฐ์ด 0๋ณด๋ค ํฐ์ง ๊ฒ์ฌํ์ง ์์์ ์์ ๋ฒ์์ ์ขํ๊ฐ์ ํต๊ณผ์ํด.
์์ธ ์ผ์ด์ค: https://www.acmicpc.net/board/view/93090
โ TRIAL 3.
์ฝ๋
from collections import deque
def check_adj(farm, v, M, N):
if 0<=v[0]<M and 0<=v[1]<N:
if farm[v[0]][v[1]]:
return True
def bfs(farm, start, M, N):
queue = deque([start])
farm[start[0]][start[1]] = 0
while queue:
v = queue.popleft()
farm[v[0]][v[1]] = 0
adjs = [(v[0], v[1]+1), (v[0], v[1]-1), (v[0]+1, v[1]), (v[0]-1, v[1])]
for adj in adjs:
if check_adj(farm, adj, M, N):
queue.append(adj)
return farm
def solution(farm):
ans = 0
for m in range(M):
for n in range(N):
if farm[m][n]:
ans += 1
farm = bfs(farm, (m, n), M, N)
return ans
T = int(input())
answers = []
for _ in range(T): #for each test case
M, N, K = map(int, input().split( ))
#prepare farm
farm = [[0 for _ in range(N)] for _ in range(M)]
for _ in range(K):
x, y = map(int, input().split( ))
farm[x][y] = 1
print(solution(farm))
์์ ํ ๋ถ๋ถ
- ์ํ์ข์ฐ node์ ๋ฒ์๊ฐ 0๋ถํฐ M, 0๋ถํฐ N ์ฌ์ด์ ๊ฐ์ธ์ง ๊ฒ์ฌํจ.
ํ๋ฆฐ ์์ธ
์๊ฐ์ด๊ณผ. ์ธ์ node๋ฅผ queue์ ๋ฃ๊ณ , ์ดํ์ queue์์ ๊ฐ node๋ฅผ popํ ๋ ๊ทธ ๊ฐ์ 0์ผ๋ก ๋ฐ๊ฟ์คฌ๋๋ฐ, BFS๋ ๊ฐ์ depth์ node๋ฅผ ๋ชจ๋ ๊ฒ์ฌํ ํ ๊ทธ ๋ค์ depth๋ก ๋์ด๊ฐ๊ธฐ ๋๋ฌธ์ ์ด๋ ๊ฒ ํ๋ฉด ์ค๋ณต์ผ๋ก queue์ ๊ฐ์ด ๋ค์ด๊ฐ๋ ๊ฒฝ์ฐ๊ฐ ์๊น.
์์ธ ์ผ์ด์ค: https://www.acmicpc.net/board/view/84210
๐ก Solution
์ฝ๋
from collections import deque
def check_adj(farm, v, M, N):
if 0<=v[0]<M and 0<=v[1]<N:
if farm[v[0]][v[1]]:
return True
def bfs(farm, start, M, N):
queue = deque([start])
farm[start[0]][start[1]] = 0
while queue:
v = queue.popleft()
adjs = [(v[0], v[1]+1), (v[0], v[1]-1), (v[0]+1, v[1]), (v[0]-1, v[1])]
for adj in adjs:
if check_adj(farm, adj, M, N):
queue.append(adj)
farm[adj[0]][adj[1]] = 0
return farm
def solution(farm):
ans = 0
for m in range(M):
for n in range(N):
if farm[m][n]:
ans += 1
farm = bfs(farm, (m, n), M, N)
return ans
T = int(input())
answers = []
for _ in range(T): #for each test case
M, N, K = map(int, input().split( ))
#prepare farm
farm = [[0 for _ in range(N)] for _ in range(M)]
for _ in range(K):
x, y = map(int, input().split( ))
farm[x][y] = 1
print(solution(farm))
์ฝ๋ ์ค๋ช
- ๋ชจ๋ node์ ๋ํด ๊ฒ์ฌ๋ฅผ ์งํํ์ฌ ๊ฐ์ด 1์ธ ๊ฒฝ์ฐ BFS๋ฅผ ์ด์ฉํ์ฌ connected component๋ค์ ๋ชจ๋ 0์ผ๋ก ๋ฐ๊ฟ์ค๋ค.
์ฃผ์ํ ์
- BFS๋ก connected component๋ฅผ ์ฐพ์ ๋, ์ํ์ข์ฐ ๋ชจ๋๋ฅผ ํ์ํด์ผ ํ๋ค.
- queue์ node๋ฅผ ๋ฃ์ ๋ ํด๋น node์ ๊ฐ์ ๋์์ 0์ผ๋ก ๋ฃ์ด์ฃผ์ด์ผ ํ๋ค. pop ํ ๋ค์ 0์ผ๋ก ๋ฐ๊ฟ์ฃผ๋ฉด ๊ทธ ์ฌ์ด์ ์ค๋ณต์ผ๋ก queue์ ๋ค์ด๊ฐ๋ ๊ฐ์ด ์๊ธด๋ค. ์ฆ queue์ ๋ฃ์๊ณผ ๋์์ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ๋ฅผ ํด ์ฃผ์ด์ผ ํจ.
'๐ CS > ์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค 14500๋ฒ] ํ ํธ๋ก๋ฏธ๋ ธ (python/ํ์ด์ฌ) (2) | 2022.09.14 |
---|---|
[๋ฐฑ์ค 7569๋ฒ] ํ ๋งํ (python/ํ์ด์ฌ) (0) | 2022.09.09 |
[๋ฐฑ์ค 16926๋ฒ] ๋ฐฐ์ด ๋๋ฆฌ๊ธฐ 1 (python/ํ์ด์ฌ) (0) | 2022.07.05 |
[๋ฐฑ์ค 2504๋ฒ] ๊ดํธ์ ๊ฐ (python/ํ์ด์ฌ) (0) | 2022.07.01 |
[๋ฐฑ์ค 1697๋ฒ] ์จ๋ฐ๊ผญ์ง (python/ํ์ด์ฌ) (0) | 2022.06.30 |