알고리즘/BOJ
[c/c++] 백준/BOJ 2667번 문제 - 단지번호붙이기
wonjun.Aden
2022. 3. 29. 15:04
https://www.acmicpc.net/problem/2667
2667번: 단지번호붙이기
<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여
www.acmicpc.net
문제

문제풀이
#include <bits/stdc++.h>
using namespace std;
string board[27];
int vis[27][27];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
//단지 번호붙이기
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int count =0;
for(int i=0;i<n;i++){
cin >> board[i];
}
/* cout << "==================\n";
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout << board[i][j];
}
cout << '\n';
} */
vector<int> ans;
for(int i=0;i < n; i++){
for(int j=0;j < n; j++){
if(board[i][j] == '0' || vis[i][j] == 1) continue;
queue<pair<int,int>> Q;
Q.push({i,j});
vis[i][j]=1;
int home=1;
count++;
while(!Q.empty()){
auto cur = Q.front(); Q.pop();
for(int dir=0;dir < 4;dir++){
int nx = cur.first + dx[dir];
int ny = cur.second+ dy[dir];
if(nx <0 || nx >= n || ny < 0 || ny >= n) continue;
if(board[nx][ny] == '0' || vis[nx][ny] == 1) continue;
Q.push({nx,ny});
vis[nx][ny] = 1;
home++;
}
}
ans.push_back(home);
}
}
sort(ans.begin(),ans.end());
cout << count << '\n';
for(auto c : ans){
cout << c << '\n';
}
return 0;
}
문제의 포인트
- 한칸씩 BFS 돌려보면 됨!!
반응형