๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš“ Self Study/๐Ÿ”“ BaekJoon

๋ฐฑ์ค€ BaekJoon (์•ˆ์ „ ์˜์—ญ, 2468๋ฒˆ) C++

by UKHYUN22 2022. 1. 12.
728x90

 

์ด์ค‘ ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”๋ฅผ ํ•  ๋•Œ vector ๋ง๊ณ  int[][] ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  memset(arr, 0, sizeof(arr)) ๋ฅผ ์‚ฌ์šฉํ•˜์ž.

๊ทธ๋ฆฌ๊ณ  ์ด ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ๋ฌผ์ด ์•„์˜ˆ ์•ˆ ์˜ค๋Š” ๊ฒฝ์šฐ๊นŒ์ง€ ํ•ด์ค˜์•ผ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋‹ค.

 

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <cstring>

    using namespace std;

    int dx[4] = {0,0,-1,1};
    int dy[4] = {1,-1,0,0};

    int main() {
        ios_base::sync_with_stdio(false); cin.tie(NULL);
        
        int N = 0;
        cin >> N;
        int arr[N+1][N+1];
        int max = 0;

        for(int i = 0 ; i < N ; i++) {
            for(int j = 0 ; j < N ; j++) {
                cin >> arr[i][j];
                if(max < arr[i][j])
                    max = arr[i][j];
            }
        }
        int count = 0;
        int max_count = 0;
        for(int k = 0 ; k <= max ; k++) {
            queue<pair<int,int>> q;
            int visited[N+1][N+1];
            memset(visited, 0, sizeof(visited));

            for(int i = 0 ; i < N ; i++) {
                for(int j = 0 ; j < N ; j++) {
                    if(arr[i][j] > k && visited[i][j] == 0) {
                        q.push({i,j});
                        visited[i][j] = 1;
                        count++;


                        while(!q.empty()) {
                            int row = q.front().first;
                            int col = q.front().second;
                            q.pop();
                            for(int m = 0 ; m < 4 ; m++){
                                int n_dx = col + dx[m];
                                int n_dy = row + dy[m];
                                if(n_dy >= 0 && n_dy < N && n_dx >= 0 && n_dx < N && 
                                    arr[n_dy][n_dx] > k && visited[n_dy][n_dx] == 0) {
                                    visited[n_dy][n_dx] = 1;
                                    q.push({n_dy, n_dx});
                                }
                            }
                        }
                    }
                }
            }
            if(max_count < count)
                    max_count = count;
            count = 0;
        }

        cout << max_count;

        return 0;
    }