Another Eight Puzzle
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).
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.
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”.
0 0
0 0
0 0
1 4 5 8 6 2
unique
answer
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
#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的更多相关文章
- Puzzle 面向服务/切面(AOP/IOC)开发框架 For .Net
Puzzle 面向服务/切面AOP开发框架 For .Net AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效 ...
- HDU5456 Matches Puzzle Game(DP)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5456 Description As an exciting puzzle game for ...
- 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 ...
- poj3678 Katu Puzzle 2-SAT
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6714 Accepted: 2472 Descr ...
- POJ1651Multiplication Puzzle[区间DP]
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8737 Accepted: ...
- codeforce B Island Puzzle
B. Island Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- poj 1651 Multiplication Puzzle (区间dp)
题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...
- Ignatius's puzzle
Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- A hard puzzle
A hard puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1097 A hard puzzle
Problem Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how ...
随机推荐
- Web 项目更改项目名
简单的记录web开发中基本的操作. 更改项目名 直接修改 找到原项目中的.project 文件,更改中项目名称.然后在同目录下找到.mymetadata 文件 并更改name.context-root ...
- Linux Ubuntu从零开始部署web环境及项目-----搭建ssh环境(一)
linux搭建ssh环境 1,用户登录 成功输入用户名和密码后 进入Ubuntu界面 2,配置网络 参考:http://blog.csdn.net/liu782726344/article/deta ...
- JS(二)
上周给大家介绍了一下JS基础中一点东西,今天给大家介绍一下JS基础中一个重要部分,循环和函数. 04-JS中的循环结构 一.[循环结构的步骤] 1.首先要先声明循环变量. 2.判断循环条件 3.执行循 ...
- Https系列之四:https的SSL证书在Android端基于okhttp,Retrofit的使用
Https系列会在下面几篇文章中分别作介绍: 一:https的简单介绍及SSL证书的生成二:https的SSL证书在服务器端的部署,基于tomcat,spring boot三:让服务器同时支持http ...
- 修改Form ->Top和Left 造成的莫名其妙的显示异常 “轴信息”
相关代码: 运行程序: 要等待很久,或者把主窗体最小化,再最大化打开"轴信息" 才会恢复正常. 这个"不爽"很蛋蛋 ,网友亲亲们,有独到见解的亲亲们,期待得到你 ...
- 【JVM】Java中的JavaCore/HeapDump文件及其分析方法
产生时间 Java程序运行时,有时会产生JavaCore及HeapDump文件,它一般发生于Java程序遇到致命问题的情况下. 有时致命问题发生后,Java应用不会死掉,还能继续运行: 但有时致命问题 ...
- 通过SQL脚本导入数据到不同数据库避免重复导入三种方式
前言 无论何种语言,一旦看见代码中有重复性的代码则想到封装来复用,在SQL同样如此,若我们没有界面来维护而且需要经常进行的操作,我们会写脚本避免下次又得重新写一遍,但是这其中就涉及到一个问题,这个问题 ...
- 如何修改select标签的默认下拉箭头样式?
对于一般的项目而言,select标签在浏览器中表现出来的默认样式也不算丑,但是一次项目中,项目经理却要求对select标签本身进行样式修改,美化其下拉小箭头的样式.我思考和尝试了许多方法,最终得到一种 ...
- Python 获取当前脚本文件路径目录
# -*- coding: cp936 -*- import sys,os # 获取脚本文件的当前路径 def cur_file_dir(): # 获取脚本路径 path = sys.path[0] ...
- 深入浅出 SpringMVC - 1
前言: 本篇笔记是LZ在之前学习 SpringMVC 框架时所的记录,分两篇分享,此篇为基础篇,包括 SpringMVC 环境在 Eclipse 中的搭建,SpringMVC 的 HelloWorld ...