zoukankan      html  css  js  c++  java
  • sgu 125 Shtirlits dfs 难度:0

    125. Shtirlits

    time limit per test: 0.25 sec. 
    memory limit per test: 4096 KB

     

     

    There is a checkered field of size N x N cells (1 Ј N Ј 3). Each cell designates the territory of a state (i.e. N2 states). Each state has an army. Let A [i, j] be the number of soldiers in the state which is located on i-th line and on j-th column of the checkered field (1£i£N, 1£j£N, 0 £  A[i, j] £  9). For each state the number of neighbors, B [i, j], that have a larger army, is known. The states are neighbors if they have a common border (i.e. £  B[i, j]  £  4). Shtirlits knows matrix B. He has to determine the number of armies for all states (i.e. to find matrix A) using this information for placing forces before the war. If there are more than one solution you may output any of them.

     

    Input

    The first line contains a natural number N. Following N lines contain the description of matrix B - N numbers in each line delimited by spaces.

     

    Output

    If a solution exists, the output file should contain N lines, which describe matrix A. Each line will contain N numbers delimited by spaces. If there is no solution, the file should contain NO SOLUTION.

     

    Sample Input

    3
    1 2 1
    1 2 1
    1 1 0
    

     

    Sample Output

    1 2 3
    1 4 5
    1 6 7
    
     
    #include<cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int B[3][3],n,A[3][3];
    bool vis[3][3],orgvis[3][3][3][3];
    const int dx[5]={1,-1,0,0,0},dy[5]={0,0,1,-1,0};
    bool in(int x,int y){
        return x>=0&&x<n&&y>=0&&y<n;
    }
    void cpy(bool a[3][3],bool b[3][3]){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++)a[i][j]=b[i][j];
        }
    }
    bool check(int x,int y){
            int len=0;
            int lit=0;
            for(int k=0;k<4;k++){
                int tx=x+dx[k],ty=y+dy[k];
                if(in(tx,ty)){
                    if(vis[tx][ty]&&A[tx][ty]>A[x][y])len++;
                    if(!vis[tx][ty])lit++;
                }
            }
            if(len>B[x][y]||lit+len<B[x][y]){return false;}
            if(lit!=0)return true;
            return len==B[x][y];
    }
    bool dfs(int x,int y){
        vis[x][y]=true;
        for(int i=n*n;i>=0;i--){
            bool fl=false;
            A[x][y]=i;
            for(int k=0;k<5;k++){
                int tx=x+dx[k],ty=y+dy[k];
                if(in(tx,ty)&&vis[tx][ty]&&!check(tx,ty)){fl=true;break;}
            }
            if(fl)continue;
            cpy(orgvis[x][y],vis);
            for(int j=0;j<4;j++){
                int tx=x+dx[j],ty=y+dy[j];
                if(in(tx,ty)){
                    if(!vis[tx][ty]){
                        if(!dfs(tx,ty)){fl=true;break;}
                    }
                }
            }
            if(!fl)return true;
            cpy(vis,orgvis[x][y]);
        }
        vis[x][y]=false;
        return false;
    }
    int main(){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                scanf("%d",B[i]+j);
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(B[i][j]==0){dfs(i,j);break;}
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(!vis[i][j]||!check(i,j)){
                    puts("NO SOLUTION");
                    return 0;
                }
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                    printf("%d%c",A[i][j],j==n-1?'\n':' ');
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    【模板】并查集
    P1063能量项链
    多维动归第一题
    7.14测试
    7.12测试
    7.10测试
    几种display:table-cell的应用
    instanceof和typeof的区别
    右侧悬浮广告
    JavaScript判断浏览器类型及版本
  • 原文地址:https://www.cnblogs.com/xuesu/p/4010852.html
Copyright © 2011-2022 走看看