【双向bfs】2017多校训练十 HDU 6171 Admiral
【题意】
- 现在给出一个三角矩阵,如果0编号的在点(x,y)的话,可以和(x+1,y),(x-1,y),(x+1,y+1),(x-1,y-1)这些点进行交换。
- 我们每一次只能对0点和其他点进行交换。问最少步数,使得最终变成:
0
1 1
2 2 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5
【思路】
【AC】
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
struct node
{
int val[][];
int tp;
int r;
int c;
int step;
}s,t; ull Hash(node t)
{
ull tmp=;
for(int i=;i<;i++)
{
for(int j=;j<=i;j++)
{
tmp=tmp*+t.val[i][j];
}
}
return tmp;
}
queue<node> Q;
map<ull,int> book[];
int dir[][]={{,},{-,},{,},{-,-}};
int bfs(node s,node t)
{
while(!Q.empty()) Q.pop();
book[].clear();
book[].clear();
s.step=t.step=;
s.tp=;t.tp=;
book[s.tp][Hash(s)]=;
book[t.tp][Hash(t)]=;
Q.push(s);Q.push(t);
while(!Q.empty())
{
node q=Q.front(),e;Q.pop();
ull tmp=Hash(q);
if(book[!q.tp].count(tmp))
{
if(book[!q.tp][tmp]+q.step<=)
return book[!q.tp][tmp]+q.step;
else continue;
}
if(q.step>=) continue;
for(int i=;i<;i++)
{
e=q;
e.r+=dir[i][];
e.c+=dir[i][];
if(e.r<||e.r>=||e.c<||e.c>=||e.c>e.r) continue;
swap(e.val[e.r][e.c],e.val[q.r][q.c]);
ull tmp=Hash(e);
if(book[e.tp].count(tmp)) continue;
book[e.tp][tmp]=++e.step;
Q.push(e);
}
}
return inf;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
for(int i=;i<;i++)
{
for(int j=;j<=i;j++)
{
scanf("%d",&s.val[i][j]);
if(s.val[i][j]==)
{
s.r=i;s.c=j;
}
t.val[i][j]=i;
}
}
t.r=t.c=;
int ans=bfs(s,t);
if(ans==inf) puts("too difficult");
else printf("%d\n",ans);
}
return ;
}
【双向bfs】2017多校训练十 HDU 6171 Admiral的更多相关文章
- 【构造+DFS】2017多校训练三 HDU 6060 RXD and dividing
acm.hdu.edu.cn/showproblem.php?pid=6060 [题意] 给定一棵以1为根的树,把这颗树除1以外的结点划分为k个集合(可以有空集),把1加入划分后的集合 每个集合的结点 ...
- 【极角排序+双指针线性扫】2017多校训练七 HDU 6127 Hard challenge
acm.hdu.edu.cn/showproblem.php?pid=6127 [题意] 给定平面直角坐标系中的n个点,这n个点每个点都有一个点权 这n个点两两可以连乘一条线段,定义每条线段的权值为线 ...
- 【(好题)组合数+Lucas定理+公式递推(lowbit+滚动数组)+打表找规律】2017多校训练七 HDU 6129 Just do it
http://acm.hdu.edu.cn/showproblem.php?pid=6129 [题意] 对于一个长度为n的序列a,我们可以计算b[i]=a1^a2^......^ai,这样得到序列b ...
- 【链表】2017多校训练三 HDU 6058 Kanade's sum
acm.hdu.edu.cn/showproblem.php?pid=6058 [题意] 给定一个排列,计算 [思路] 计算排列A中每个数的贡献,即对于每个ai,计算有ni个区间满足ai是区间中的第k ...
- 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储存.当发现现 ...
- 【2017多校训练2+计算几何+板】HDU 6055 Regular polygon
http://acm.hdu.edu.cn/showproblem.php?pid=6055 [题意] 给定n个格点,问有多少个正多边形 [思路] 因为是格点,只可能是正方形 枚举正方形的对角线,因为 ...
- 2017 多校训练 1002 Balala Power!
Balala Power! Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 2017 多校训练 1006 Function
Function Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
随机推荐
- 洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving
题目描述 Farmer John is distributing chocolates at the barn for Valentine's day, and B (1 <= B <= ...
- gunzip
gunzip——解压缩.gz的压缩文件 GNU unzip 命令所在路径:/bin/gunzip 示例1: # gunzip services.gz 解压缩当前目录下的services.gz文件,执行 ...
- MIPS简单入门
What ‘s the MIPS? 汇编语言 汇编语言是一类语言的总称,因处理器不同,而对应的不同机器的指令集也不同,产生了很多种汇编语言. 目前最流行的是ARM,MIPS,x86.ARM用于大量的移 ...
- metasploit-shellcode生成
0x00 安装metasploit $ curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/t ...
- iPhone Scrollbars with iScroll
Since we've had web browsers and JavaScript, we've been intent on replacing native browser functiona ...
- Bootstrap 静态控件
当您需要在一个水平表单内表单标签后放置纯文本时,请在 <p> 上使用 class .form-control-static. 实例: <!DOCTYPE html><ht ...
- 业务系统中最核心的状态设计,异常 case. (系统设计)
系统设计几方面 1. 具象: 几个角色 -- 用例 2. 具象: 边界模块 3. 具象: 实体模块 4. 抽象: 详细设计后,抽出公用的部分. 5. Status状态字段的设置和更改 系统设计中最核心 ...
- NSLayoutConstraint.constraintsWithVisualFormat详解,以及AlignAllCenterY
NSLayoutConstraint.constraintsWithVisualFormat详解,以及AlignAllCenterY 转载2015-07-08 18:02:02 鉴于苹果官方文档的解释 ...
- NOIP 成绩
这道题中点是在小数上,因为成绩可能是:“95.5 87.7……”所以我们就要用:printf和scanf这样就可以控制小数了!!! code: #include<bits/stdc++.h> ...
- centos6 安装windows字体
注意:字体文件必须是TTF或者ttf格式的文件, 1.yum install -y fontconfig mkfontscale2.mkdir -p /usr/share/fonts/windows_ ...