-
[백준/BOJ] 1012번 유기농 배추 (C++)알고리즘 문제풀이/백준 2021. 1. 14. 18:21
1012번: 유기농 배추
차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에
www.acmicpc.net
전형적인 BFS 유형의 문제였습니다.
세로의 길이를 먼저 입력받는 것을 주의
#include <iostream> #include <vector> #include <queue> #include <cstring> using namespace std; int map[51][51]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; void BFS(int sx, int sy, int m, int n) { queue<pair<int, int > > q; q.push({ sx, sy }); map[sx][sy] = 0; while(!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for(int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if(nx < 0 || nx >= n || ny < 0 || ny >= m || map[nx][ny] == 0) continue; q.push({ nx, ny }); map[nx][ny] = 0; } } } int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while(T--) { int n, m, k, answer = 0; cin >> n >> m >> k; while(k--) { int x, y; cin >> x >> y; map[x][y] = 1; } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(map[i][j] == 1) { BFS(i, j, m, n); answer++; } } } cout << answer << "\n"; } }
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준/BOJ] 16964번 DFS 스페셜 저지 (C++) (0) 2021.01.15 [백준/BOJ] 7569번 토마토 (C++) (0) 2021.01.14 [백준/BOJ] 16940번 BFS 스페셜 저지 (0) 2021.01.14 [백준/BOJ] 4179번 불! (C++) (0) 2021.01.13 [백준/BOJ] 7576번 토마토 (C++) (0) 2021.01.13