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;
}

  

转载于:https://www.cnblogs.com/xuesu/p/4010852.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前台: (1)注册登录模块:按照学校的相关规定进行注册和登录。 (2)招聘信息查看:高校毕业生们可以网站首页上查看所有的招聘信息,除此之外还可以输入公司名称或岗位名称进行搜索。 (3)用人单位模块:此模块为宣传用人单位的主要功能模块,具体包括用人单位简介、岗位需求及职责及公司介绍等功能。 (4)就业指导:学生朋友们在就业前可以通过此模块获取指导。 (5)新闻信息:为了让用户们可以了解到最新的新闻动态,本系统可以通过新闻信息查看功能阅读近期的新闻动态。 (6)在线论坛:毕业季的同学们可以通过此模块相互交流。 后台: (1)系统用户管理模块:可以查看系统内的管理员信息并进行维护。 (2)学生管理模块:通过此功能可以添加学生用户,还可以对学生信息进行修改和删除。 (3)用人单位管理模块:管理员用户通过此模块可以管理用人单位的信息,还可以对用人单位信息进行查看和维护。 (4)招聘管理模块:管理员通过此功能发布和维护系统内的照片信息。 (5)就业指导管理模块:通过此模块可以编辑和发布就业指导信息,从而更好的帮助就业季的同学们。 (6)论坛管理:通过论坛管理可以查看论坛中的主题帖及里面的回复信息,除此之外还可以对论坛中的信息进行维护和管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值