BFS+Hash(储存,判重) HDOJ 1067 Gap
题意:一个图按照变成指定的图,问最少操作步数
分析;状态转移简单,主要是在图的存储以及判重问题,原来队列里装二维数组内存也可以,判重用神奇的hash技术
#include <bits/stdc++.h>
using namespace std; const int MOD = 1e6 + 7;
struct Point {
int ch[5][9];
int x[4], y[4];
int step;
};
bool vis[MOD];
int ha; int get_hash(int c[5][9]) {
int tmp[60], k = 0;
for (int i=1; i<=4; ++i) {
for (int j=2; j<=8; ++j) {
tmp[k++] = c[i][j] % 10;
tmp[k++] = c[i][j] / 10;
}
}
int ret = 0;
for (int i=0; i<k; ++i) {
ret = (ret * 7 % MOD + tmp[i]) % MOD;
}
ret = (ret & 0x7fffffff) % MOD;
return ret;
} int init(void) {
int res[5][9];
int x[4] = {11, 21, 31, 41};
for (int i=0; i<4; ++i) {
for (int j=1; j<=7; ++j) {
res[i+1][j] = x[i]++;
}
res[i+1][8] = 0;
}
return get_hash (res);
} void change(Point &v, int x0, int y0, int k) {
int a = v.ch[x0][y0-1] + 1;
for (int i=1; i<=4; ++i) {
for (int j=2; j<=8; ++j) {
if (v.ch[i][j] == a) {
v.x[k] = i; v.y[k] = j;
swap (v.ch[x0][y0], v.ch[i][j]);
return ;
}
}
}
} int BFS(Point &p) {
memset (vis, false, sizeof (vis));
int sh = get_hash (p.ch);
vis[sh] = true;
queue<Point> que; que.push (p);
while (!que.empty ()) {
Point u = que.front (); que.pop ();
int uh = get_hash (u.ch);
if (uh == ha) {
return u.step;
}
for (int i=0; i<4; ++i) {
int x = u.x[i], y = u.y[i];
if (y == 1 || u.ch[x][y] % 10 == 7) continue;
Point v = u;
change (v, x, y, i);
int vh = get_hash (v.ch);
if (vis[vh]) continue;
vis[vh] = true; v.step++;
que.push (v);
}
} return -1;
} int main(void) {
ha = init ();
int T; scanf ("%d", &T);
while (T--) {
Point p; p.step = 0;
for (int i=1; i<=4; ++i) {
p.ch[i][1] = 0;
}
for (int i=1; i<=4; ++i) {
for (int j=2; j<=8; ++j) {
scanf ("%d", &p.ch[i][j]);
}
}
for (int i=1; i<=4; ++i) {
for (int j=2; j<=8; ++j) {
if (p.ch[i][j] == 11) {
swap (p.ch[1][1], p.ch[i][j]);
p.x[0] = i; p.y[0] = j;
}
else if (p.ch[i][j] == 21) {
swap (p.ch[2][1], p.ch[i][j]);
p.x[1] = i; p.y[1] = j;
}
else if (p.ch[i][j] == 31) {
swap (p.ch[3][1], p.ch[i][j]);
p.x[2] = i; p.y[2] = j;
}
else if (p.ch[i][j] == 41) {
swap (p.ch[4][1], p.ch[i][j]);
p.x[3] = i; p.y[3] = j;
}
}
}
int ans = BFS (p);
printf ("%d\n", ans);
} return 0;
}
BFS+Hash(储存,判重) HDOJ 1067 Gap的更多相关文章
- UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...
- BFS以及hash表判重的应用~
主要还是讲下hash判重的问题吧 这道题目用的是除法求余散列方式 前几天看了下算法导论 由于我们用的是线性再寻址的方式来解决冲突问题 所以hash表的大小(余数的范围)要包含我们要求的范围 对mod的 ...
- 北京网络赛G BOXES 状态压缩+有序BFS+高维数组判重
#include <bits/stdc++.h> using namespace std; ]; ][]; ][][]; ][][][]; ][][][][]; ][][][][][]; ...
- (通俗易懂小白入门)字符串Hash+map判重——暴力且优雅
字符串Hash 今天我们要讲解的是用于处理字符串匹配查重的一个算法,当我们处理一些问题如给出10000个字符串输出其中不同的个数,或者给一个长度100000的字符串,找出其中相同的字符串有多少个(这样 ...
- hdu 4821 字符串hash+map判重 String (长春市赛区I题)
http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这 ...
- HDU - 1067 Gap (bfs + hash) [kuangbin带你飞]专题二
题意: 起初定28张卡牌的排列,把其中11, 21, 31, 41移动到第一列,然后就出现四个空白,每个空白可以用它的前面一个数的下一个数填充,例如43后面的空格可以用44填充,但是47后面即 ...
- codevs 1004 四子连棋 BFS、hash判重
004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋 ...
- 洛谷 P1379 八数码难题 Label:判重&&bfs
特别声明:紫书上抄来的代码,详见P198 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给 ...
- poj 3131 双向搜索+hash判重
题意: 初始状态固定(朝上的全是W,空格位置输入给出),输入初始状态的空格位置,和最终状态朝上的位置,输出要多少步才能移动到,超过30步输出-1. 简析: 每一个格子有6种状态,分别是 0WRB, 1 ...
随机推荐
- php 与 js 正则匹配
php : <?php $str='<p>xxx</p>abc';$matches = array();if(preg_match('/<p>.*<\/ ...
- Linux Free命令各数字含义及Buffer和Cache的区别
Linux Free命令各数字含义及Buffer和Cache的区别 Free 命令的各数字含义 命令演示 [root@vm1 ~]# free total used free shared buffe ...
- moogodb3.x总结
搞了三天了,从阿里云服务器,到linux,再到mongodb数据库,只有一个感觉就是,头都要炸了,这篇是对mongodb做一个总结吧,也算有一个成果了 mongodb在linux下的安装 mongod ...
- "".equals(str)和str.equals("")的区别
如果当str为null的话 "".equals(str)不会报空指针异常,而str.equals("")会报异常.这种方式主要用来防止空指针异常
- 《C#本质论》读书笔记(14)支持标准查询操作符的集合接口
14.2.集合初始化器 使用集合初始化器,程序员可以采用和数组相似的方式,在集合的实例化期间用一套初始的成员来构造这个集合. 如果没有集合初始化器,就只有在集合实例化后才能显示添加到集合中--例如 ...
- Jquery.Datatables 结合时间段查询,daterangepicker实现Datatables表格带参数查询
参考:http://datatables.club/example/user_share/send_extra_param.html 下载地址:http://pan.baidu.com/s/1 ...
- SpringBoot Jms
https://dzone.com/articles/spring-boot-example-of-spring-integration-and-acti
- Android ANR分析(1)
转自:http://blog.csdn.net/itachi85/article/details/6918761 一:什么是ANR ANR:Application Not Responding,即应用 ...
- Notice: Only variable references should be returned by reference(PHP版本兼容性问题)
摘自:http://sushener.spaces.live.com/blog/cns!BB54050A5CFAFCDD!435.entry PHP5一个很让人恼火的一点就是BC(向后兼容)不是很理想 ...
- 在ubuntu上搭建开发环境9---Ubuntu删除ibus出现的问题及解决
删除 ibus输入法: sudo apt-get install ibus 我们会遇到下面的问题 Ubuntu 14.04 系统设置很多选项消失. 其实遇到这个问题的一个最主要的原因是之前执行过卸载i ...