题目链接

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. 【css2、css3】css改变select选择框的样式

    效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  2. C# 三角形外心和外接圆半径计算方法

    在网上找了好久,想找一个现成的方法来用,折腾半天发现没有一个好用的,最后迫不得已自己写了一个,需要的同学可以直接拿去用, private void GetTriangleExcenterRadius( ...

  3. 命令行启动Hololens Emulator,可解决内存不足的问题

    有时候在虚拟机测试时常会出现 内存不足 的情况,导致应用卡顿,调整 /memsize 参数大小: start "HoloLens" "C:\Program Files ( ...

  4. mysql 查看数据库中所有表的记录数

    use information_schema; SELECT DISTINCT t.table_name, t.engine '表引擎', t.table_rowsFROM TABLES tWHERE ...

  5. js框架封装,模拟jQuery封装

    模拟jQuery框架,利用原生的js技术,封装一个js框架,以加深对jQuery的常用api的使用和面向对象原理的理解:一:结构部分首先利用闭包,构造一个自执行函数,然后利用选择器函数Sizzle,获 ...

  6. css-ie6下实现最小,最大宽度

    _width: expression((document.documentElement.clientWidth||document.body.clientWidth)<1040?"1 ...

  7. 《C++反汇编与逆向分析技术揭秘》——观察各种表达式的求值过程

    ---恢复内容开始--- 加法: 示例: 常量相加,则在编译期间就计算出两个常量相加后的结果,直接将这个结果参与运算,减少了运行期的计算.当有变量参与运算时,会先取出内存中的数据,放入通用寄存器中,再 ...

  8. 引用AForge.video.ffmpeg,打开时会报错:找不到指定的模块,需要把发行包第三方文件externals\ffmpeg\bin里的dll文件拷到windows的system32文件夹下。

    引用AForge.video.ffmpeg,打开时会报错:找不到指定的模块,需要把发行包第三方文件externals\ffmpeg\bin里的dll文件拷到windows的system32文件夹下. ...

  9. spring @Scheduled 执行2次

    今天遇到定时任务Scheduled 执行2次的情况,做一个简单的记录. 网上有好多办法,我几乎都试了一遍,我的情况下面的办法可用. 1. autodeploy属性值设置为false,如果此项设为tru ...

  10. typeof使用笔记

    typeof操作符返回一个字符串,表示未经计算的操作数的类型 以下是可能值的列表 类型      结果 Undefined   "undefined" Null  "nu ...