题目链接

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

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
 
题解:类似数独问题,将8个数字填入图中,连线两端的数字不能连续,因为线有17条,所以check函数写得非常长,其他的跟一般的DFS题目没有大区别,就是其他的题目只需恢复标记数组,而这个题目还需要恢复填写数字的数组。
 
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
//#define LOCAL
int sign[];
int mp[];
int ans,res[];
bool check(int n)
{
switch(n)
{
case :
{
if(abs(mp[]-mp[])==)return ;
return ;
}
case :
{
if(abs(mp[]-mp[])==)return ;
if(abs(mp[]-mp[])==)return ;
return ;
}
case :
{
if(abs(mp[]-mp[])==)return ;
if(abs(mp[]-mp[])==)return ;
return ;
}
case :
{
if(abs(mp[]-mp[])==)return ;
if(abs(mp[]-mp[])==)return ;
return ;
}
case :
{
if(abs(mp[]-mp[])==)return ;
if(abs(mp[]-mp[])==)return ;
if(abs(mp[]-mp[])==)return ;
if(abs(mp[]-mp[])==)return ;
return ;
}
case :
{
if(abs(mp[]-mp[])==)return ;
if(abs(mp[]-mp[])==)return ;
if(abs(mp[]-mp[])==)return ;
return ;
}
case :
{
if(abs(mp[]-mp[])==)return ;
if(abs(mp[]-mp[])==)return ;
if(abs(mp[]-mp[])==)return ;
return ;
}
}
return ;
}
void dfs(int n)
{
if(n==)
{
if(mp[n]==)
{
for(int i=; i<=; i++)
{
if(sign[i]==)
{
mp[n]=i;
sign[i]=;
if(check(n))
{
ans++;
for(int j=; j<; j++)
res[j]=mp[j];
}
sign[i]=;
mp[n]=;
}
}
}
else
{
if(check(n))
{
ans++;
for(int j=; j<; j++)
res[j]=mp[j];
}
}
}
else
{
if(mp[n]==)
{
for(int i=; i<=; i++)
{
if(sign[i]==)
{
mp[n]=i;
sign[i]=;
if(check(n))dfs(n+);
sign[i]=;
mp[n]=;
}
}
}
else
{
if(check(n))dfs(n+);
}
}
return;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
int n,cas=;
cin>>n;
while(n--)
{
cas++;
ans=,ms(res),ms(mp),ms(sign);
for(int i=; i<; i++)
{
cin>>mp[i];
if(mp[i]!=)sign[mp[i]]=;
}
for(int i=; i<; i++)
{
if(mp[i]==)
{
dfs(i);
break;
}
}
if(ans==)
{
printf("Case %d:",cas);
for(int i=; i<; i++)
printf(" %d",res[i]);
printf("\n");
}
else if(ans==)printf("Case %d: No answer\n",cas);
else printf("Case %d: Not unique\n",cas);
}
return ;
}

HDU 2514 Another Eight Puzzle(DFS)的更多相关文章

  1. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  2. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  3. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  4. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  5. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  6. HDU(1572),最短路,DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1572 很久没写深搜了,有点忘了. #include <iostream> #include ...

  7. [HDU 5113] Black And White (dfs+剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5113 题目大意:给你N*M的棋盘,K种颜色,每种颜色有c[i]个(sigma(c[i]) = N*M) ...

  8. HDU 5723 Abandoned country (最小生成树+dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...

  9. HDU 3974 Assign the task (DFS序 + 线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...

随机推荐

  1. [转]numpy线性代数基础 - Python和MATLAB矩阵处理的不同

    转自:http://blog.csdn.net/pipisorry/article/details/45563695 http://blog.csdn.net/pipisorry/article/de ...

  2. mybatis学习笔记三(关联关系)

    学习mybatis的关联关系,主要注解在代码上,这里不做解释.配置文件一样的就不贴了 1.关联关系表创建(学生对应老师 多对一) 学生老师表 2.表对应的实体类 package com.home.en ...

  3. Attribute的理解和认识

    1什么是Attribute? 在网上看到这样一段定义 MADN的定义为:公共语言运行时允许添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法和属性等.A ...

  4. php注册数模式

    在前两篇单例模式和工厂模式后,终于迎来了最后一个基础的设计模式--注册树模式. 什么是注册树模式? 注册树模式当然也叫注册模式,注册器模式.之所以我在这里矫情一下它的名称,是因为我感觉注册树这个名称更 ...

  5. CodeForces 669C Little Artem and Matrix GNU

    模拟. 把操作记录一下,倒着复原回去. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cs ...

  6. The Clocks

    The Clocks 题目链接:http://poj.org/problem?id=1166 题意:给出9个时钟的初始状态,问最少通过几次操作,能使每个时钟指向12点(每次操作都会使对应时钟顺时针旋转 ...

  7. Web Service接口返回泛型的问题(System.InvalidCastException: 无法将类型为“System.Collections.Generic.List`1[System.String]”的对象强制转换为类型“System.String[]”)

    在使用C#写Web Service时遇到了个很奇怪的问题.返回值的类型是泛型(我用的是类似List<string>)的接口,测试时发现总是报什么无法转换为对象的错误,百思不得其解. 后来在 ...

  8. UITableView优化方案

    1.UITableView的简单认识 > UITableView最核心的思想就是UITableViewCell的重用机制.简单的理解就是:UITableView只会创建一屏幕(或一屏幕多一点)的 ...

  9. 常用网页标签重置CSS

    做前端开发时常需要将网页中所有标签的格式清空,从而使各个浏览器标签样式一样.以下是网络上常见的CSS代码 /* =s Reset (by YUI 3) */ html{color:#000;} bod ...

  10. Jsp中out.println()与System.out.println()的区别

    第一次上Web实验课时咱写了一个jsp程序: <% System.out.println("Hello The World"); %> 然后放在浏览器下运行,结果是这样 ...