题目链接: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. jmeter 插件安装

    1.下载Plugins Manager插件 打开下载插件地址:https://jmeter-plugins.org/ 2.将下载的plugins-manager.jar包复制到Jmeter安装目录,l ...

  2. LINQ巩固

    LINQ巩固 LINQ过滤运算符 Where 基于谓词函数过滤值 测试例子如下: public class TestModel { public string Name { get; set; } p ...

  3. (数据科学学习手札14)Mean-Shift聚类法简单介绍及Python实现

    不管之前介绍的K-means还是K-medoids聚类,都得事先确定聚类簇的个数,而且肘部法则也并不是万能的,总会遇到难以抉择的情况,而本篇将要介绍的Mean-Shift聚类法就可以自动确定k的个数, ...

  4. java入门---基础语法&基础常识&编码规范&命名规范

        一个Java程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作.下面简要介绍下类.对象.方法和实例变量的概念. 对象:对象是类的一个实例,有状态和行为.例如,一条狗是一个对 ...

  5. xshell怎样打印

    Xshell提供用本地打印机打印终端窗口文本的功能.在Xshell打印时可以沿用终端窗口使用的字体及颜色.且在页面设置对话框可以设置打印纸的边距. 如何设置打印纸的大小和方向: 1.打开xshell ...

  6. Android使用butterknife注解出现nullPointerException解决

    1.下载butterknife加入到你的libs中,构建到你的项目中,此时还不能注解成功,必须进行2配置 2.选择你的项目右键---->properties----->java compi ...

  7. 线程池ThreadPoolExecutor使用

    一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...

  8. Java重写构造方法

    public class TestSuper { public static void main(String[] args) { new ChildClass("alex", 1 ...

  9. jmeter更改启动编码设置

    项目中碰到这样的问题,在eclipse经过utf-8转码的代码,能正常运行,放到了jmeter里面运行,就是乱码,如下: String s = "乔佳飞"; String ss = ...

  10. 简单工具 & 杂技

    图片压缩: 腾讯智图(http://zhitu.isux.us/) 手机的所有尺寸大小规范: http://screensiz.es/phone 需求: 移动端宽高一致的盒子(因为移动端屏幕宽度不一样 ...