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. Linux配置及指令

    目录 Linux配置及指令 一.linux中常用软件的安装 二.主机名和网络 1.修改主机名 2.设置网络 三.关闭防火墙 1.检查防火墙是否开启 2.清除策略 3.永久关闭第一个防火墙 4.关闭第二 ...

  2. SpringCloud微服务小白入门之Eureka注册中心和服务中心搭建示例

    一.注册中心配置文件 代码复制区域: spring: application: name: spring-cloud-server server: port: 7000 eureka: instanc ...

  3. CAD2015 C#二次开发 字体变形

    开发环境:VS2012问题描述:一个简单的WinForm窗口,一个群组控件和一个Label,都是微软雅黑12pxCAD2015下,看起来却不一样,一个明显细得多. CAD2014下,无此问题.实验了C ...

  4. Spring参数的自解析--还在自己转换?你out了!

    背景前段时间开发一个接口,因为调用我接口的同事脾气特别好,我也就不客气,我就直接把源代码发给他当接口定义了. 没想到同事看到我的代码问:要么 get  a,b,c  要么  post [a,b,c]. ...

  5. 《Java 编写基于 Netty 的 RPC 框架》

    一 简单概念 RPC: ( Remote Procedure Call),远程调用过程,是通过网络调用远程计算机的进程中某个方法,从而获取到想要的数据,过程如同调用本地的方法一样. 阻塞IO :当阻塞 ...

  6. Java基础之Iterable与Iterator

    Java基础之Iterable与Iterator 一.前言: Iterable :故名思议,实现了这个接口的集合对象支持迭代,是可迭代的.able结尾的表示 能...样,可以做.... Iterato ...

  7. 前端通过Blob实现文件下载

    最近遇到一个需求,需要将页面中的配置信息下载下来供用户方便使用,以前这个场景的需求有时候会放到后端处理,然后给返回一个下载链接.其实并不需要这么麻烦,这样既增大了服务器的负载,也让用户产生了没有必要的 ...

  8. js封装 DOM获取

    function $(selector){ //获取第一个字符 var firstLetter = selector.charAt(0); //对第一个字符进行判断 switch(firstLette ...

  9. GitExtensions使用教程

    GitExtensions工具使用教程 第一步:安装 1.双击:GitExtensions24703SetupComplete.msi <ignore_js_op> image001.pn ...

  10. PHP文件基础操作

    文件的基本操作:(更多) fopen():文件打开 $file = fopen("file.txt","r+"); fopen()函数的参数是目标文件的路径和文 ...