HDU 2514 Another Eight Puzzle(DFS)
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).
7 3 1 4 5 8 0
1 0 0 0 0 0 0 0
#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)的更多相关文章
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- HDOJ(HDU).1258 Sum It Up (DFS)
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- HDU(1572),最短路,DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1572 很久没写深搜了,有点忘了. #include <iostream> #include ...
- [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) ...
- HDU 5723 Abandoned country (最小生成树+dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...
- HDU 3974 Assign the task (DFS序 + 线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...
随机推荐
- IIS错误解决办法(HTTP Error 500.19 - Internal Server Error)
window10 切换登陆用户,VS2015的IIS Express 调试代码报HTTP Error 500.19 - Internal Server Error 错误,无法读取配置文件解决办法. I ...
- iOS-延迟操作方法总结
在实际应用中,有时候我们会需要延时执行某些操作,所以我们这里总结了四种延迟操作的方法,并简要分析了每种方法的异同. NSObject的相关方法 第一种方法是使用NSObject类的performSel ...
- C#下在图片文件本地
//C#下载图片文件到本地,c#,c#下载,下载图片,下载文件,下载函数// 从图片地址下载图片到本地磁盘// 将二进制文件保存到磁盘 using System;using System.Drawin ...
- js事件监听-addEventListener (w3c标准) 和 attachEvent(ie)
研究了一个小时,没看懂这两个属性 window.onload = function(){ var oDiv = document.getElementById("J_myDiv") ...
- 《Intel汇编第5版》 汇编拷贝字符串
一.字符串定义 二.dup指令 三.调用Writestring过程 四.代码以及效果 TITLE String Copy INCLUDE Irvine32.inc includelib Irvine3 ...
- SNMP概述–运维必知的协议基础
一.什么是SNMP? SNMP是 “Simple Network Management Protocol” 的缩写,中文意思是简单网络管理协议,它是由互联网工作小组在RFC1157中定义的应用层 ...
- jffs2reset 实现分析
在脚本 reset2default中看到如下内容: #!/usr/bin/lua local hwm = require "hwm" hwm.myexec("jffs2r ...
- 按键精灵*ff
Function gethttp(URL) Set objXML=CreateObject("Microsoft.XMLHTTP") objXML.Open "Get&q ...
- android弹出dialog后,activity得到焦点
1.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,WindowManager.LayoutParams.FLAG ...
- AppDelegate 里一个基本的跳转方法,用来在rootView崩溃的时候直接调试我自己的页面
将 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)lau ...