ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 2667 C++ 풀이
    백준 2018. 10. 3. 22:00
    #include <iostream>
    #include <algorithm>
    #include <queue>
    using namespace std;
    int N;
    bool buffer[101][101];
    priority_queue<int, vector<int>, greater<int>> q;
    struct Dir {
    int x, y;
    };
    Dir moveDir[4] = {{1,0}, {-1, 0},{0, 1}, {0,-1}};
    int villa(int x, int y){
    if (0 <= x && x < N && 0<= y && y < N){
    if (buffer[x][y] == false) {
    return 0;
    }
    buffer[x][y] = false;
    int sum = 1;
    for (int i = 0; i <4 ; i++){
    sum += villa(x+ moveDir[i].x, y + moveDir[i].y);
    }
    return sum;
    }else {
    return 0;
    }
    }


    int main() {
    cin.tie(NULL);
    ios::sync_with_stdio(false);

    cin >> N;
    for (int i = 0; i<N; i++){
    for (int j = 0; j <N; j++){
    char temp;
    cin >> temp;
    if (temp == '1') {
    buffer[i][j] = true;
    }
    }
    }
    for (int i = 0; i< N; i++){
    for (int j = 0; j<N; j++){
    int danzi = villa(i,j);
    if (danzi > 0) {
    q.push(danzi);
    }
    }
    }
    cout << q.size() << endl;
    while (!q.empty()){
    cout << q.top() << endl;
    q.pop();
    }
    return 0;
    }


    처음으로 풀어 본 플러드 필 알고리즘입니다.


    예전에 본 강의 중에 플러드 필 알고리즘을 이용ㄷ해서,

    게임의 맵의 모든 타일이 서로 통하도록 랜덤 맵을 생성하게 하는 스크립트가 있었는데,

     그 강좌가 생각나기도 했습니다.

     https://www.youtube.com/watch?v=2ycN6ZkWgOo

    '백준' 카테고리의 다른 글

    백준 1890 C++ 풀이  (0) 2018.10.04
    백준 10216번 풀이  (0) 2018.10.03
    백준 2178번 C++ 풀이  (0) 2018.10.03
    백준 1260번 C++ 풀이  (0) 2018.09.06
    백준 1547번 C++ 풀이  (0) 2018.09.06
Designed by Tistory.