ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 4963번 C++ 풀이
    카테고리 없음 2018. 11. 19. 23:15
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <climits>
    #include <string>
    #include <map>
    using namespace std;

    int buffer[51][51];
    int delta[8][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1},
    {1, 1} ,{-1, -1}, {1, -1}, {-1,1} };

    int main(){
    cin.tie(NULL);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    while ( !( n == 0 && m == 0 )){
    for (auto y = 0; y<m; y++){
    for (auto x=0; x<n; x++){
    cin >> buffer[x][y];
    }
    }
    int land = 0;
    for (auto y = 0; y<m; y++){
    for (auto x=0; x<n; x++){
    if (buffer[x][y] == 1){
    land++;
    buffer[x][y] = 0;
    queue<pair<int, int>> q;

    q.push(make_pair(x,y));

    while (!q.empty()){
    pair<int, int> top = q.front();
    q.pop();
    for (auto i=0; i<8; i++){
    int nextX = top.first + delta[i][0];
    int nextY = top.second + delta[i][1];

    if ( 0 <= nextX && nextX < n &&
    0 <= nextY && nextY <m ){
    if (buffer[nextX][nextY] == 1){
    buffer[nextX][nextY] = 0;
    q.push(make_pair(nextX, nextY));
    }
    }
    }
    }

    }
    }
    }
    cout << land << '\n';
    cin >> n >> m;

    }
    return 0;
    }


Designed by Tistory.