您的位置:首页 > 其它

hdu1426&3111 数独问题

2016-01-29 18:41 585 查看
题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1426

http://acm.hdu.edu.cn/showproblem.php?pid=3111

这两题均可用dfs即可

line[i][j] 记录第i行j数字有没有出现

row[i][j] 记录第i列j数字有没有出现

sqr[b[i][j]][k] 记录第i行第j列在其3*3方块中有无出现k

代码1426:

#include<cstdio>
#include<cstring>
using namespace std;
const int N=20;
char map

;
int line

;int row

;
int sqr

;int b

;
int cnt,flag;
void init(){
memset(line,0,sizeof(line));
memset(row,0,sizeof(row));
memset(sqr,0,sizeof(sqr));
for(int i=1;i<10;i++)
for(int j=1;j<10;j++){
if(i<=3&&j<=3) b[i][j]=1;
else if(i<=3&&j>3&&j<=6) b[i][j]=2;
else if(i<=3&&j>6&&j<=9) b[i][j]=3;
else if(i>6&&j<=3) b[i][j]=7;
else if(i>6&&j>3&&j<=6) b[i][j]=8;
else if(i>6&&j>6&&j<=9) b[i][j]=9;
else if(j<=3) b[i][j]=4;
else if(j>3&&j<=6)b[i][j]=5;
else b[i][j]=6;
}
}

void dfs(int x,int y,int num)
{
if(num==cnt){
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++){
printf("%c%c",map[i][j],j==9?'\n':' ');
}
flag=1;
return ;
}
if(flag) return ;
if(y==10) x++,y=1;
if(map[x][y]=='?'){
for(int i=1;i<=9;i++){
if(!line[x][i]&&!row[y][i]&&!sqr[b[x][y]][i]){
line[x][i]=1,row[y][i]=1,sqr[b[x][y]][i]=1;
map[x][y]=i+'0';
dfs(x,y+1,num+1);
if(flag) return ;
line[x][i]=0,row[y][i]=0,sqr[b[x][y]][i]=0;
map[x][y]='?';
}
}
}
else dfs(x,y+1,num);
}

int main()
{

char s[2];
int ss=0;
while(scanf("%s",s)!=-1)
{
init();
cnt=0;flag=0;
map[1][1]=s[0];
if(map[1][1]!='?'){
int t=map[1][1]-'0';
line[1][t]=1;
row[1][t]=1;
sqr[b[1][1]][t]=1;
}
else cnt++;
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++){
if(i==1&&j==1){
continue;
}
scanf("%s",s);
map[i][j]=s[0];
if(map[i][j]!='?'){
int t=map[i][j]-'0';
line[i][t]=1;
row[j][t]=1;
sqr[b[i][j]][t]=1;
}
else cnt++;
}
if(ss++)
printf("\n");
dfs(1,1,0);
}
return 0;
}


代码3111:

#include<cstdio>
#include<cstring>
using namespace std;
const int N=20;
char map

;
int line

;int row

;
int sqr

;int b

;
int cnt,flag;

void init(){
memset(line,0,sizeof(line));
memset(row,0,sizeof(row));
memset(sqr,0,sizeof(sqr));
for(int i=1;i<10;i++)
for(int j=1;j<10;j++){
if(i<=3&&j<=3) b[i][j]=1;
else if(i<=3&&j>3&&j<=6) b[i][j]=2;
else if(i<=3&&j>6&&j<=9) b[i][j]=3;
else if(i>6&&j<=3) b[i][j]=7;
else if(i>6&&j>3&&j<=6) b[i][j]=8;
else if(i>6&&j>6&&j<=9) b[i][j]=9;
else if(j<=3) b[i][j]=4;
else if(j>3&&j<=6)b[i][j]=5;
else b[i][j]=6;
}
}

void dfs(int x,int y,int num)
{
if(num==cnt){
for(int i=1;i<=9;i++)
printf("%s\n",map[i]+1);
return ;
}
if(y==10) x++,y=1;
if(map[x][y]=='?'){
for(int i=1;i<=9;i++){
if(!line[x][i]&&!row[y][i]&&!sqr[b[x][y]][i]){
line[x][i]=1,row[y][i]=1,sqr[b[x][y]][i]=1;
map[x][y]=i+'0';
dfs(x,y+1,num+1);
line[x][i]=0,row[y][i]=0,sqr[b[x][y]][i]=0;
map[x][y]='?';
}
}
}
else dfs(x,y+1,num);
}

int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
cnt=0;flag=1;
for(int i=1;i<=9;i++)
{
scanf(" %s",map[i]+1);
for(int j=1;j<=9;j++){
if(map[i][j]!='?'){
int t=map[i][j]-'0';
if(line[i][t]==1) flag=0;
else line[i][t]=1;
if(row[j][t]==1) flag=0;
else row[j][t]=1;
if(sqr[b[i][j]][t]==1) flag=0;
else sqr[b[i][j]][t]=1;
}
else cnt++;
}
}
if(T) scanf("%s",map[10]+1);
if(flag){
dfs(1,1,0);
}
else printf("impossible\n");
if(T)printf("---\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs