sgu 125 Shtirlits
来源:互联网 发布:java安装方法 编辑:程序博客网 时间:2024/05/16 01:25
题目描述:
125. Shtirlits
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
There is a checkered field of sizeN 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 numberN. 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 containN 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
31 2 11 2 11 1 0
Sample Output
1 2 31 4 51 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;}
- SGU 125 Shtirlits
- sgu 125 Shtirlits
- Shtirlits - SGU 125 dfs
- SGU 125 Shtirlits(dfs)
- [SGU]125. Shtirlits
- SGU 125. Shtirlits 剪枝技巧
- SGU 125. Shtirlits DFS暴搜
- SGU 125. Shtirlits(dfs)
- sgu125:Shtirlits
- SGU125 Shtirlits
- sgu 125
- sgu 125解题记录
- SGU
- SGU
- SGU
- SGU
- SGU
- SGU
- C_C++ 各种计时函数
- Linux下ARM汇编语法
- hibernate.dialect' must be set when no Connection available错误
- Unable to get the default Bean Validation factory
- 数据结构学习之启发式搜索求解骑士周游问题
- sgu 125 Shtirlits
- oracle 11g 学习笔记 10_31(2)_维护数据的完整性
- CSS选择器的浏览器支持大全
- OpenGL 4.3 (Core Profile) - August 6, 2012 spec Fundamental 2.5 Objects and the Object Model
- 数据结构学习之哈弗曼树的实现
- sgu 126 Boxes
- iphone ipa图片资源提取
- oracle10g更改字符集WE8ISO8859P1为ZHS16GBK
- iOS:应用程序的线程安全性