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. Spring boot实战项目整合阿里云RocketMQ (非开源版)消息队列实现发送普通消息,延时消息 --附代码

    一.为什么选择RocketMQ消息队列? 首先RocketMQ是阿里巴巴自研出来的,也已开源.其性能和稳定性从双11就能看出来,借用阿里的一句官方介绍:历年双 11 购物狂欢节零点千万级 TPS.万亿 ...

  2. 最小生成树模板题-----P3366 【模板】最小生成树

    题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入格式 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<=200000) ...

  3. R 实用命令 2

    1. how to temporarily unload the packages in R > library(Daim) 载入程辑包:‘Daim’ The following objects ...

  4. react中babel的使用

    在开发中经常会使用到es6语法,那么如何能够很好兼容es6写法呢

  5. 彻底搞懂Java中equals和==的区别

    java当中的数据类型和“==”的含义: 1.基本数据类型(也称原始数据类型) :byte,short,char,int,long,float,double,boolean.他们之间的比较,应用双等号 ...

  6. python学习笔记(6)--面向对象学习

    本节内容:   面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法.   引言 你现在是一家游戏公司的开发人员,现在需要你开发一款叫做(人狗大战)的游戏,你就思 ...

  7. springboot 整合shiro

    参考:        https://blog.csdn.net/fuweilian1/article/details/80309192(推荐)       https://blog.csdn.net ...

  8. eclipse导入的web项目不能部署到tomcat,显示为java项目

    今天在eclipse中导入之前做个项目,想运行起来看看,发现导入之后没法部署. 先解决办法如下: 右键项目 勾选上面三项并选择相应的值后就变成web项目,可以部署在tomcat上了.

  9. 基于CAS分析对ABA问题的一点思考

    基于CAS分析对ABA问题的一点思考 什么是CAS? 背景 synchronized加锁消耗太大 volatile只保证可见性,不保证原子性 基础 用CPU提供的特殊指令,可以: 自动更新共享数据; ...

  10. deepin 15.11 成功安装 jupyter notebook

    系统环境: OS:deepin 15.11(均为系统默认配置) Anaconda Distribution 64位(x86)安装程序(517 MB) Jupyter 官方提供三种安装方式:conda. ...