- toc {:toc}
๋ฌธ์
<๊ทธ๋ฆผ 1>๊ณผ ๊ฐ์ด ์ ์ฌ๊ฐํ ๋ชจ์์ ์ง๋๊ฐ ์๋ค. 1์ ์ง์ด ์๋ ๊ณณ์, 0์ ์ง์ด ์๋ ๊ณณ์ ๋ํ๋ธ๋ค. ์ฒ ์๋ ์ด ์ง๋๋ฅผ ๊ฐ์ง๊ณ ์ฐ๊ฒฐ๋ ์ง์ ๋ชจ์์ธ ๋จ์ง๋ฅผ ์ ์ํ๊ณ , ๋จ์ง์ ๋ฒํธ๋ฅผ ๋ถ์ด๋ ค ํ๋ค. ์ฌ๊ธฐ์ ์ฐ๊ฒฐ๋์๋ค๋ ๊ฒ์ ์ด๋ค ์ง์ด ์ข์ฐ, ํน์ ์๋์๋ก ๋ค๋ฅธ ์ง์ด ์๋ ๊ฒฝ์ฐ๋ฅผ ๋งํ๋ค. ๋๊ฐ์ ์์ ์ง์ด ์๋ ๊ฒฝ์ฐ๋ ์ฐ๊ฒฐ๋ ๊ฒ์ด ์๋๋ค. <๊ทธ๋ฆผ 2>๋ <๊ทธ๋ฆผ 1>์ ๋จ์ง๋ณ๋ก ๋ฒํธ๋ฅผ ๋ถ์ธ ๊ฒ์ด๋ค. ์ง๋๋ฅผ ์ ๋ ฅํ์ฌ ๋จ์ง์๋ฅผ ์ถ๋ ฅํ๊ณ , ๊ฐ ๋จ์ง์ ์ํ๋ ์ง์ ์๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ์ฌ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.

์ ๋ ฅ
์ฒซ ๋ฒ์งธ ์ค์๋ ์ง๋์ ํฌ๊ธฐ N(์ ์ฌ๊ฐํ์ด๋ฏ๋ก ๊ฐ๋ก์ ์ธ๋ก์ ํฌ๊ธฐ๋ ๊ฐ์ผ๋ฉฐ 5โคNโค25)์ด ์ ๋ ฅ๋๊ณ , ๊ทธ ๋ค์ N์ค์๋ ๊ฐ๊ฐ N๊ฐ์ ์๋ฃ(0ํน์ 1)๊ฐ ์ ๋ ฅ๋๋ค.
์ถ๋ ฅ
์ฒซ ๋ฒ์งธ ์ค์๋ ์ด ๋จ์ง์๋ฅผ ์ถ๋ ฅํ์์ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ ๋จ์ง๋ด ์ง์ ์๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ์ฌ ํ ์ค์ ํ๋์ฉ ์ถ๋ ฅํ์์ค.
์ถ์ฒ: https://www.acmicpc.net/problem/2667
ํ์ด
- ์๋ฃํ์ ์ฃผ์ํ์. ์ ๋ ฅ์ด ์ด๋ป๊ฒ ๋ค์ด์ค๋์ง๋ฅผ ํ์ธํ๊ณ ์ ๋ ฅ์ ๋ฐ๋ฅธ ์๋ฃ ๊ฐ๊ณต์ ์ฃผ์ํด์ผ ํ๋ค.
- ๊ณต๋ถํ ๋ถ๋ถ ์ฌ์ฉํ๋ ค ๋ ธ๋ ฅํ์.
- ๊ณต๊ฐ๊ณผ ๊ฐ์๋ฅผ ์ธ๋ ์ ํ์ ๊ฐ์๋ฅผ ์ธ๋ ์์น์ ์ ์ํ์.
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
int board[30][30];
int vis[30][30];
int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};
int n;
queue<pair<int, int>> q;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
char temp;
cin >> temp;
if(temp == '0')
board[i][j] = 0;
else
board[i][j] = 1;
}
}
/*
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << board[i][j] << '\t';
}
cout << endl;
}
*/
int area = 0;
vector<int> cVec;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int cnt = 0;
if(board[i][j] == 0 || vis[i][j] == 1) continue;
q.push({i, j});
vis[i][j] = 1;
area++;
cnt++;
while(!q.empty()){
auto cur = q.front(); q.pop();
for(int dir = 0; dir < 4; dir++){
int nx = cur.X + dx[dir];
int ny = cur.Y + dy[dir];
if(nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
if(board[nx][ny] == 0 || vis[nx][ny] == 1) continue;
vis[nx][ny] = 1;
q.push({nx, ny});
cnt++;
}
if(q.empty() && cnt != 0){
cVec.push_back(cnt);
}
}
}
}
/*
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << vis[i][j] << '\t';
}
cout << endl;
}
*/
sort(cVec.begin(), cVec.end());
cout << area << endl;
for(auto elem : cVec){
cout << elem << endl;
}
}