[hdu 1067]bfs+hash
题目链接: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的更多相关文章
- HDU 1067 Gap
HDU 1067 Gap Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) P ...
- HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)
题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...
- 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)
http://www.lydsy.com/JudgeOnline/problem.php?id=1054 一开始我还以为要双向广搜....但是很水的数据,不需要了. 直接bfs+hash判重即可. # ...
- hdu 1496 Equations hash表
hdu 1496 Equations hash表 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 思路: hash表,将原来\(n^{4}\)降 ...
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- [BZOJ1054][HAOI2008]移动玩具 bfs+hash
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2432 Solved: 1355[Submit][Stat ...
- NOIP 模拟 玩积木 - 迭代加深搜索 / bfs+hash+玄学剪枝
题目大意: 有一堆积木,0号节点每次可以和其上方,下方,左上,右下的其中一个交换,问至少需要多少次达到目标状态,若步数超过20,输出too difficult 目标状态: 0 1 1 2 2 2 3 ...
- hdu.1067.Gap(bfs+hash)
Gap Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- HDU - 1067 Gap (bfs + hash) [kuangbin带你飞]专题二
题意: 起初定28张卡牌的排列,把其中11, 21, 31, 41移动到第一列,然后就出现四个空白,每个空白可以用它的前面一个数的下一个数填充,例如43后面的空格可以用44填充,但是47后面即 ...
随机推荐
- ecshop 漏洞如何修复 补丁升级与安全修复详情
目前ecshop漏洞大面积爆发,包括最新版的ecshop 3.0,ecshop 4.0,ecshop2.7.3全系列版本都存在着高危网站漏洞,导致网站被黑,被篡改,被挂马,许多商城系统深受其漏洞的攻击 ...
- AIM Tech Round 5C. Rectangles 思维
C. Rectangles time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Linux mysql启动与关闭
service mysql stop service mysqld start
- Python3 使用基本循环实现多级目录(思路)
一.多级目录设计: 1. 通过循环的方式显示菜单和进入菜单 2. 设置标志位以提供回退上一层菜单 2. 设置标志位以提供退出程序 二.注意要点: 1. 菜单样式,层次关系不要弄混乱 2. 当输入错误时 ...
- Go语言的标准net库使用
Go语言的标准net库使用 与大多数语言一样,Go的标准库是很全的,因为Go的出现本来就是为了网络通信的高并发实现,所以其相关的网络库封装得很简洁,也更加的易读.这里对使用到的api进行记录. net ...
- SAPFiori
最新SAP Fiori常用事务代码持续更新中...谢谢支持 注意: 以 / 开头的事务码需要加/N或/O进入,否则进不去 SEGW: 创建Gateway Service /UI2/FLP ...
- 查看sql 作业明细及运行记录
--查看作业明细及状态 select j.name 'Job名', j.description '描述', j.ENABLED job_enabled, cast(js.last_run_date a ...
- 如何搭建SBT编译Scala开发的Android工程
作者:戚明峰 最近接触了shadowsocks的Android客户端项目源码(https://github.com/shadowsocks/shadowsocks-android),刚好这个项目是使用 ...
- 一个知乎日报pwa
前几天写了一篇文章关于如何实现一个简单版的pwa应用,端午撸了一个简易版知乎日报pwa. 关于如何写一个pwa,这里就不多介绍了,请移步这里.应用使用vue+vuex+axios,API这里,这里做了 ...
- python安装Django
现在有很多建站系统,很多都是基于php的,比如WordPress. 而Django 是老牌基于Python的CMS框架了,一直听说很强大,甚至曾经很红的Ruby On Rails都参考了它的很多概念, ...