sgu 125 Shtirlits dfs 难度:0
2014-10-08 11:57
363 查看
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. 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 |
#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; }
相关文章推荐
- sgu108. Self-numbers 2 滚动数组 打表 难度:1
- SGU 141.Jumping Joe 数论,拓展欧几里得,二元不等式 难度:3
- 快速切题 sgu 112. a^b-b^a 大数 次方 难度:0 非java:1
- Shtirlits - SGU 125(搜索)
- SGU 139. Help Needed! 逆序数,奇偶性,分析 难度:0
- SGU 144. Meeting 概率dp 几何概率分布 难度:0
- SGU 140. Integer Sequences 线性同余,数论 难度:2
- 快速切题 sgu 111.Very simple problem 大数 开平方 难度:0 非java:1
- SGU 131. Hardwood floor 状压dp 难度:2
- sgu 142. Keyword 暴力,hash 难度:0
- sgu 125 Shtirlits
- sgu114. Telecasting station 难度:1
- sgu 146. The Runner 取模技巧 难度:1
- SGU 219 Synchrograph tarjian找环,理解题意,图论 难度:3
- 快速切题 sgu115. Calendar 模拟 难度:0
- SGU 110. Dungeon 计算几何 难度:3
- SGU 132. Another Chocolate Maniac 状压dp 难度:1
- SGU 125 Shtirlits 搜索+可行性剪枝
- sgu101 欧拉路径 难度:1
- SGU 156 Strange Graph 欧拉回路,思路,汉密尔顿回路 难度:3