题目链接: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. ruby Encoding

    一. 查看ruby支持的编码 Encoding.name_list 二. 搜索编码 Encoding.find('US-ASCII') #=> US-ASCII,不存在则抛出异常 三. __EN ...

  2. ecshop 漏洞如何修复 补丁升级与安全修复详情

    目前ecshop漏洞大面积爆发,包括最新版的ecshop 3.0,ecshop 4.0,ecshop2.7.3全系列版本都存在着高危网站漏洞,导致网站被黑,被篡改,被挂马,许多商城系统深受其漏洞的攻击 ...

  3. Python3 函数参数

    # # 常规参数 # def jiafa(a,b): # print(a+b) # # jiafa(5,6) # # # def xinxi(name,location,sex='male'): # ...

  4. CPU计算密集型和IO密集型

    CPU计算密集型和IO密集型 第一种任务的类型是计算密集型任务,其特点是要进行大量的计算,消耗CPU资源,比如计算圆周率.对视频进行高清解码等等,全靠CPU的运算能力.这种计算密集型任务虽然也可以用多 ...

  5. WPF中使用定时器的注意事项

    原文:WPF中使用定时器的注意事项 注意事项 要使用System.Windows.Threading.DispatcherTimer,而不能使用System.Timers.Timer. 原因是WPF是 ...

  6. 扩展报表-JavaSet

    前言 使用商业分析中的扩展报表平台,可以很方便的进行数据分析,进行图表化直观展示.一般情况下使用SQL数据集进行SQL的编写,进而配合扩展报表平台进行数据分析图表的绘制,但SQL数据集针对固定的参数进 ...

  7. 2 web服务器:固定返回值

    1.老师给的思路 #tcp socket 服务端 socket = socket.socket() socket.bind() socket.listen() client_socket = sock ...

  8. Lambda方式左连接有Linq方式左连接

    网上查到的直接使用Join+DefaultIfEmpty的方式是错误的,实际生成SQL是两表先内联接,然后再LEFT JOIN.经过查证,参考资料,最终得到如下两种方式的左连接写法: public v ...

  9. jquery框架一点小心得

    下面的小事例 主要实现了 一和按ID查找,并获取元素的 value 或 标签内容和一个去字符串空格的小功能能 假设元素id=“myid”: 获取标签内容$("myid").html ...

  10. Spring 整合 Shiro

    一.引入依赖 <!-- spring start --> <dependency> <groupId>org.springframework</groupId ...