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. ZooKeeper系列(五)—— ACL 权限控制

    一.前言 为了避免存储在 Zookeeper 上的数据被其他程序或者人为误修改,Zookeeper 提供了 ACL(Access Control Lists) 进行权限控制.只有拥有对应权限的用户才可 ...

  2. Spring Cloud与Dubbo的完美融合之手「Spring Cloud Alibaba」

    很早以前,在刚开始搞Spring Cloud基础教程的时候,写过这样一篇文章:<微服务架构的基础框架选择:Spring Cloud还是Dubbo?>,可能不少读者也都看过.之后也就一直有关 ...

  3. 40道经典java多线程面试题

    40道经典java多线程面试题 题目来源 看完了java并发编程的艺术,自认为多线程"大成",然后找了一些面试题,也发现了一些不足. 一下问题来源于网上的博客,答案均为本人个人见解 ...

  4. 使用Cmake编译CEF时遇到Error in configuration process,project file may be invalid的解决办法

    今天在用Cmake编译cef框架时,弹出了错误,如图: 可以排查一下几种原因: 1.在64位计算机编译32位程序 可以更换编译环境,或者下载64位版本来解决这个问题. 2.选择的Visual Stud ...

  5. Nginx+Zuul集群实现高可用网关

    代码参考:https://github.com/HCJ-shadow/Zuul-Gateway-Cluster-Nginx Zuul的路由转发功能 前期准备 搭建Eureka服务注册中心 服务提供者m ...

  6. 记录一则clear重做日志文件的案例

    1.官方文档描述 2.故障报错信息 3.分析解决问题 1.官方文档描述 关于Clearing a Redo Log File的官方文档描述: A redo log file might become ...

  7. Ansible实践总结

    Ansible playbook 根据条件动态设置变量 首先新建 inventory,主机列表如下: node-01 ansible_host=192.168.64.30 node-02 ansibl ...

  8. RANSAC简史

    前言 在进行泡泡机器人[图灵智库]栏目的翻译的过程中,我发现在2018-2019的顶会中,依然有很多文章(我看到的不少于6篇)对RANSAC进行各种改进,这令我感到很吃惊.毕竟该方法在1981年就被提 ...

  9. GPU服务器安装NVIDIA驱动以及CUDA

    1.安装系统 系统版本: ubuntu16.04.05 LTS 分区要求: /boot 1024M swap 64G / 剩余空间

  10. js遍历API总结

    1.for 循环 普通遍历方法,可优化,存下数组的length,避免每次都去获取数组的length,性能提升 2.for-in 可遍历数组和对象, (for key in obj){} 该方法既可以读 ...