老六本六-徐梓恒 • 1年前
using namespace std;
int g[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}, }; bool row[N][N],col[N][N],gong[N][N]; int num[N][N]; void dfs(int x,int y){
if(x==10){
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++){
cout<<num[i][j]<<(j==9?"\n":" ");
}
}
exit(0);
}
if(!num[x][y]){
for(int i=1;i<=9;i++){
if(!row[x][i]&&!col[y][i]&&!gong[g[x][y]][i]){
row[x][i]=col[y][i]=gong[g[x][y]][i]=1;
num[x][y]=i;
if(y==9)dfs(x+1,1);
else dfs(x,y+1);
row[x][i]=col[y][i]=gong[g[x][y]][i]=0;
num[x][y]=0;
}
}
}
else{
if(y==9)dfs(x+1,1);
else dfs(x,y+1);
}
} int main (){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++){
cin>>num[i][j];
if(num[i][j]){
row[i][num[i][j]]=col[j][num[i][j]]=gong[g[i][j]][num[i][j]]=1;
}
}
}
dfs(1,1);
return 0;
}
评论:
请先登录,才能进行评论