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个操 ...
随机推荐
- jquery 图片遮罩 坠落遮挡效果
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- Map - leetcode [哈希表]
149. Max Points on a Line unordered_map<float, int> hash 记录的是斜率对应的点数 unordered_map<float, i ...
- 多表查询 INNER JOIN ON WHERE
SELECT *FROM STUDENT_INFO siINNER JOIN CLASS_INFO ci on si.CLASS_INFO_ID = ci.ID INNER JOIN TEACHER_ ...
- Web流量劫持
BadTunnel实战之远程劫持任意内网主机流量 http://www.freebuf.com/articles/web/109345.html http://blog.csdn.net/ts__cf ...
- [UWP小白日记-1]判断APP是否是第一次运行初始化SQLITE数据库
利用应用程序设置来实现此功能. 1.首先,获取APP设置的容器: ApplicationDataContainer localSettings = ApplicationData.Current.Lo ...
- VR行业未来是会走向巅峰还是会归于落寞?
日前591ARVR资讯网www.591arvr.com根据有关市场调研机构的权威数据分析进行预测表明,全球VR头显出货量将达到1.3亿部,但是现在市场数字不过年出货1700万部,这一部分VR并不指的是 ...
- 【noi openjudge题解】最低通行费
这道题完全没有必要去计算限制时间,把时间当做一个参数来做就行了.知道了这一点之后就可以直接使用DP求解了 #include <algorithm> #include <iostrea ...
- 关于Trie Tree简单实现
最近突然有兴致hiho一下了,实现了下trie tree,感觉而言,还是挺有意思的,个人觉得这货不光可以用来查单词吧,其实也可以用来替代Hash,反正查找,插入复杂度都挺低的,哈哈,啥都不懂,瞎扯.. ...
- MySql 安装过程(摘自网络)
下面的是MySQL安装的图解,用的可执行文件安装的,详细说明了一下! 打开下载的mysql安装文件mysql-5.0.27-win32.zip,双击解压缩, 运行“setup.exe”,出现如下界面 ...
- android 之 java环境部署
上甲骨文公司官网下载最新的jdk http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk8-downloads-2133151-z ...