题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1067

queue里面果然不能放vector,还是自己写的struct比较省内存……

#include<bits/stdc++.h>
using namespace std; int a[][];
const int INF=0x3f3f3f3f;
const int p=;
const int md=; struct Node
{
int a[][];
Node(){}
Node(const int x[][])
{
for (int i=;i<;i++)
for (int j=;j<;j++)
a[i][j]=x[i][j];
}
int gethash() const
{
int now=;
for (int i=;i<;i++)
for (int j=;j<;j++)
now=(1ll*now*p%md+a[i][j])%md;
return now;
}
}; const int End[][]=
{
{,,,,,,,},
{,,,,,,,},
{,,,,,,,},
{,,,,,,,}
}; const Node endNode(End); const int term=endNode.gethash(); unordered_map<int,int> M; queue<Node> q; int bfs()
{
M.clear();
while (!q.empty()) q.pop();
for (int i=;i<;i++)
for (int j=;j<;j++)
if (a[i][j]%==)
{
int tmp=a[i][j];
a[i][j]=;
a[tmp/-][]=tmp;
}
Node tmp;
for (int i=;i<;i++) for (int j=;j<;j++) tmp.a[i][j]=a[i][j];
q.push(tmp);
int H=tmp.gethash();
if (H==term) return ;
M[H]=;
while (!q.empty())
{
Node now=q.front();
int st=M[now.gethash()];
q.pop();
for (int i=;i<;i++)
for (int j=;j<;j++)
if (now.a[i][j]== && now.a[i][j-]%!=)
{
int tar=now.a[i][j-]+;
int K=-;
for (int I=;I<;I++)
{
for (int J=;J<;J++)
{
if (now.a[I][J]==tar)
{
K=I*+J;
break;
}
}
if (K!=-) break;
}
now.a[i][j]=tar;
now.a[K/][K%]=;
int H=now.gethash();
if (!M.count(H))
{
if (H==term) return st+;
q.push(now);
M[H]=st+;
}
now.a[i][j]=;
now.a[K/][K%]=tar;
}
}
return INF;
} int main()
{
int t;
scanf("%d",&t);
while (t--)
{
for (int i=;i<;i++)
for (int j=;j<;j++)
scanf("%d",&a[i][j]);
int ans=bfs();
if (ans!=INF) printf("%d\n",ans);
else printf("-1\n");
}
return ;
}

[hdu 1067]bfs+hash的更多相关文章

  1. HDU 1067 Gap

    HDU 1067 Gap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   P ...

  2. HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)

    题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...

  3. 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1054 一开始我还以为要双向广搜....但是很水的数据,不需要了. 直接bfs+hash判重即可. # ...

  4. hdu 1496 Equations hash表

    hdu 1496 Equations hash表 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 思路: hash表,将原来\(n^{4}\)降 ...

  5. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  6. [BZOJ1054][HAOI2008]移动玩具 bfs+hash

    1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2432  Solved: 1355[Submit][Stat ...

  7. NOIP 模拟 玩积木 - 迭代加深搜索 / bfs+hash+玄学剪枝

    题目大意: 有一堆积木,0号节点每次可以和其上方,下方,左上,右下的其中一个交换,问至少需要多少次达到目标状态,若步数超过20,输出too difficult 目标状态: 0 1 1 2 2 2 3 ...

  8. hdu.1067.Gap(bfs+hash)

    Gap Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  9. HDU - 1067 Gap (bfs + hash) [kuangbin带你飞]专题二

    题意:    起初定28张卡牌的排列,把其中11,  21, 31, 41移动到第一列,然后就出现四个空白,每个空白可以用它的前面一个数的下一个数填充,例如43后面的空格可以用44填充,但是47后面即 ...

随机推荐

  1. go执行外部应用

    go执行外部应用 最近想将原来用asp.net mvc写的一个公司内部网站改用beego来写,但发现其中有一个功能是,能将加密的sqlite文件进行解密,因为这个解密是不能公开的,但有些同事需要查看这 ...

  2. (数据科学学习手札10)系统聚类实战(基于R)

    上一篇我们较为系统地介绍了Python与R在系统聚类上的方法和不同,明白人都能看出来用R进行系统聚类比Python要方便不少,但是光介绍方法是没用的,要经过实战来强化学习的过程,本文就基于R对2016 ...

  3. 浅析express以及express中间件

    一.express: 1.express: Express是什么? Express是基于node.js平台的web应用开发框架: 作用:可以实现快速搭建骨架: 优点:开发web应用更加方便,更加快捷. ...

  4. 深度分析如何在Hadoop中控制Map的数量(摘抄)

    很多文档中描述,Mapper的数量在默认情况下不可直接控制干预,因为Mapper的数量由输入的大小和个数决定.在默认情况下,最终input占据了多少block,就应该启动多少个Mapper.如果输入的 ...

  5. Java面试题集合

    1.Java的HashMap是如何工作的? HashMap是一个针对数据结构的键值,每个键都会有相应的值,关键是识别这样的值. HashMap 基于 hashing 原理,我们通过 put ()和 g ...

  6. java容器操作一

    List l = new ArrayList(); l.add(1); l.add("ne"); // 获取 System.out.println(l.get(0)); // 判断 ...

  7. Django信号的使用

    https://www.cnblogs.com/renpingsheng/p/7566647.html

  8. Deep Learning 之 最优化方法

    Deep Learning 之 最优化方法 2017年05月21日 22:18:40 阅读数:5910 写在前面本文主要是对Deep Learning一书最优化方法的总结,具体详细的算法,另起博文展开 ...

  9. python爬取数据需要注意的问题

    1 爬取https的网站或是接口的时候,如果是不受信用的SSL证书,会报错,需要添加如下代码,如下代码可以保证当前代码块内所有的请求都自动屏蔽ssl证书问题: import ssl # 这个是爬取ht ...

  10. Java开发WebService(使用Java-WS)

    前言: 初学Java,因为工作需要,直接跳到开发WebService.以前用.NET开发过WebService,对比一下,Java的WebService开发部署难度高了不止一个档次.网上的教程各式各异 ...