描述
答案如下
输入
输入数据入如下:
#72##4###
####6#8##
#########
###3#9###
#1#######
68####7##
##4####2#
####8####
##3####49
输出
572834961
431967852
968521437
247319586
315678294
689245713
754193628
196482375
823756149
———————————————————————————————————————————代码:
#include<bits/stdc++.h>
using namespace std;
int a[10][10];
bool check[10];
bool row(int r)
{
memset(check,0,sizeof(check));
for(int i=0;i<9;i++)
{
if(check[a[r][i]])
return false;
check[a[r][i]]=1;
check[0]=0;
}
return true;
}
bool column(int c)
{
memset(check,0,sizeof(check));
for(int i=0;i<9;i++)
{
if(check[a[i][c]])
return false;
check[a[i][c]]=1;
check[0]=0;
}
return true;
}
bool square(int x,int y)
{
memset(check,0,sizeof(check));
int tmp=x/3;
x=tmp*3;
tmp=y/3;
y=tmp*3;
for(int i=x;i<x+3;i++)
{
for(int j=y;j<y+3;j++)
{
if(check[a[i][j]])
return false;
check[a[i][j]]=1;
check[0]=0;
}
}
return true;
}
void dfs(int x,int y)
{
if(x==9)
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++) cout<<a[i][j];
cout<<endl;
}
exit(0);
}
if(a[x][y]==0)
{
for(int i=1;i<=9;i++)
{
a[x][y]=i;
if(row(x)&&column(y)&&square(x,y))
{
if(y==8) dfs(x+1,0);
else dfs(x,y+1);
}
a[x][y]=0;
}
}
else
{
if(y==8) dfs(x+1,0);
else dfs(x,y+1);
}
}
int main()
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
char c;
cin>>c;
if(c!='#') a[i][j]=(int)c-48;
}
}
dfs(0,0);
return 0;
}