-
Notifications
You must be signed in to change notification settings - Fork 0
/
p1784.cpp
75 lines (70 loc) · 1.65 KB
/
p1784.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<bits/stdc++.h>
using namespace std;
int a[15][15];
int p[11][11],l[11][11],box[11][11];
void dfs(int x,int y)
{
if(x==10&&y==1)
{
for(int i=1;i<=9;++i)
{
for(int j=1;j<=9;++j) printf("%d ",a[i][j]);
cout<<endl;
}
exit(0);
}
if(a[x][y]!=0)
{
if(y==9) dfs(x+1,1);
else dfs(x,y+1);
}
else
{
// int tmp[10];
// memset(tmp,0,sizeof(tmp));
// for(int i=1;i<=9;++i)
// {
// tmp[a[x][i]]=1;
// tmp[a[i][y]]=1;
// }
// int m=x-1-(x-1)%3,n=y-1-(y-1)%3;
// for(int i=m+1;i<=m+3;++i)
// {
// for(int j=n+1;j<=n+3;++j)
// {
// tmp[a[i][j]]=1;
// }
// }
for(int i=1;i<=9;++i)
{
if((p[x][i]==0)&&(l[y][i]==0)&&(box[(x-1)/3*3+(y-1)/3+1][i]==0))//tmp[i]==0
{
a[x][y]=i;
p[x][i]=l[y][i]=box[(x-1)/3*3+(y-1)/3+1][i]=1;
if(y==9) dfs(x+1,1);
else dfs(x,y+1);
a[x][y]=0;
p[x][i]=l[y][i]=box[(x-1)/3*3+(y-1)/3+1][i]=0;
}
}
}
}
int main()
{
memset(a,0,sizeof(a));
memset(p,0,sizeof(p));
memset(l,0,sizeof(l));
memset(box,0,sizeof(box));
int ip;
for(int i=1;i<=9;++i)
{
for(int j=1;j<=9;++j)
{
scanf("%d",&ip);
a[i][j]=ip;
if(ip!=0) p[i][ip]=l[j][ip]=box[(i-1)/3*3+(j-1)/3+1][ip]=1;
}
}
dfs(1,1);
return 0;
}