// BOJ 2667 - 단지번호붙이기
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int n,map[25][25],cnt;
int dx[] = {0,0,-1,1}, dy[4] = {1,-1,0,0};
vector v<int>;
void Init(){
scanf("%d",&n);
for(int i=0; i<n; i++) for(int j=0; j<n; j++){
scanf("%1d",&map[i][j]);
}
}
void dfs(int x, int y){
cnt++;
map[x][y] = 0;
for(int i=0; i<4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx<0 || ny<0 || nx>=n || ny>=n || !map[nx][ny]) continue;
dfs(nx,ny);
}
}
int main()
{
Init();
for(int i=0; i<n; i++) for(int j=0; j<n; j++)
if(map[i][j]==1){
cnt=0;
dfs(i,j);
v.push_back(cnt);
}
sort(v.begin(),v.end());
int size = v.size();
printf("%d\n",size);
for(int i=0; i<size; i++){
printf("%d\n",v[i]);
}
return 0;
}
// BOJ 10026 - 적록색약
#include <iostream>
using namespace std;
int n,cnt,cntt;
char map[110][110];
int visit[110][110] = { 0, };
int dx[] = { 0,0,-1,1 }, int dy[] = { 1,-1,0,0 };
void Init() {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> map[i][j];
}
}
}
void changeColor() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (map[i][j] == 'R') map[i][j] = 'G';
}
}
}
void dfs(int x, int y, char tmp)
{
visit[x][y] = 1;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || visit[nx][ny] == 1 || map[nx][ny] != tmp) continue;
dfs(nx, ny, tmp);
}
}
int main()
{
Init();
cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (visit[i][j]==0) {
dfs(i, j, map[i][j]);
cnt++;
}
}
}
changeColor();
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) visit[i][j] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (visit[i][j] == 0) {
dfs(i, j, map[i][j]);
cntt++;
}
}
}
cout << cnt << " " << cntt << endl;
return 0;
}
'알고리즘' 카테고리의 다른 글
[DT/UNIV] 파이썬 예제 (0) | 2020.06.23 |
---|---|
[D-13] 비슷한 유형 #2 (최대 몇 개를 고를 때..) (0) | 2019.04.02 |
[BOJ 14499] 주사위 굴리기 (0) | 2019.03.19 |
[SW 2117] 홈 방범 서비스 (0) | 2019.03.17 |
[D-28] 알고리즘 추천 문제 (0) | 2019.03.16 |