解9*9数独
#include<iostream>
#include<cstring>
#include<set>
using namespace std;
string mesh[10];
set<int>st[3][3];
bool flag=0;
void print(){
cout<<"------------"<<endl;
for(int i=0;i<=8;i++){
for(int j=0;j<=8;j++){
cout<<mesh[i][j];
if((j+1)%3==0)cout<<" ";
}
cout<<endl;
if((i+1)%3==0)cout<<endl;
}
}
void dfs(int x ,int y){
int i,j;
if(x>8){
flag=1;return;
}
if(!(mesh[x][y]>='1'&&mesh[x][y]<='9'))
for(i=1+'0';i<=9+'0';i++){
for(j=0;j<=8;j++){
if(mesh[x][j]==i||mesh[j][y]==i)break;
}
if(j==9){
if(st[x/3][y/3].find(i)==st[x/3][y/3].end()){
mesh[x][y]=i;
st[x/3][y/3].insert(i);
}
else continue;
//print();
dfs(x+1*((y+1)==9),(y+1)%9);
if(flag)return;
mesh[x][y]='*';
st[x/3][y/3].erase(i);
}
}
else {
dfs(x+1*((y+1)==9),(y+1)%9);
}
return;
}
int main(){
for(int i=0;i<=8;i++){
cin>>mesh[i];
}
for(int x=0;x<=8;x++)for(int y=0;y<=8;y++){
if(mesh[x][y]>='1'&&mesh[x][y]<='9'){
st[x/3][y/3].insert(int(mesh[x][y]));
// cout<<"xy:"<<x/3<<y/3<<" data:"<<mesh[x][y]<<endl;
}
}
dfs(0,0);
print();
}