Problem Description

Suppose that you are an admiral of a famous naval troop. Our naval forces have got 21 battleships. There are 6 types of battleships. First, we have got one flagship in which the admiral must be and it is denoted by number 0. Others are denoted by number from 1 to 5, each of them has 2, 3, 4, 5, 6 ships of its kind. So, we have got 21 battleships in total and we must take a giant battle against the enemy. Hence, the correct strategy of how to arrange each type of battleships is very important to us. The shape of the battlefield is like the picture that is shown below. To simplify the problem, we consider all battleships have the same rectangular shape.Fortunately, we have already known the optimal state of battleships. As you can see, the battlefield consists of 6 rows. And we have 6 types of battleship, so the optimal state is that all the battleships denoted by number i are located at the i-th row. Hence, each type of battleship corresponds to different color. You are given the initial state of battlefield as input. You can change the state of battlefield by changing the position of flagship with adjacent battleship. Two battleships are considered adjacent if and only if they are not in the same row and share parts of their edges. For example, if we denote the cell which is at i-th row and j-th position from the left as (i,j), then the cell (2,1) is adjacent to the cells (1,0), (1,1), (3,1), (3,2). Your task is to change the position of the battleships minimum times so as to reach the optimal state. Note: All the coordinates are 0-base indexed.

Input

The first line of input contains an integer T (1 <= T <= 10), the number of test cases.  Each test case consists of 6 lines. The i-th line of each test case contains i integers, denoting the type of battleships at i-th row of battlefield, from left to right.

Output

For each test case, if you can’t reach the goal in no more than 20 moves, you must output “too difficult” in one line. Otherwise, you must output the answer in one line.

SampleInput

1
1
2 0
2 1 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5

SampleOutput

3

题意就是给你一个6*6的塔,上下两个相邻的单位可以进行交换,问最少进行几次交换,可以得到
0
1 1
2 2 2
3 3 3 3
……………………
这种状态,开始思路是用A*做,结果A*不是很熟练,没搞出来,写了个直接搜索炸了,然后我也是看了一下网上博客,使用双向搜索就行了。
思路就是从末尾开始往前搜索10步,从开始状态往后搜索10步,分别状态压缩一下存在map中,然后就看有没有两种相同的状态,否则就输出太难了。
代码:
 #include <bits/stdc++.h>
using namespace std;
#define ll long long
int fx[][] = {,,,,-,-,-,}; //左下,右下,左上,右上 struct node{
ll p[][];
int r,c;
int flag;
int step; node(){}
node(int _r,int _c,int _flag,int _step):r(_r),c(_c),flag(_flag),step(_step){}
}; queue<node>q;
map<ll,ll>p[];  //分别存储两个方向的bfs状态 ll _hash(node a){  //用hash压缩路径状态
ll res = ;
for(int i = ; i < ; i++){
for(int j = ; j <= i; j++){
res = res* + a.p[i][j];
}
}
return res;
} int bfs(node &s,node &e){
while(!q.empty()){
q.pop();
}
p[].clear();
p[].clear();
q.push(s);
q.push(e);
p[s.flag][_hash(s)] = ;  //必须要标记一下,因为后面会用到count函数查询是否存在
p[e.flag][_hash(e)] = ;
while(!q.empty()){
node now = q.front();
q.pop();
ll sta = _hash(now);
if(p[!now.flag].count(sta)){
int num = p[!now.flag][sta] + now.step;
if(num <= )
return num;
else
continue;
} if(now.step >= )  //处理10步即可
continue;
for(int i = ; i < ; i++){
node nxt = now;
nxt.step++;
nxt.r += fx[i][];
nxt.c += fx[i][];
if(nxt.r < || nxt.r > || nxt.c < || nxt.c > nxt.r)
continue;
swap(nxt.p[now.r][now.c],nxt.p[nxt.r][nxt.c]);
if(p[nxt.flag].count(_hash(nxt)) == )
p[nxt.flag][_hash(nxt)] = nxt.step;
q.push(nxt);
}
}
return -;
} int main(){
int t;
cin>>t;
node s, e;
while(t--){
for(int i = ; i < ; i++){
for(int j = ; j <= i; j++){
cin>>s.p[i][j];
if(s.p[i][j] == )
s.r = i, s.c = j;
e.p[i][j] = i;
}
}
s.flag = ;
s.step = ;
e = node(,,,);
int ans = bfs(s,e);
if(ans >= && ans <= )
cout << ans << endl;
else
cout << "too difficult" << endl;
}
return ;
}

 

Admiral(双向BFS + Hash)的更多相关文章

  1. 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...

  2. HDU 6171 Admiral(双向BFS+队列)题解

    思路: 最大步骤有20,直接BFS会超时. 因为知道开始情况和结果所以可以用双向BFS,每个BFS规定最大步骤为10,这样相加肯定小于20.这里要保存每个状态搜索到的最小步骤,用Hash储存.当发现现 ...

  3. 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1054 一开始我还以为要双向广搜....但是很水的数据,不需要了. 直接bfs+hash判重即可. # ...

  4. Hdu1401-Solitaire(双向bfs)

    Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered ...

  5. HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)

    题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...

  6. POJ1915Knight Moves(单向BFS + 双向BFS)

    题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...

  7. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  8. POJ 3170 Knights of Ni (暴力,双向BFS)

    题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...

  9. [转] 搜索之双向BFS

    转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...

随机推荐

  1. java后端_百度一面

    参考: https://www.nowcoder.com/discuss/215891?type=2&order=0&pos=10&page=1 1.会啥框架.不会. 2.锁的 ...

  2. 面试java_后端面经_5

    情话部分: 小姐姐:为什么有很多人在感情中付出很多,却得不到想要的结果? 你答:我听过一个这样的故事:讲的是蚯蚓一家人,有一天,蚯蚓爸爸特别无聊,就把自己切成了俩段愉快的打羽毛球去了,蚯蚓妈妈见状,把 ...

  3. hadoop2.7之作业提交详解(下)

    接着作业提交详解(上)继续写:在上一篇(hadoop2.7之作业提交详解(上))中已经讲到了YARNRunner.submitJob() [WordCount.main() -> Job.wai ...

  4. AutoCAD .NET: 遍历模型空间

    原文:http://spiderinnet1.typepad.com/blog/2012/06/autocad-net-iterate-through-model-space.html https:/ ...

  5. Python 參考網站

    Python 3 Readiness : http://py3readiness.org/ Python Speed Center : https://speed.python.org/ Python ...

  6. Git 实用技巧:git stash

    我们经常会遇到这样的情况: 正在dev分支开发新功能,做到一半时有人过来反馈一个bug,让马上解决,但是新功能做到了一半你又不想提交,这时就可以使用git stash命令先把当前进度保存起来.然后切换 ...

  7. ASP.NET 一个页上需要显示多个验证码

    1.后台获取验证字节流,以字符串的形式返回到前端. public ActionResult GetValidateGraphic() { var validate = new ValidateCode ...

  8. Django+zTree构建组织架构树

    树,因其清晰明了的展现形式而被广泛的使用 日常的开发过程中我们需要经常与"树"打交道,例如公司的组织架构树.服务器的项目归属树,管理后台侧边树等等,本篇文章介绍关于树的两个内容 多 ...

  9. Genymotion 启动app闪退解决方案

    1.之前安装Genymotion后,无法联网下载模拟器 解决方法:下载ova离线包,导入即可 2.启动app,一直处于闪退状态 解决方案: 进入BIOS----->Configuration-- ...

  10. JVM知识点总结

    JVM总体梳理 一.jvm体系总体概览 JVM体系总体分四大块:类的加载机制.jvm内存结构.GC算法 垃圾回收.GC分析 命令调优 这里画了一个思维导图,将所有的知识点进行了陈列,因为图比较大可以点 ...