ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 7733. 치즈 도둑
    SWexpertAcademy 2019. 7. 14. 01:09
    
    #include<iostream>
    #include<queue>
    #include<utility>
    
    using namespace std;
    
    
    int test;
    int n;
    int result = 0;
    int a[100][100];
    int d[100][100];
    
    bool check[100][100];
    int dx[] = {0,0,1,-1};
    int dy[] ={1,-1,0,0};
    void initial(){
        for(int i = 0; i < 100; i++){
            for(int j = 0; j < 100; j++){
                a[i][j] = 0;
                d[i][j] = 0;
                check[i][j] = false;
            }
        }
    }
    void is(int day){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(a[i][j] <= day)
                {
                    d[i][j] = 1;
                }
                check[i][j] = false;
            }
        }
    }
    
    bool checking(){
        int c = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(d[i][j] == 1){
                    c += 1;
                }
            }
        }
        if( c== n * n)
            return true;
        return false;
    }
    
    void bfs(){
        queue<pair<int, int>> q;
        result = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(check[i][j] == false){
                    if(d[i][j] == 0){
                        result += 1;
                        q.push(make_pair(i,j));
                        check[i][j] = true;
                        while(!q.empty()){
                            int x = q.front().first; int y = q.front().second;
                            q.pop();
                            for(int k = 0; k < 4; k++){
                                int nx = x+ dx[k]; int ny = y+ dy[k];
                                if(nx >= 0 && nx < n && ny >=0 && ny < n)
                                {
                                    if(check[nx][ny] == false){
                                        if(d[nx][ny] == 0){
                                            q.push(make_pair(nx, ny));
                                            check[nx][ny] = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    int main(){
        cin>>test;
    
        for(int i = 1; i <= test; i++){
            initial();
            cin>>n;
            int ex = 0;
            for(int i = 0; i < n; i++){
                for(int j = 0; j < n; j++){
                    cin>>a[i][j];
                    if(a[i][j] == 1){
                       ex++; 
                    }
                        
                }
            }
            bool go = false;
            if(ex == n*n) go = true;
            int day = 1;
            int max = 0;
            while(day <= 100)
            {
                is(day);
                bfs();
                if(max < result) max = result;
                if(checking()== true) break;
                day++;
    
            }
            if(go == true){
                cout<<"#"<<i<<" "<<1<<endl;
            }
            else
                cout<<"#"<<i<<" "<<max<<endl;
        }
    
    }

    이 문제 이상하다.. 처음부터 계속 동일한 BFS방식으로 답구해내면 테스트 케이스중 10개 맞는다고 나오는데..

    알고보니 배열이 전부 1로 채워져있을때 한덩이로 취급 해야 맞는거같다.

    근데 이러면 문제 전부가 오류가나야되는거아닌가?

    댓글

Designed by Tistory.