题目描述:
125. Shtirlits
time limit per test: 0.5 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. 0 £ 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
题目给出的数据范围比较小,考虑搜索的方法。
因为n=1或n=2的情况可以直接枚举暴搞。
n=3时,我们给搜索加点减支,其实还是比较容易过的一个题。
代码写的很挫。。。
太懒了我。。。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>
#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)
using namespace std;
int const nMax = 1000;
int const base = 10;
typedef int LL;
typedef pair<LL,LL> pij;
int B[4][4],A[4][4];
int n;
bool check(){
FOR(i,n) {
FOR(j,n){
int d=0;
if(i-1>0&&A[i-1][j]>A[i][j])d++;
if(i+1<=n&&A[i+1][j]>A[i][j])d++;
if(j-1>0&&A[i][j-1]>A[i][j])d++;
if(j+1<=n&&A[i][j+1]>A[i][j])d++;
if(d!=B[i][j])return false;
}
}
return true;
}
bool check(int x,int y){
int d=0,f=0;
if(x-1>0)f++;if(x+1<=n)f++;
if(y-1>0)f++;if(y+1<=n)f++;
if(x-1>0&&A[x-1][y]!=-1) { if(A[x-1][y]>A[x][y]) d++; f--;}
if(x+1<=n&&A[x+1][y]!=-1){ if(A[x+1][y]>A[x][y]) d++; f--;}
if(y-1>0&&A[x][y-1]!=-1) { if(A[x][y-1]>A[x][y]) d++; f--;}
if(y+1<=n&&A[x][y+1]!=-1){ if(A[x][y+1]>A[x][y]) d++; f--;}
if(B[x][y]!=d)return false;
return true;
}
bool dfs(int step){
if(step==9){
if(check())return true;
else return false;
}
if(step==4&&!check(1,1))return false;
if(step==5&&!check(1,2))return false;
if(step==6&&!check(1,3))return false;
if(step==7&&!check(2,1))return false;
if(step==8&&(!check(2,2)||!check(3,1)))return false;
int x=step/3+1,y=step%3+1;
for(int i=0;i<10;i++){
A[x][y]=i;
if(dfs(step+1))return true;
A[x][y]=-1;
}
return false;
}
int main(){
cin>>n;
FOR(i,n) FOR(j,n) cin>>B[i][j];
if(n==1) { if(B[1][1]!=0) printf("NO SOLUTION\n"); else puts("1"); return 0;}
if(n==2) {
FOR(a,10) FOR(b,10) FOR(c,10) FOR(d,10) {
A[1][1]=a-1,A[1][2]=b-1,A[2][1]=c-1,A[2][2]=d-1;
if(check()){FOR(i,n) printf("%d %d\n",A[i][1],A[i][2]);return 0;}
}
printf("NO SOLUTION\n");
}
if(n==3){
CLR(A,-1);
if(dfs(0))
FOR(i,n) printf("%d %d %d\n",A[i][1],A[i][2],A[i][3]);
else
printf("NO SOLUTION\n");
}
return 0;
}