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. 身体质量指数BMI

    Solution: 方法一:"Python语言程序设计"(中国大学MOOC平台)的答案 分析:对比两种指标,将共性(相同的区间)和异性(不同的区间)细分.这样两种指标的判断条件(不 ...

  2. python历史背诵

    一.python简介 python2:源代码不统一 有重复功能的代码 默认编码是ascii 没有中文 输出中文需要用头文件 #-*-coding=utf-8-*- 进行转换 py3:源代码统一 没有重 ...

  3. ES解决bootstrap checks failed, memory locking requested for elasticsearch process but memory is not locked问题

    问题描述: ERROR: [1] bootstrap checks failed[1]: memory locking requested for elasticsearch process but ...

  4. Eclipse中安装JRebel热部署教程

    Eclipse中安装JRebel热部署教程 前言        Eclipse安装JRebel插件可快速实现热部署,节省了大量重启时间,提高开发效率. 本文只介绍Eclipse安装JRebel插件版本 ...

  5. OPC协议

    详解OPC协议-工业控制和自动化领域的接口标准     摘要:OPC全称是OLEforProcessControl,即用于过程控制的OLE,是针对现场控制系统的一个工业标准接口,是工业控制和生产自动化 ...

  6. 设计模式(C#)——05适配器模式

    推荐阅读:  我的CSDN  我的博客园  QQ群:704621321       自然界有一条规则--适者生存.意思是生物要使用自然界的变化:在程序界中则需要新环境调用现存对象.那么,如何在新环境中 ...

  7. Egret白鹭开发小游戏中容易犯的错

    在游戏开发过程中遇到问题,请首先查阅:http://developer.egret.com/cn/github/egret-docs/Engine2D/minigame/minigameFAQ/ind ...

  8. 【CocosBuilder】学习笔记目录

    从2019年8月底开始学习CocosBuilder. ‎CocosBuilder 学习笔记(1) CCBReader 解析.ccbi文件流程 ‎CocosBuilder 学习笔记(2) .ccbi 文 ...

  9. MySQL的count(*)的优化,获取千万级数据表的总行数[转]

    一.前言 这个问题是今天朋友提出来的,关于查询一个1200w的数据表的总行数,用count(*)的速度一直提不上去.找了很多优化方案,最后另辟蹊径,选择了用explain来获取总行数. 二.关于cou ...

  10. ASP.NET Core中使用Csp标头对抗Xss攻击

    内容安全策略(CSP)是一个增加的安全层,可帮助检测和缓解某些类型的攻击,包括跨站点脚本(XSS)和数据注入攻击.这些攻击用于从数据窃取到站点破坏或恶意软件分发的所有内容(深入CSP) 简而言之,CS ...