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 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
随机推荐
- 【TensorFlow入门完全指南】模型篇·逻辑斯蒂回归模型
import库,加载mnist数据集. 设置学习率,迭代次数,batch并行计算数量,以及log显示. 这里设置了占位符,输入是batch * 784的矩阵,由于是并行计算,所以None实际上代表并行 ...
- web开发之负载均衡的简单架构
负载均衡 负载均衡的核心思想就是:请求分担 最简单的配置: 一台负载均衡服务器 两台webserver服务器 两台webserver服务器需要配置相同的服务器环境,设置相同的域名指向 负载均衡服务器需 ...
- TCP/IP协议之ping和traceroute
Ping程序就是调用的就是ICMP报文.利用的是ICMP的应答和回显请求.来看下具体的ping报文. Request的报文类型为8 Reply的类型为0 通过具体的ping报文可以看到ping报文的大 ...
- java 笔记 Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别
查了一些资料也不是太明白两个的区别,但是前者是最安全的用法 打个简单的比方,你一个WEB程序,发布到Tomcat里面运行.首先是执行Tomcat org.apache.catalina.startup ...
- Selenium自动化初级/中级网络授课班招生
近期学习selenium和appium的测试人员越来越多,应广大刚接触UI自动化以及对selenium想要更深入了解的测试人员的要求,特请一位资深测试架构师为我们开课讲解selenium,以及如何设计 ...
- 史上最易懂——ReactNative分组列表SectionList使用详情及示例详解
React Native系列 <逻辑性最强的React Native环境搭建与调试> <ReactNative开发工具有这一篇足矣> <解决React Native un ...
- Linux 下实时查看日志
Linux 下实时查看日志 cat /var/log/*.log 如果日志在更新,如何实时查看 tail -f /var/log/messages 还可以使用 watch -d -n 1 cat /v ...
- nodeJS实现路由功能
前面的话 本文将使用NodeJS实现较复杂应用的路由功能 结构 项目结构如下 代码如下 功能 [router.js] // 加载所需模块 var http = require('http'); var ...
- 教学小例子:简易的webSevrer
HttpListener 流利简单的API static void Main() { using (var server = new SimpleWebServer("http://loca ...
- 表达式求值(栈方法/C++语言描述)(一)
一个算数表达式(以下简称为表达式)由运算数.运算符.左括号和右括号组成,定义一个枚举类型TokenType表示为: typedef enum { BEGIN, NUMBER, OPERATOR, LE ...