题目链接

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.
 
Sample Input
1
1
2 0
2 1 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5
 
Sample Output
3
 
题意:有21个数由1个0、2个1、3个2、4个3、5个4和6个5组成,这21个数构成一个三角形,与杨辉三角一样第一行1个数,第2行2个数……第6行6个数,现在要把这21个数归位按顺序摆好,0 在第一行,1在第二行……5在第六行,每次操作为0与它相邻的数可以进行交换,同行之间不能进行交换,求最少经过多少步将所有数归位,如果20步还我完成不了则输出“too difficult”。
 
思路:搜索,但是直接搜20步会超时,所以可以从两端各搜10步,这样就降低了复杂度,另外可以用hash来保存状态。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
int dx[]={,,-,-};
int dy[]={,,-,};
struct Node{
LL p[][];
int r,c;
int flag;
int dept;
};
queue<Node>Q;
map<LL,int>M[];
LL cal(Node a)
{
LL ans=;
for(int i=;i<;i++)
{
for(int j=;j<=i;j++)
{
ans=ans*+a.p[i][j];
}
}
return ans;
}
int bfs(Node &s,Node &e)
{
while(!Q.empty()) Q.pop();
M[].clear(); M[].clear();
M[][cal(s)]=;
M[][cal(e)]=;
Q.push(s);
Q.push(e);
while(!Q.empty())
{
Node x=Q.front(); Q.pop();
LL sta=cal(x);
if(M[!x.flag].count(sta))
{
int num=M[!x.flag][sta]+x.dept;
if(num<=) return num;
else continue;
}
if(x.dept>=) continue;
for(int i=;i<;i++)
{
Node y=x;
y.dept++;
y.r+=dx[i];
y.c+=dy[i];
if(y.r< || y.r>= || y.c< || y.c>y.r) continue;
swap(y.p[x.r][x.c],y.p[y.r][y.c]);
if(M[y.flag].count(cal(y))==) M[y.flag][cal(y)]=y.dept;
Q.push(y);
}
}
return -;
} int main()
{
int T; cin>>T;
Node s,e;
while(T--)
{
for(int i=;i<;i++)
{
for(int j=;j<=i;j++)
{
scanf("%lld",&s.p[i][j]);
if(s.p[i][j]==) s.r=i, s.c=j;
e.p[i][j]=i;
}
}
s.flag=; s.dept=;
e.r=; e.c=;
e.flag=; e.dept=;
int ans=bfs(s,e);
if(ans>=&&ans<=) printf("%d\n",ans);
else puts("too difficult");
}
return ;
}
/**
1
2 1
2 0 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5
0
1 1
2 2 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5
*/

hdu 6171---Admiral(双向搜索)的更多相关文章

  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. 【双向bfs】2017多校训练十 HDU 6171 Admiral

    [题意] 现在给出一个三角矩阵,如果0编号的在点(x,y)的话,可以和(x+1,y),(x-1,y),(x+1,y+1),(x-1,y-1)这些点进行交换. 我们每一次只能对0点和其他点进行交换.问最 ...

  4. 【HDU 6171】Admiral(搜索+剪枝)

    多校10 1001 HDU 6171 Admiral 题意 目标状态是第i行有i+1个i数字(i=0-5)共6行.给你初始状态,数字0可以交换上一行最近的两个和下一行最近的两个.求20步以内到目标状态 ...

  5. poj 1198 hdu 1401 搜索+剪枝 Solitaire

    写到一半才发现能够用双向搜索4层来写,但已经不愿意改了,干脆暴搜+剪枝水过去算了. 想到一个非常水的剪枝,h函数为  当前点到终点4个点的最短距离加起来除以2.由于最多一步走2格,然后在HDU上T了, ...

  6. Admiral(双向BFS + Hash)

    Problem Description Suppose that you are an admiral of a famous naval troop. Our naval forces have g ...

  7. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  9. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

随机推荐

  1. java 关键字final

    一.最终的意思,可以修饰类,方法,变量.特点: A:它修饰的类,不能被继承. B:它修饰的方法,不能被重写. C:它修饰的变量,是一个常量.二.面试相关: 1.局部变量基本类型 值不能发生改变 例如: ...

  2. 关于SQL经典题

    最近刚刚练过的一道sql分享给大家, 先上题目: -- 部门表 CREATE TABLE DEPT( DEPTNO INT PRIMARY KEY, -- 部门编号 DNAME VARCHAR(14) ...

  3. 关于shell脚本函数、数组、字符串截取、svn更新发布实例

    #/bin/bash #功能:QA服根据模板创建区配置文件并提交到svn上. SOURCE_PATH=/data/source_code SVN_PATH=/code/psm   #svn发布目录,要 ...

  4. 20170711_map/reduce

    js: map: var arr = [1,2,3,4,5]; var res = arr.map(function(x){ return x * x; }); //res 现在 = [1,4,9,1 ...

  5. jQuery源码分析-03扩展工具函数jQuery.extend

    // 扩展工具函数 jQuery.extend({ // http://www.w3school.com.cn/jquery/core_noconflict.asp // 释放$的 jQuery 控制 ...

  6. 关于MySQL latch争用深入分析与判断

    1.latch锁是什么锁? 2.latch锁是如何保护list? 3.latch争用的现象和过程? 4.latch什么时候会产生严重的争用? 5.如何监控latch争用情况? 6.如何确认latch争 ...

  7. Python爬虫初学(三)—— 模拟登录知乎

    模拟登录知乎 这几天在研究模拟登录, 以知乎 - 与世界分享你的知识.经验和见解为例.实现过程遇到不少疑问,借鉴了知乎xchaoinfo的代码,万分感激! 知乎登录分为邮箱登录和手机登录两种方式,通过 ...

  8. NOIP 2000 提高组 题解

    NOIP2000 提高组 题解 No 1. 进制转换 https://www.rqnoj.cn/problem/295 水题 对于n和基数r, 每次用n mod r, 把余数按照逆序排列 注意 mod ...

  9. 只需要一点点C++基础,新手也可以制作单机游戏内存修改器

    声明:本文只是为了初学C++的,能够做出一些实用的东西,跳出管理系统的束缚,提升学习的兴趣,在这里选取了单机游戏,请不要尝试在线游戏,违发而已未必可行.序:首先我们需要一个Qt+VS环境Qt从http ...

  10. Vue表单控件绑定

    前面的话 本文将详细介绍Vue表单控件绑定 基础用法 可以用 v-model 指令在表单控件元素上创建双向数据绑定.它会根据控件类型自动选取正确的方法来更新元素.v-model本质上不过是语法糖,它负 ...