ํฐ์คํ ๋ฆฌ ๋ทฐ
https://www.acmicpc.net/problem/1012
#DFS ํ์ด
import sys
from collections import deque
sys.setrecursionlimit(1000000)
def DFS(x, y):
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
for i in range(4):
nowx = x + dx[i]
nowy = y + dy[i]
if (0<=nowx<N) and (0<=nowy<M):
if graph[nowx][nowy] == 1:
graph[nowx][nowy] = -1
DFS(nowx, nowy)
input = sys.stdin.readline
T = int(input().rstrip())
for i in range(T):
M, N, K = map(int, input().rstrip().split())
graph = [[0]*M for _ in range(N)]
cnt = 0
#๋ฐฐ์ถ ์์น์ 1ํ
for j in range(K):
X, Y = map(int, input().rstrip().split())
graph[Y][X] = 1
for x in range(M):
for y in range(N):
if graph[y][x] == 1:
DFS(y, x)
cnt+=1
print(cnt)
# BFS ํ์ด
import sys
from collections import deque
sys.setrecursionlimit(1000000)
def BFS(a, b):
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
queue = deque()
queue.append((a,b))
graph[a][b] = 0
while queue:
x,y = queue.popleft()
for i in range(4):
nx = x+dx[i]
ny = y+dy[i]
if 0<=nx<N and 0<=ny<M:
if graph[nx][ny] ==1:
graph[nx][ny] = -1
queue.append((nx,ny))
input = sys.stdin.readline
T = int(input().rstrip())
for i in range(T):
M, N, K = map(int, input().rstrip().split())
graph = [[0]*M for _ in range(N)]
cnt = 0
#๋ฐฐ์ถ ์์น์ 1ํ
for j in range(K):
X, Y = map(int, input().rstrip().split())
graph[Y][X] = 1
for x in range(M):
for y in range(N):
if graph[y][x] == 1:
#DFS(y, x)
BFS(y, x)
cnt+=1
print(cnt)
๋ฐ์ํ
'Programming > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์คํ/ํ] lv.2 ๊ธฐ๋ฅ๊ฐ๋ฐ | Python ํ์ด์ฌ (0) | 2024.02.06 |
---|---|
[์คํ/ ํ] lv.1 ๊ฐ์ ์ซ์๋ ์ซ์ด | Python ํ์ด์ฌ (0) | 2024.02.06 |
230224 [SQL(JOIN)&DFS+BFS&๊ตฌํ๋ฌธ์ ๋ค ๋ณต์ต] (0) | 2023.09.02 |
230223 [SQL ๋ฌธ๋ฒ ์ ๋ฆฌ] (0) | 2023.09.02 |
230221 [ํ๋ก๊ทธ๋๋จธ์ค ํ๊ฒ ๋๋ฒ] (0) | 2023.09.02 |
๊ณต์ง์ฌํญ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
- Total
- Today
- Yesterday
๋งํฌ
TAG
- ์ฝํ
- ํ๋ก๊ทธ๋๋จธ์ค
- StableDiffusion
- ๋ ผ๋ฌธ๋ฆฌ๋ทฐ
- ํ ํฌ์๋ฐ
- ํ์ด์ฌ์ฝํ
- ์ฝ๋ฉ๊ณต๋ถ
- AIRUSH
- ์ฝํ ์ค๋น
- WGAN
- ์คํ ์ด๋ธ๋ํจ์
- CLOVAX
- dreambooth
- ๊ตฌ๊ธ์ฝ๋ฉ
- DALLE
- AIRUSH2023
- ํ์ด์ฌ
- ๊ธฐ์ ์ปจํผ๋ฐ์ค
- HyperCLOVA
- MYSQL
- C์ธ์ด
- AI์ปจํผ๋ฐ์ค
- SKTECHSUMMIT
- gan
- SQL
- lgaimers
- ๋๋ฆผ๋ถ์ค
- ์ฝ๋ฉ์๋ฌ
- ๋ ผ๋ฌธ์ฝ๊ธฐ
- Aimers
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
๊ธ ๋ณด๊ดํจ