๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] 2667 ๋‹จ์ง€ ๋ฒˆํ˜ธ ๋ถ™์ด๊ธฐ BFS

by Jouureee 2021. 2. 19.

๋ฌธ์ œ :

www.acmicpc.net/problem/2667

 

2667๋ฒˆ: ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ

<๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ์ง€๋„๊ฐ€ ์žˆ๋‹ค. 1์€ ์ง‘์ด ์žˆ๋Š” ๊ณณ์„, 0์€ ์ง‘์ด ์—†๋Š” ๊ณณ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ฒ ์ˆ˜๋Š” ์ด ์ง€๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์—ฐ๊ฒฐ๋œ ์ง‘์˜ ๋ชจ์ž„์ธ ๋‹จ์ง€๋ฅผ ์ •์˜ํ•˜๊ณ , ๋‹จ์ง€์— ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ด๋ ค ํ•œ๋‹ค. ์—ฌ

www.acmicpc.net

 

๋ถ„์„ : 

์ด ๋ฌธ์ œ๋Š” BFS๋กœ ํ’€์—ˆ๋‹ค.(DFS๋กœ ํ’€์–ด๋„ ๋…ธ์ƒ๊ด€)

์ „ํ˜•์ ์ธ BFS ๋ฌธ์ œ์ด์ง€๋งŒ ๋Š์–ด์ง„ ๋ฐฉ๋ฌธ ์œ„์น˜๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ข…๋งŒ๋ถ์—์„œ ๋ณด์•˜๋˜ ๊ฒƒ์ฒ˜๋Ÿผ BFS_ALL์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

c++ ์ฝ”๋“œ :

//
//  2667_complex.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/02/14.
//

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX 100
int n;
int find_one = 0;
int cnt = 0;
bool visit[MAX][MAX];
bool map [MAX][MAX];

int dx[] = { 0, 0, 1, -1 }; //up down left right
int dy[] = { 1, -1, 0, 0 };



int dfs(pair<int, int> start){
    int count = 1;
    visit[start.first][start.second] = true;
    stack<pair<int, int>> s;
    s.push(make_pair(start.first, start.second));
    while(!s.empty()){//stack s๊ฐ€ ๋น„์–ด ์žˆ์ง€ ์•Š์„ ๋™์•ˆ
        int x = s.top().first;
        int y = s.top().second;
        //cout << y << " " << x << endl;
        s.pop();
        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){
                if(visit[nx][ny] == false && map[nx][ny]){ // ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ณ  ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ์ด๋ผ๋ฉด
                    visit[nx][ny] = true; // ๋ฐฉ๋ฌธํ•œ ๊ฒƒ์„ ํŒ๋ณ„
                    count++;
                    s.push(make_pair(nx, ny));
                }
            }
        }
    }
    return count;
    
}

void dfsAll(){
    vector<int> ans;
    memset(visit, false, sizeof(visit));
    
    pair<int, int> start;
    for(int i =0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(map[i][j] == true && visit[i][j] == false){ // ์•„์ง ๊ฐ€์ง€ ์•Š์•˜์ง€๋งŒ 1์ธ๊ฒŒ ๋‚จ์•„ ์žˆ๋Š” ๊ฒฝ์šฐ
                start = make_pair(i, j);
                int cnt = dfs(start);
                //cout << cnt << endl;
                ans.push_back(cnt);
            }
        }
    }
    sort(ans.begin(), ans.end());
    cout << (int)ans.size() << endl;

    for(int i = 0; i < (int)ans.size(); i++){
        cout << ans.at(i) << endl;
    }
    
}

int main(){
    cin >> n;
    
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int a;
            scanf("%1d", &a);
            if(a == 1){
                map[i][j] = true;
            }else{
                map[i][j] = false;
            }
        }
    }
    
    //    for(int i = 0; i < n; i++){
    //        for(int j = 0; j < n; j++){
    //
    //            cout << map[i][j] << " ";
    //        }
    //        cout << endl;
    //    }

    dfsAll();
    
    return 0;
}

๋Œ“๊ธ€