Description
Input
输入一共15行,包含一个新数独的实例。第奇数行包含左右方向的符号(<和>),第偶数行包含上下方向的符号(^和v)。
Output
输出包含9行,每行9个1~9的数字,以单个空格隔开。输入保证解惟一。
Sample Input
< > > < > <
v v ^ ^ v v ^ ^ ^
< < > < > <
^ ^ ^ v ^ ^ ^ v v
< < < < > >
> < > > > >
v ^ ^ ^ ^ v v v ^
> > > > < >
v v ^ v ^ v ^ v ^
> < < > > >
< < < < > <
v ^ v v v v ^ ^ v
< > > < < >
^ v v v ^ v ^ v v
< > < > < >
v v ^ ^ v v ^ ^ ^
< < > < > <
^ ^ ^ v ^ ^ ^ v v
< < < < > >
> < > > > >
v ^ ^ ^ ^ v v v ^
> > > > < >
v v ^ v ^ v ^ v ^
> < < > > >
< < < < > <
v ^ v v v v ^ ^ v
< > > < < >
^ v v v ^ v ^ v v
< > < > < >
Sample Output
4 9 1 7 3 6 5 2 8
2 3 7 8 1 5 6 4 9
5 6 8 2 4 9 7 3 1
9 1 3 6 5 4 8 7 2
8 5 4 9 7 2 1 6 3
7 2 6 3 8 1 9 5 4
3 4 9 5 6 8 2 1 7
1 8 5 4 2 7 3 9 6
6 7 2 1 9 3 4 8 5
比靶形数独那个题水多了= =……
#include<iostream> #include<cstring> #include<cstdio> #define id(x,y) (x-1)*9+y using namespace std; int Relation[101][101],ans[12][12],maxn; int dx[4]= {0,0,-1},dy[4]= {0,-1,0}; bool row[10][10],column[10][10],square[10][10][10],flag; char st[101]; void Dfs(int x,int y) { maxn=max(x,maxn); if (x==10 && y==1) { flag=true; for (int i=1; i<=9; ++i) { for (int j=1; j<=8; ++j) printf("%d ",ans[i][j]); printf("%d\n",ans[i][9]); } return; } int down=1,up=9; for (int i=1; i<=2; ++i) { int xx=x+dx[i],yy=y+dy[i]; if (xx<1 || xx>9 || yy<1 || yy>9 || ans[xx][yy]==0) continue; if ( Relation[id(x,y)][id(xx,yy)] == 1 ) down=max(down,ans[xx][yy]+1); if ( Relation[id(x,y)][id(xx,yy)] == 0 ) up=min(up,ans[xx][yy]-1); } for (int i=down; i<=up; ++i) if (!row[x][i] && !column[y][i] && !square[(x-1)/3][(y-1)/3][i]) { ans[x][y]=i; row[x][i]=column[y][i]=square[(x-1)/3][(y-1)/3][i]=true; if (y==9) Dfs(x+1,1); else Dfs(x,y+1); ans[x][y]=0; row[x][i]=column[y][i]=square[(x-1)/3][(y-1)/3][i]=false; if (flag) return; } } int main() { memset(Relation,-1,sizeof(Relation)); for (int i=1; i<=15; ++i) if (i%2==(1^(i>=6 && i<=10))) { for (int j=1; j<=3; ++j) for (int k=1; k<=2; ++k) { char opt=getchar(); while (opt!=‘>‘ && opt!=‘<‘) opt=getchar(); if (opt==‘>‘) { Relation[(j-1)*3+k+(i/2+(i>10))*9][(j-1)*3+k+(i/2+(i>10))*9+1]=1; Relation[(j-1)*3+k+(i/2+(i>10))*9+1][(j-1)*3+k+(i/2+(i>10))*9]=0; } else { Relation[(j-1)*3+k+(i/2+(i>10))*9+1][(j-1)*3+k+(i/2+(i>10))*9]=1; Relation[(j-1)*3+k+(i/2+(i>10))*9][(j-1)*3+k+(i/2+(i>10))*9+1]=0; } } } else { for (int j=1; j<=9; ++j) { char opt=getchar(); while (opt!=‘^‘ && opt!=‘v‘) opt=getchar(); if (opt==‘v‘) { Relation[id(i/2+(i>=5),j)][id(i/2+(i>=5)+1,j)]=1; Relation[id(i/2+(i>=5)+1,j)][id(i/2+(i>=5),j)]=0; } else { Relation[id(i/2+(i>=5)+1,j)][id(i/2+(i>=5),j)]=1; Relation[id(i/2+(i>=5),j)][id(i/2+(i>=5)+1,j)]=0; } } } Dfs(1,1); }