Problem Description
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

Eight Puzzle" title="Another Eight Puzzle">


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).
Input
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.


Output
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”.
Sample Input
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
Sample Output
Case 1: 7 3
1 4 5 8 6 2
Case 2: Not
unique
Case 3: No
answer
题意:如图所示,有A-H 8个圆圈,关系如图所示,给出各个圆圈内的数,0代表你要填的数,让你求要多少种填法;
解题思路:刚看到时,以为和相邻两个数的和是素数那个差不多,后来写的时候,有点不一样那个,从第一位开始搜索,位上有数字就跳过,如果没有就搜索:
A-B , A-C,
A-D

   B-A, B-C,
B-E, B-F

   C-A, C-B, C-D, C-F,
C-E,C-G

   D-A, D-C, D-F, D-G

   E-B, E-C, E-F, E-H

   F-C, F-D, F-G, F-H, F-E,
F-B

   G-D, G-C, G-F, G-H

   H-E, H-F, H-G

   A=1 B=2 C=3 D=4 E=5 F=6 G=7
H=8
每到一位的判断关系;
感悟:判断条件老是写不对,真是费劲啊
代码:
#include

#include

#include

#include

#include

#define maxn 10

using namespace std;

int pos[maxn],t,visit[maxn],ans=0,mark[maxn],p[maxn];



bool conect(int x,int y)

{

    if (x>y)
{int t=x; x=y; y=t;}

    switch
(x)

    {

       
case 1:return y==2 || y==3 || y==4;

       
case 2:return y==3 || y==5 || y==6;

       
case 3:return y==4 || y==5 || y==6 || y==7;

       
case 4:return y==6 || y==7;

       
case 5:return y==6 || y==8;

       
case 6:return y==7 || y==8;

       
case 7:return y==8;

    }

}

bool ok(int p)

{

    int i;

    for
(i=1;i<=8;i++)

       
if ((conect(i,p) && abs(pos[p]-pos[i])==1) &&
pos[i]!=0) return 0;

    return
1;

}



void dfs(int cur)

{

   
//cout<<cur<<" ";

   
if(ans>1)

       
return ;//剪枝大于2了就不需要在搜了

   
//cout<<"cur="<<cur<<endl;

   
while(cur<=8&&pos[cur]) cur++;//把0空过去

   
if(cur>8)

    {

       
ans++;

       
if(ans==1)//因为只有ans=1时才需要输出

           
memcpy(p,pos,sizeof(pos));//将整个数组转移过来

       
return;

    }

    for(int
i=1;i<=8;i++) if(!visit[i])

    {

       
//cout<<cur<<endl;

       
//cout<<i<<" ";

       
visit[i]=1;

       
pos[cur]=i;

       
if(ok(cur))//相邻的不是相邻的数

           
dfs(cur+1);

       
pos[cur]=0;//原来这里就是零的

       
visit[i]=0;//释放标记,用于下一次搜索;

       
if(ans>1)

           
return;

    }

}

int main()

{

   
//freopen("in.txt", "r", stdin);

    int
flag=0;

   
scanf("%d",&t);

    for(int
i=1;i<=t;i++)

    {

       
ans=0;

       
memset(pos,0,sizeof(pos));

       
memset(visit,0,sizeof(visit));

       
for(int j=1;j<=8;j++)

       
{

           
scanf("%d",&pos[j]);

           
visit[pos[j]]=1;

       
}

       
dfs(1);

       
//cout<<"ans="<<ans<<endl;

       
printf("Case %d: ",i);

       
if(!ans)

           
printf("No answer");

       
else if(ans==1)

           
for(int i=1;i<=8;i++)

               
printf(i==1?"%d":" %d",p[i]);

       
else

           
printf("Not unique");

       
printf("\n");

    }

}

Another Eight Puzzle的更多相关文章

  1. Puzzle 面向服务/切面(AOP/IOC)开发框架 For .Net

    Puzzle 面向服务/切面AOP开发框架 For .Net AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效 ...

  2. HDU5456 Matches Puzzle Game(DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5456 Description As an exciting puzzle game for ...

  3. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

  4. poj3678 Katu Puzzle 2-SAT

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6714   Accepted: 2472 Descr ...

  5. POJ1651Multiplication Puzzle[区间DP]

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8737   Accepted:  ...

  6. codeforce B Island Puzzle

    B. Island Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

  8. Ignatius's puzzle

    Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. A hard puzzle

    A hard puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  10. hdu 1097 A hard puzzle

    Problem Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how ...

随机推荐

  1. 基于 Electron 的爬虫框架 Nightmare

    作者:William 本文为原创文章,转载请注明作者及出处 Electron 可以让你使用纯 JavaScript 调用 Chrome 丰富的原生的接口来创造桌面应用.你可以把它看作一个专注于桌面应用 ...

  2. AngularJS–Scope(作用域)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ Scope Scope 是一个应用程序的模块的对象.它是表达式的执行上下文.它充斥在DO ...

  3. 接口测试——httpclient介绍与请求方式详解

    httpClient工具介绍 HTTP协议可能是现在lntemet上使用得最多.最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源.虽然在JDK的java.net包中已经 ...

  4. 【小程序】微信小程序实现各种特效实例

    写在前面 最近在负责一个微信小程序的前端以及前后端接口的对接的项目,整体上所有页面的布局我都已经搭建完成,里面有一些常用的特效,总结一下,希望对大家和我都能有所帮助 实例1:滚动tab选项卡 先看一下 ...

  5. Qt中的坐标系统

    Qt使用统一的坐标系统来定位窗口部件的位置和大小. 以屏幕的左上角为原点即(0, 0)点,从左向右为x轴正向,从上向下为y轴正向,这整个屏幕的坐标系统就用来定位顶层窗口: 此外,窗口内部也有自己的坐标 ...

  6. 用mp3stego来加密与解密文件的几次尝试

    用法来自实验吧的"Canon"隐写题目的灵感. 先来简单的聊一下这道题目,打开题目后发现了一个mp3文件,除此之外还有一枚压缩包.然而压缩包是加密的,看来我们需要通过解出来mp3里 ...

  7. 【BZOJ】 2463 [中山市选2009]谁能赢呢?(博弈论)

    Description   小明和小红经常玩一个博弈游戏.给定一个n×n的棋盘,一个石头被放在棋盘的左上角.他们轮流移动石头.每一回合,选手只能把石头向上,下,左,右四个方向移动一格,并且要求移动到的 ...

  8. Java面向对象 GUI 补录

     Java面向对象 GUI 补录 知识概要:(1)GUI和CLI                   (2)AWT和SWING                   (3)AWT继承关系图      ...

  9. JavaScript设计模式--简单工厂模式

    一,介绍 工厂模式创建对象(视为工厂里的产品)时无需指定创建对象的具体类. 工厂模式定义一个用于创建对象的接口,这个接口由子类决定实例化哪一个类.该模式使一个类的实例化延迟到了子类.而子类可以重写接口 ...

  10. JavaScript实现常见算法面试题

    算法题目部分参照了<进军硅谷>这本书. github:https://github.com/qcer/Algo-Practice (如果你觉得有帮助,记得给个star,THS) 一.排序 ...