您的位置:首页 > 其它

HDU2514 Another Eight Puzzle

2013-07-16 18:09 218 查看

                                           Another Eight Puzzle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 716    Accepted Submission(s): 442


[align=left]Problem Description[/align]
Fill the following 8 circles with digits 1~8,with each number exactly once . Conntcted circles cannot be filled with two consecutive numbers.

There are 17 pairs of connected cicles:

A-B , A-C, A-D

B-C, B-E, B-F

C-D, C-E, C-F, C-G

D-F, D-G

E-F, E-H

F-G, F-H

G-H



Filling G with 1 and D with 2 (or G with 2 and D with 1) is illegal since G and D are connected and 1 and 2 are consecutive .However ,filling A with 8 and B with 1 is legal since 8 and 1 are not consecutive .

In this problems,some circles are already filled,your tast is to fill the remaining circles to obtain a solution (if possivle).

 

[align=left]Input[/align]
The first line contains a single integer T(1≤T≤10),the number of test cases. Each test case is a single line containing 8 integers 0~8,the numbers in circle A~H.0 indicates an empty circle.

 

[align=left]Output[/align]
For each test case ,print the case number and the solution in the same format as the input . if there is no solution ,print “No answer”.If there more than one solution,print “Not unique”.

 

[align=left]Sample Input[/align]

3
7 3 1 4 5 8 0 0
7 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0

 

[align=left]Sample Output[/align]

Case 1: 7 3 1 4 5 8 6 2
Case 2: Not unique
Case 3: No answer

 

[align=left]Source[/align]
ECJTU 2008 Autumn Contest
 

[align=left]Recommend[/align]
lcy

首道自过dfs题,必须纪念,折腾了整整一下午才出来的,好有成就感啊有木有。先贴个AC代码,中间有些注释是做题时利于自己理解而码的,亲们可以模仿。由于第一次写,比较拙劣,亲们不要见怪啊!

解题思路:本题用DFS解答,是比较经典的DFS题目,其原型可见http://acm.hdu.edu.cn/showproblem.php?pid=1016,该题由原题稍作改编而来,有异曲同工之妙。

解题数据结构:

呵呵!其他的,祥见注释,祝亲们刷题愉快!



先贴个简洁的代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int map[9][9];
int x[9],y[9];
int sum;
int temp[9];
void dfs(int t)
{
int i,j;
if(t==9)    //    如果到最后,符合要求,记录结果
{
sum++;
if(sum==1)
for(i=0;i<=8;i++)
temp[i]=x[i];
}
else if(x[t])   //该位置已赋值,跳过
dfs(t+1);
else    //将i赋给x[t]-‘A+t-1’
{
for(i=1;i<=8;i++) //每次都从头到尾搜索一遍,判断有没有符合要求的
{
if(!y[i])       //i未使用
{
for(j=1;j<=8;j++)   //冲突判断
if((map[t][j]||map[j][t])&&x[j]&&abs(i-x[j])==1)
{
break;
}
if(j==9)    //无冲突
{
y[i]=1;    //已使用,标记
x[t]=i;
dfs(t+1);
y[i]=0;    //返回时,撤销递归标志
x[t]=0;
}
}
}
}
}
int main()
{
int case1=1;
memset(map,0,sizeof(map));
map[1][2]=map[1][3]=map[1][4]=1;
map[2][3]=map[2][5]=map[2][6]=1;
map[3][4]=map[3][5]=map[3][6]=map[3][7]=1;
map[4][6]=map[4][7]=1;
map[5][6]=map[5][8]=1;
map[6][7]=map[6][8]=1;
map[7][8]=1;
int n,i;
scanf("%d",&n);
while(n--)
{
memset(y,0,sizeof(y));
for(i=1;i<=8;i++)
{
scanf("%d",&x[i]),y[x[i]]=1;
}
printf("Case %d: ",case1++);
sum=0;
dfs(1);
if(sum==1)
{
printf("%d",temp[1]);
for(i=2;i<=8;i++)
printf(" %d",temp[i]);
}
else if(sum==0)
printf("No answer");
else
printf("Not unique");
printf("\n");
}
return 0;
}


这个是原码,原汁原味,青春的纪念

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int map[9][9];
int x[9],y[9];
int sum;
int temp[9];
void dfs(int t)
{
// printf("\n\n\t\tt=%d-------",t);
int i,j;
if(t==9)    //    如果到最后,符合要求,记录结果
{
//printf(" 如果到最后,符合要求,记录结果\n");
sum++;
if(sum==1)
for(i=0;i<=8;i++)
temp[i]=x[i];
// printf("%d",x[1]);  //输出测试
// for(i=2;i<=8;i++)
//printf(" %d",x[i]);
//printf("\n");
}
else if(x[t])   //该位置已赋值,跳过
dfs(t+1);//printf("////该位置已赋值,跳过\n"),
else    //将i赋给x[t]-‘A+t-1’
{
//printf("//将i赋给x[t]-‘A+t-1’  t=%d\n",i,t);
for(i=1;i<=8;i++) //每次都从头到尾搜索一遍,判断有没有符合要求的
{
//printf("---------i=%d\n",i);
if(!y[i])       //i未使用
{
// printf(" //i未使用\n");
for(j=1;j<=8;j++)   //冲突判断
if((map[t][j]||map[j][t])&&x[j]&&abs(i-x[j])==1)
{
// printf("与x[j]=%d冲突\n",x[j]);
break;
}
if(j==9)    //无冲突
{
//printf("//无冲突\n");
y[i]=1;    //已使用,标记
x[t]=i;
dfs(t+1);
y[i]=0;    //返回时,撤销递归标志
x[t]=0;
}
}
}
}
}
int main()
{
int case1=1;
memset(map,0,sizeof(map));
map[1][2]=map[1][3]=map[1][4]=1;
map[2][3]=map[2][5]=map[2][6]=1;
map[3][4]=map[3][5]=map[3][6]=map[3][7]=1;
map[4][6]=map[4][7]=1;
map[5][6]=map[5][8]=1;
map[6][7]=map[6][8]=1;
map[7][8]=1;
int n,i;
scanf("%d",&n);
while(n--)
{
memset(y,0,sizeof(y));
for(i=1;i<=8;i++)
{
scanf("%d",&x[i]),y[x[i]]=1;
}
printf("Case %d: ",case1++);
sum=0;
dfs(1);
//printf("\n-------\n");
if(sum==1)
{
printf("%d",temp[1]);
for(i=2;i<=8;i++)
printf(" %d",temp[i]);
}
else if(sum==0)
printf("No answer");
else
printf("Not unique");
printf("\n");
}
return 0;
}


再贴个不AC的,测试案例直接错了,但我觉得我的方法值得分享,所以贴上来,亲们不用研究哦!我已经发现问题了,就是,中间我有用到X[i]判断,但递归回来时,值没有复原,多个抹除痕迹的地方就可以了啊,详见AC代码!

测试代码:通过2个案例
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int map[9][9];
int x[9],y[9];
int sum;
int temp[9];
void dfs(int t)
{
printf("\n\n\t\tt=%d-------",t);
int i,j;
if(t==9)    //    如果到最后,符合要求,记录结果
{
printf(" 如果到最后,符合要求,记录结果\n");
sum++;
if(sum==1)
for(i=0;i<=8;i++)
temp[i]=x[i];
printf("%d",x[1]);  //输出测试
for(i=2;i<=8;i++)
printf(" %d",x[i]);
printf("\n");
}
else if(x[t])   //该位置已赋值,跳过
printf("////该位置已赋值,跳过\n"),dfs(t+1);
else    //将i赋给x[t]-‘A+t-1’
{
printf("//将i赋给x[t]-‘A+t-1’  t=%d\n",i,t);
for(i=1;i<=8;i++) //每次都从头到尾搜索一遍,判断有没有符合要求的
{
printf("---------i=%d\n",i);
if(!y[i])       //i未使用
{
printf(" //i未使用\n");
for(j=1;j<=8;j++)   //冲突判断
if((map[t][j]||map[j][t])&&x[j]&&abs(i-x[j])==1)
{
printf("与x[j]=%d冲突\n",x[j]);
break;
}
if(j==9)    //无冲突
{
printf("//无冲突\n");
y[i]=1;    //已使用,标记
x[t]=i;
dfs(t+1);
//y[i]=0;    //返回时,撤销递归标志
}
}
}
}
}
int main()
{
int case1=1;
memset(map,0,sizeof(map));
map[1][2]=map[1][3]=map[1][4]=1;
map[2][3]=map[2][5]=map[2][6]=1;
map[3][4]=map[3][5]=map[3][6]=map[3][7]=1;
map[4][6]=map[4][7]=1;
map[5][6]=map[5][8]=1;
map[6][7]=map[6][8]=1;
map[7][8]=1;
int n,i;
scanf("%d",&n);
while(n--)
{
memset(y,0,sizeof(y));
for(i=1;i<=8;i++)
{
scanf("%d",&x[i]),y[x[i]]=1;
}
printf("Case %d: \n",case1++);
sum=0;
dfs(1);
printf("\n-------\n");
/*if(sum==1)
{
printf("%d",temp[1]);
for(i=2;i<=8;i++)
printf(" %d",temp[i]);
}
else if(sum==0)
printf("No answer");
else
printf("Not unique");
printf("\n");*/
printf("案例结束,sum=%d\n",sum);
}
return 0;
}

测试结果:
3
7 3 1 4 5 8 0 0
Case 1:

t=1-------////该位置已赋值,跳过

t=2-------////该位置已赋值,跳过

t=3-------////该位置已赋值,跳过

t=4-------////该位置已赋值,跳过

t=5-------////该位置已赋值,跳过

t=6-------////该位置已赋值,跳过

t=7-------//将i赋给x[t]-‘A+t-1’  t=2009095316
---------i=1
---------i=2
//i未使用
与x[j]=1冲突
---------i=3
---------i=4
---------i=5
---------i=6
//i未使用
//无冲突

t=8-------//将i赋给x[t]-‘A+t-1’  t=2009095316
---------i=1
---------i=2
//i未使用
//无冲突

t=9------- 如果到最后,符合要求,记录结果
7 3 1 4 5 8 6 2
---------i=3
---------i=4
---------i=5
---------i=6
---------i=7
---------i=8
---------i=7
---------i=8

-------
案例结束,sum=1
7 0 0 0 0 0 0 0
Case 2:

t=1-------////该位置已赋值,跳过

t=2-------//将i赋给x[t]-‘A+t-1’  t=2009095316
---------i=1
//i未使用
//无冲突

t=3-------//将i赋给x[t]-‘A+t-1’  t=2009095316
---------i=1
---------i=2
//i未使用
与x[j]=1冲突
---------i=3
//i未使用
//无冲突

t=4-------//将i赋给x[t]-‘A+t-1’  t=2009095316
---------i=1
---------i=2
//i未使用
与x[j]=3冲突
---------i=3
---------i=4
//i未使用
与x[j]=3冲突
---------i=5
//i未使用
//无冲突

t=5-------//将i赋给x[t]-‘A+t-1’  t=2009095316
---------i=1
---------i=2
//i未使用
与x[j]=1冲突
---------i=3
---------i=4
//i未使用
与x[j]=3冲突
---------i=5
---------i=6
//i未使用
//无冲突

t=6-------//将i赋给x[t]-‘A+t-1’  t=2009095316
---------i=1
---------i=2
//i未使用
与x[j]=1冲突
---------i=3
---------i=4
//i未使用
与x[j]=3冲突
---------i=5
---------i=6
---------i=7
---------i=8
//i未使用
//无冲突

t=7-------//将i赋给x[t]-‘A+t-1’  t=2009095316
---------i=1
---------i=2
//i未使用
与x[j]=3冲突
---------i=3
---------i=4
//i未使用
与x[j]=3冲突
---------i=5
---------i=6
---------i=7
---------i=8
---------i=7
---------i=8
---------i=6
---------i=7
---------i=8
---------i=4
//i未使用
与x[j]=5冲突
---------i=5
---------i=6
---------i=7
---------i=8
---------i=2
//i未使用
与x[j]=3冲突
---------i=3
---------i=4
//i未使用
与x[j]=3冲突
---------i=5
---------i=6
---------i=7
---------i=8

-------
案例结束,sum=0
1 0 0 0 0 0 0 0
Case 3:

t=1-------////该位置已赋值,跳过

t=2-------//将i赋给x[t]-‘A+t-1’  t=2009095316
---------i=1
---------i=2
//i未使用
与x[j]=1冲突
---------i=3
//i未使用
//无冲突

t=3-------//将i赋给x[t]-‘A+t-1’  t=2009095316
---------i=1
---------i=2
//i未使用
与x[j]=1冲突
---------i=3
---------i=4
//i未使用
与x[j]=3冲突
---------i=5
//i未使用
//无冲突

t=4-------//将i赋给x[t]-‘A+t-1’  t=2009095316
---------i=1
---------i=2
//i未使用
与x[j]=1冲突
---------i=3
---------i=4
//i未使用
与x[j]=5冲突
---------i=5
---------i=6
//i未使用
与x[j]=5冲突
---------i=7
//i未使用
//无冲突

t=5-------//将i赋给x[t]-‘A+t-1’  t=2009095316
---------i=1
---------i=2
//i未使用
与x[j]=3冲突
---------i=3
---------i=4
//i未使用
与x[j]=3冲突
---------i=5
---------i=6
//i未使用
与x[j]=5冲突
---------i=7
---------i=8
//i未使用
//无冲突

t=6-------//将i赋给x[t]-‘A+t-1’  t=2009095316
---------i=1
---------i=2
//i未使用
与x[j]=3冲突
---------i=3
---------i=4
//i未使用
与x[j]=3冲突
---------i=5
---------i=6
//i未使用
与x[j]=5冲突
---------i=7
---------i=8
---------i=8
---------i=6
//i未使用
与x[j]=7冲突
---------i=7
---------i=8
---------i=4
//i未使用
与x[j]=5冲突
---------i=5
---------i=6
//i未使用
与x[j]=5冲突
---------i=7
---------i=8

-------
案例结束,sum=0

Process returned 0 (0x0)   execution time : 135.859 s
Press any key to continue.
问题:递归回溯失败
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: