有一个风靡世界的游戏,叫数独。
5????7??6
?6????5?4
?834?????
???182?4?
??1???9??
?7?369???
?????543?
1?5????9?
7??2????1
就是这个。
最low的算法。
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 10
#define INF 2147483647
using namespace std;
int g[N][N],cnt = 0;
bool row[N][N],col[N][N],check[N][N];
//行,列,9宫格。
int sub(int x,int y){
if (x <= 3){
if (y <= 3) return 1;
if (y <= 6) return 2;
if (y <= 9) return 3;
}
if (x <= 6){
if (y <= 3) return 4;
if (y <= 6) return 5;
if (y <= 9) return 6;
}
else {
if (y <= 3) return 7;
if (y <= 6) return 8;
if (y <= 9) return 9;
}
}
void init()
{
memset(g,0,sizeof g);
memset(row,true,sizeof row);
memset(col,true,sizeof col);
memset(check,true,sizeof check);
for (int i = 1;i <= 9;i++)
{
char st[N];
cin >> st;
for (int j = 0; j < 9; j++)
if (st[j] != '?')
{
cnt++;
g[i][j+1] = st[j] - '0';
row[i][g[i][j+1]] = false;
col[j+1][g[i][j+1]] = false;
check[sub(i,j+1)][g[i][j+1]] = false;
}
}
cnt = 81 - cnt;
}
void Print(){
for (int i=1;i<=9;i++){
for (int j=1;j<=9;j++) printf("%d",g[i][j]);
printf("\n");
}
}
void Sdk(int x,int y,int sum){
if (sum == cnt) {
Print();
return;
}
if (g[x][y]) {
if (y < 9) Sdk(x,y+1,sum);
else Sdk(x+1,1,sum);
}else
for (int k = 1;k <= 9;k++)
{
int num = sub(x,y);
if ( row[x][k] && col[y][k] && check[num][k])
{
g[x][y] = k;
row[x][k] = false;
col[y][k] = false;
check[num][k] = false;
if (y < 9) Sdk(x,y+1,sum+1);
else Sdk(x+1,1,sum+1);
g[x][y] = 0;
row[x][k] = true;
col[y][k] = true;
check[num][k] = true;
}
}
}
int main(){
init();
Sdk(1,1,0);
return 0;
}