728x90
반응형
같은 실버등급에 있는 단지 수 찾기와 비슷한 문제라고 생각하여 똑같은 알고리즘으로 풀었습니다
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
//배추 심어진 좌표 저장할 큐 생성
queue<pair<int, int>> isBaechu;
//배추밭 vector 이중배열로 생성
vector<vector<int>> field(52, vector<int>(52, -2));
int moveX[4] = { -1,1,0,0 };
int moveY[4] = { 0,0,-1,1 };
void DeleteDFS(int x, int y) {
//주변 정보 저장할 큐 생성
queue<pair<int, int>> surround;
//해당 좌표 0으로 변경
field[y][x] = 0;
//주변정보 탐색하여 1이면 큐에 저장
for (int i = 0;i < 4;i++) {
int lastX = x + moveX[i];
int lastY = y + moveY[i];
if (field[lastY][lastX] == 1) {
surround.push(make_pair(lastX, lastY));
}
}
//주변 좌표 DFS수행
while (!surround.empty()) {
DeleteDFS(surround.front().first, surround.front().second);
surround.pop();
}
}
int findWormNum() {
//구역 수 저장할 변수
int cnt = 0;
while (!isBaechu.empty()) {
//배추의 좌표정보 받고 삭제
int y = isBaechu.front().first;
int x = isBaechu.front().second;
isBaechu.pop();
//좌표 1이면 DFS수행하고 구역 수 업데이트
if (field[y][x] == 1) {
DeleteDFS(x, y);
cnt++;
}
}
return cnt;
}
//배열 초기화 하는 함수
void reset() {
for (int i = 0;i < 52;i++) {
for (int j = 0;j < 52;j++) {
field[i][j] = -2;
}
}
}
int main() {
//테스트케이스 입력받기
int test;
int width, height, baechu;
cin >> test;
while (test--) {
//배추 정보 입력받기
cin >> width >> height >> baechu;
//배추가 심어져있는 좌표 field에 업데이트
while (baechu--){
int x, y;
cin >> x >> y;
field[y + 1][x + 1] =1;
//배추 정보 isBaechu에 저장
isBaechu.push(make_pair(y + 1, x + 1));
}
//구역 수 = 필요한 흰 벌레 수
cout<<findWormNum()<<'\n';
//필드 초기화
reset();
}
}
728x90
반응형
'백준 > SILVER' 카테고리의 다른 글
[백준 9095번][C++] 1, 2, 3 더하기 (0) | 2022.08.29 |
---|---|
[백준 1697번][C++] 숨바꼭질 (0) | 2022.08.07 |
[백준 11726번][C++] 2×n 타일링 (0) | 2022.08.04 |
[백준 2667번][C++] 단지 번호 붙이기 (0) | 2022.08.01 |
[백준 1931번][C++] 회의실 배정 (0) | 2022.07.29 |