https://www.acmicpc.net/problem/1012
나의 코드
T = int(input())
for t in range(T):
M, N, K = map(int, input().split())
field = [[0]*M for _ in range(N)]
visited = [[0]*M for _ in range(N)]
di = [0, 1, 0, -1]
dj = [1, 0, -1, 0]
q = []
for k in range(K):
x, y = map(int, input().split())
field[y][x] = 1
count = 0
for i in range(N):
for j in range(M):
if not visited[i][j] and field[i][j]:
q.append([i, j])
while q:
r, c = q.pop(0)
visited[r][c] = 1
for k in range(4):
ni = r + di[k]
nj = c + dj[k]
if 0 <= ni < N and 0 <= nj < M and field[ni][nj] == 1 and visited[ni][nj] == 0:
q.append([ni, nj])
visited[ni][nj] = 1
count += 1
print(count)
결과

내가 제일 자신있어하는 BFS문제다! 이번엔 빈칸에 배추를 심어야 했는데 바로 성공!
'스터디 1일 1커밋' 카테고리의 다른 글
| 240428 [BOJ/백준] 1303. 전쟁 - 전투 (0) | 2024.04.28 |
|---|---|
| 240427 [BOJ/백준] 19637. IF문 좀 대신 써줘 (0) | 2024.04.27 |
| 240425 [BOJ/백준] 1063. 킹 (0) | 2024.04.25 |
| 240422 [BOJ/백준] 1026. 보물 (0) | 2024.04.22 |
| 240422 [BOJ/백준] 17390. 이건 꼭 풀어야 해! (0) | 2024.04.22 |