98 lines
2.2 KiB
C++
98 lines
2.2 KiB
C++
#include<bits/stdc++.h>
|
|
using namespace std;
|
|
|
|
int m[10][10];
|
|
bool h_is_used[10][10],l_is_used[10][10],t_is_used[5][5][10];
|
|
|
|
#ifdef OI
|
|
#define DB(code){code}
|
|
#define PV(v){cout<<#v<<" : "<<(v)<<endl;}
|
|
#else
|
|
#define DB(code)
|
|
#define PV(v)
|
|
#endif
|
|
|
|
void dfs(int x,int y);
|
|
optional<pair<int,int>> find_next(int x,int y);
|
|
|
|
int main(){
|
|
for(int i=1;i<=9;i++){
|
|
string str;
|
|
getline(cin,str);
|
|
for(int j=1;j<=9;j++){
|
|
if(str[j-1]=='.'){
|
|
m[i][j]=-1;
|
|
}else{
|
|
m[i][j]=str[j-1]-'0';
|
|
h_is_used[i][m[i][j]]=true;
|
|
l_is_used[j][m[i][j]]=true;
|
|
t_is_used[i/3+1][j/3+1][m[i][j]]=true;
|
|
}
|
|
}
|
|
}
|
|
DB(
|
|
for(int i=1;i<=9;i++){
|
|
for(int j=1;j<=9;j++){
|
|
cout<<m[i][j]<<"\t";
|
|
}
|
|
cout<<endl;
|
|
}
|
|
)
|
|
auto find = find_next(1, 1);
|
|
if(find.has_value()==false){
|
|
for(int i=1;i<=9;i++){
|
|
for(int j=1;j<=9;j++){
|
|
cout<<m[i][j];
|
|
}
|
|
cout<<endl;
|
|
}
|
|
exit(0);
|
|
}
|
|
dfs(find->first, find->second);
|
|
}
|
|
|
|
optional<pair<int,int>> find_next(int x,int y){
|
|
for (int j=y+1; j<=9; j++) {
|
|
if(m[x][j]==-1){
|
|
return make_optional<pair<int,int>>({x,j});
|
|
}
|
|
}
|
|
for(int i=x+1;i<=9;i++){
|
|
for(int j=1;j<=9;j++){
|
|
if(m[i][j]==-1){
|
|
return make_optional<pair<int,int>>({i,j});
|
|
}
|
|
}
|
|
}
|
|
return nullopt;
|
|
}
|
|
|
|
void dfs(int x,int y){
|
|
DB(PV(x)PV(y)cout<<endl;)
|
|
auto find = find_next(x, y);
|
|
if(find.has_value()==false){
|
|
for(int i=1;i<=9;i++){
|
|
for(int j=1;j<=9;j++){
|
|
cout<<m[i][j];
|
|
}
|
|
cout<<endl;
|
|
}
|
|
exit(0);
|
|
}
|
|
for(int i=1;i<=9;i++){
|
|
if(h_is_used[x][i]||l_is_used[y][i]||t_is_used[x/3+1][y/3+1][i]){
|
|
continue;
|
|
}
|
|
h_is_used[x][i]=true,
|
|
l_is_used[y][i]=true,
|
|
t_is_used[x/3+1][y/3+1][i]=true;
|
|
m[x][y]=i;
|
|
|
|
dfs(find->first, find->second);
|
|
|
|
h_is_used[x][i]=false;
|
|
l_is_used[y][i]=false;
|
|
t_is_used[x/3+1][y/3+1][i]=false;
|
|
m[x][y]=-1;
|
|
}
|
|
} |