题目链接

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. IIS错误解决办法(HTTP Error 500.19 - Internal Server Error)

    window10 切换登陆用户,VS2015的IIS Express 调试代码报HTTP Error 500.19 - Internal Server Error 错误,无法读取配置文件解决办法. I ...

  2. iOS-延迟操作方法总结

    在实际应用中,有时候我们会需要延时执行某些操作,所以我们这里总结了四种延迟操作的方法,并简要分析了每种方法的异同. NSObject的相关方法 第一种方法是使用NSObject类的performSel ...

  3. C#下在图片文件本地

    //C#下载图片文件到本地,c#,c#下载,下载图片,下载文件,下载函数// 从图片地址下载图片到本地磁盘// 将二进制文件保存到磁盘 using System;using System.Drawin ...

  4. js事件监听-addEventListener (w3c标准) 和 attachEvent(ie)

    研究了一个小时,没看懂这两个属性 window.onload = function(){ var oDiv = document.getElementById("J_myDiv") ...

  5. 《Intel汇编第5版》 汇编拷贝字符串

    一.字符串定义 二.dup指令 三.调用Writestring过程 四.代码以及效果 TITLE String Copy INCLUDE Irvine32.inc includelib Irvine3 ...

  6. SNMP概述–运维必知的协议基础

    一.什么是SNMP?   SNMP是  “Simple Network Management Protocol” 的缩写,中文意思是简单网络管理协议,它是由互联网工作小组在RFC1157中定义的应用层 ...

  7. jffs2reset 实现分析

    在脚本 reset2default中看到如下内容: #!/usr/bin/lua local hwm = require "hwm" hwm.myexec("jffs2r ...

  8. 按键精灵*ff

    Function gethttp(URL) Set objXML=CreateObject("Microsoft.XMLHTTP") objXML.Open "Get&q ...

  9. android弹出dialog后,activity得到焦点

    1.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,WindowManager.LayoutParams.FLAG ...

  10. AppDelegate 里一个基本的跳转方法,用来在rootView崩溃的时候直接调试我自己的页面

    将 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)lau ...