【双向bfs】2017多校训练十 HDU 6171 Admiral
2024-08-24 03:50:47
【题意】
- 现在给出一个三角矩阵,如果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 ;
}
最新文章
- python课程第三周重点记录
- Charlie&#39;s Change_完全背包&;&;路径记录
- HTTP一次请求的过程
- ThinkPHP3.1快速入门(2)数据CURD
- HDU-1031(水题)
- 基于Spring Cloud和Netflix OSS构建微服务,Part 2
- PHP+Apache怎样监控多个port和配置多网站
- 51 nod 1203 JZPLCM
- c++ 回调函数使用
- Vasya the Hipster
- request之额外路径
- python3高级编程
- A - Brackets POJ - 2955 (区间DP模板题)
- LOJ.6073.[2017山东一轮集训Day5]距离(可持久化线段树 树链剖分)
- 使用kubebapps 管理helm 仓库已经应用使用Monocular专门提供helm 仓库查找
- Bootstrap(8) 路径分页标签和徽章组件
- Django 文章标签功能
- 详解java中CAS机制所导致的问题以及解决——内存顺序冲突
- C#中获取各种路径获取方法
- Java反射机制涉及的类常见方法使用总结
热门文章
- 清理ThreadLocal
- NBUT 1116 Flandre&#39;s Passageway (LIS变形)
- Servlet和JSP之标签文件学习
- ubuntu16.0 安装 openstack
- UVA 1347 Tour 双调TSP
- C基础:关于预处理宏定义命令
- Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
- base64类
- Java 局部变量未初始化会报错,局部变量没有初始值,成员变量有初始值
- hihoCoder-1089-Floyd