题目链接: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. labview在编写程序框图中遇到的一些布尔按钮控制布尔指示灯问题

                      上图布尔控件按下,数据0x04成功发送给下位机,布尔灯不亮. ............... ............. ........... 下图布尔控件按下, ...

  2. 加载旋转框(loading spinner)

    目标是这样的 用到的组件 AlertDialog 和 ProgressBar 先创建一个 AlertDialog 的布局 <?xml version="1.0" encodi ...

  3. Hibernate-ORM:07.Hibernate中的参数绑定

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客会讲解Hibernate中的参数绑定,就是相当于sql语句中的where后面的条件 一,讲解概述: 1 ...

  4. luogu4238 【模板】多项式求逆

    ref #include <iostream> #include <cstdio> using namespace std; typedef long long ll; int ...

  5. php之apc浅探

    扩展编译: ./configure --enable-apc --with-php-config=/usr/local/php/bin/php-config --prefix=/usr/local/a ...

  6. 6.1 python+appium元素定位方式(登录app)

    1.0.0     :常见的十种元素定位方式 .driver.find_element_by_id() #id定位 .driver.find_element_by_name() #name定位(已经凉 ...

  7. LeetCode 全解(bug free 训练)

    1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...

  8. c++调用Python基础功能

    c++调用Python首先安装Python,以win7为例,Python路径为:c:\Python35\,通过mingw编译c++代码.编写makefile文件,首先要添加包含路径:inc_path ...

  9. Go基础篇【第6篇】: 内置库模块 flag

    import "flag" flag包实现了命令行参数的解析.每个参数认为一条记录,根据实际进行定义,到一个set集合.每条都有各自的状态参数. 在使用flag时正常流程: 1.  ...

  10. cocos2d-x环境搭建 摘自百度文库

    cocos2d-x环境搭建 引言:笔者在网上寻觅了很多资料,最终发现了这份实际可用的文档,供大家参考.源地址:http://wenku.baidu.com/view/93f7b0f1102de2bd9 ...