【题意】

  • 现在给出一个三角矩阵,如果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 ;
}

最新文章

  1. python课程第三周重点记录
  2. Charlie&#39;s Change_完全背包&amp;&amp;路径记录
  3. HTTP一次请求的过程
  4. ThinkPHP3.1快速入门(2)数据CURD
  5. HDU-1031(水题)
  6. 基于Spring Cloud和Netflix OSS构建微服务,Part 2
  7. PHP+Apache怎样监控多个port和配置多网站
  8. 51 nod 1203 JZPLCM
  9. c++ 回调函数使用
  10. Vasya the Hipster
  11. request之额外路径
  12. python3高级编程
  13. A - Brackets POJ - 2955 (区间DP模板题)
  14. LOJ.6073.[2017山东一轮集训Day5]距离(可持久化线段树 树链剖分)
  15. 使用kubebapps 管理helm 仓库已经应用使用Monocular专门提供helm 仓库查找
  16. Bootstrap(8) 路径分页标签和徽章组件
  17. Django 文章标签功能
  18. 详解java中CAS机制所导致的问题以及解决——内存顺序冲突
  19. C#中获取各种路径获取方法
  20. Java反射机制涉及的类常见方法使用总结

热门文章

  1. 清理ThreadLocal
  2. NBUT 1116 Flandre&#39;s Passageway (LIS变形)
  3. Servlet和JSP之标签文件学习
  4. ubuntu16.0 安装 openstack
  5. UVA 1347 Tour 双调TSP
  6. C基础:关于预处理宏定义命令
  7. Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
  8. base64类
  9. Java 局部变量未初始化会报错,局部变量没有初始值,成员变量有初始值
  10. hihoCoder-1089-Floyd