hdu 6171---Admiral(双向搜索)
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.
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.
#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(双向搜索)的更多相关文章
- 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...
- HDU 6171 Admiral(双向BFS+队列)题解
思路: 最大步骤有20,直接BFS会超时. 因为知道开始情况和结果所以可以用双向BFS,每个BFS规定最大步骤为10,这样相加肯定小于20.这里要保存每个状态搜索到的最小步骤,用Hash储存.当发现现 ...
- 【双向bfs】2017多校训练十 HDU 6171 Admiral
[题意] 现在给出一个三角矩阵,如果0编号的在点(x,y)的话,可以和(x+1,y),(x-1,y),(x+1,y+1),(x-1,y-1)这些点进行交换. 我们每一次只能对0点和其他点进行交换.问最 ...
- 【HDU 6171】Admiral(搜索+剪枝)
多校10 1001 HDU 6171 Admiral 题意 目标状态是第i行有i+1个i数字(i=0-5)共6行.给你初始状态,数字0可以交换上一行最近的两个和下一行最近的两个.求20步以内到目标状态 ...
- poj 1198 hdu 1401 搜索+剪枝 Solitaire
写到一半才发现能够用双向搜索4层来写,但已经不愿意改了,干脆暴搜+剪枝水过去算了. 想到一个非常水的剪枝,h函数为 当前点到终点4个点的最短距离加起来除以2.由于最多一步走2格,然后在HDU上T了, ...
- Admiral(双向BFS + Hash)
Problem Description Suppose that you are an admiral of a famous naval troop. Our naval forces have g ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
随机推荐
- 51nod_1714:B君的游戏(博弈 sg打表)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1714 nim游戏的一个变形,需要打出sg函数的表 #incl ...
- Java发送新浪微博的问题
一,背景 2017-06-26微博公告替换了一些接口,导致以前的: statuses/repost 转发一条微博 statuses/update 发布一条微博 statuses/upload 上传图片 ...
- 走进安卓的重灾区----video
html5的video已经出来很久了.在ios上使用基本上没什么毛病,但是安卓下就是一个重灾区了,各种体验差.这几天搞了安卓的兼容,简直是要吐血.所以特意总结了一些强势的坑点. 先看一下常用的一些属性 ...
- Linux项目自动部署
场景:linux中自动部署项目在工作中经常遇到,快速高效的部署项目能够大幅提高工作效率.现在将项目部署的过程记录下来,以供参考,其中用到的知识点现在还有很多不很清楚,后面要好好琢磨琢磨! 1 项目部署 ...
- opencc 繁体简体互转 (C++示例)
繁体字通常采用BIG5编码,简体字通常采用GBK或者GB18030编码,这种情况下,直接使用iconv(linux下有对应的命令,也有对应的C API供编程调用)就行.对于默认采用utf-8 ...
- Nlpir Parser敏感词搜索灵玖语义技术应用
近年来随着网络技术的飞速发展和用户的剧烈增长,网络传输数据量越来越大,网络用语越来越趋于多样化.如何快速的屏蔽用户的不当言论.过滤用户发表内容中的非法词汇已成为关键词匹配领域的一项重大难题. 目前主要 ...
- 47. leetcode 437. Path Sum III
437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the ...
- C语言左值,运算符的优先级以及结合性探讨
刚刚开始看一本书.<C陷阱与缺陷>,相信学习C语言的大家都对这本书有耳闻.今天看到了里面的贪心法则.也即在读到一个字符后,尽可能多的读入更多的字符,直到读入的字符组成的字符串已经不可能再组 ...
- swift3.0 对UITextField()输入框输入的内容进行监控
首先需要继承 UITextFieldDelegate class TestViewController: UIViewController,UITextFieldDelegate{ } 添加事件委托 ...
- 转换Json中的时间戳为标准时间格式
//出自http://www.cnblogs.com/ahjesus function ConvertJSONDateToJSDate(jsonDate) { /// <su ...