There is nothing to say about searching. Pay attention to reading.
Code:
#include<cstdio>#include<cstdlib>using namespace std;const int num[10][10]={{0,0,0,0,0,0,0,0,0,0},{0,1,1,1,2,2,2,3,3,3},{0,1,1,1,2,2,2,3,3,3},{0,1,1,1,2,2,2,3,3,3},{0,4,4,4,5,5,5,6,6,6},{0,4,4,4,5,5,5,6,6,6},{0,4,4,4,5,5,5,6,6,6},{0,7,7,7,8,8,8,9,9,9},{0,7,7,7,8,8,8,9,9,9},{0,7,7,7,8,8,8,9,9,9}};int map[10][10][10][10],vish[10][10],visl[10][10],visn[10][10],chess[10][10],h=1,l=2;char s[5];void Read(){ for(int k=1;k<=3;k++) { for(int j=1;j<=2;j++) { for(int i=1;i<=3;i++) { scanf("%s",s); map[h][l][h][l-1]= ( s[0]==‘>‘ ? -1 : 1 ); scanf("%s",s); map[h][l+1][h][l]= ( s[0]==‘<‘ ? 1 : -1 ); l+=3; } for(int i=1;i<=9;i++) { scanf("%s",s); map[h+1][i][h][i]= ( s[0]==‘v‘ ? -1 : 1 ); } h++;l=2; } for(int i=1;i<=3;i++) { scanf("%s",s); map[h][l][h][l-1]= ( s[0]==‘>‘ ? -1 : 1 ); scanf("%s",s); map[h][l+1][h][l]= ( s[0]==‘<‘ ? 1 : -1 ); l+=3; } h++;l=2; }}void print(){ for(int i=1;i<=9;i++) { for(int j=1;j<9;j++) printf("%d ",chess[i][j]); printf("%d\n",chess[i][9]); }}bool check(const int &X,const int &Y,const int &Val){ if(map[X][Y][X][Y-1]==1) if(Val<=chess[X][Y-1])return false; if(map[X][Y][X][Y-1]==-1) if(Val>=chess[X][Y-1])return false; if(map[X][Y][X-1][Y]==1) if(Val<=chess[X-1][Y])return false; if(map[X][Y][X-1][Y]==-1) if(Val>=chess[X-1][Y])return false; return true;}void dfs(int x,int y){ if(x==10) { print(); exit(0); } for(int i=1;i<=9;i++) if(!vish[x][i]&&!visl[y][i]&&!visn[num[x][y]][i]) if(check(x,y,i)) { chess[x][y]=i; vish[x][i]=visl[y][i]=visn[num[x][y]][i]=true; if(y==9) dfs(x+1,1); else dfs(x,y+1); vish[x][i]=visl[y][i]=visn[num[x][y]][i]=false; }}int main(){ Read(); dfs(1,1); return 0;}
[Search] bzoj3109 [cqoi2013] New Sudoku