题目传送门

题意:一个图按照变成指定的图,问最少操作步数

分析;状态转移简单,主要是在图的存储以及判重问题,原来队列里装二维数组内存也可以,判重用神奇的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的更多相关文章

  1. UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))

    Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...

  2. BFS以及hash表判重的应用~

    主要还是讲下hash判重的问题吧 这道题目用的是除法求余散列方式 前几天看了下算法导论 由于我们用的是线性再寻址的方式来解决冲突问题 所以hash表的大小(余数的范围)要包含我们要求的范围 对mod的 ...

  3. 北京网络赛G BOXES 状态压缩+有序BFS+高维数组判重

    #include <bits/stdc++.h> using namespace std; ]; ][]; ][][]; ][][][]; ][][][][]; ][][][][][]; ...

  4. (通俗易懂小白入门)字符串Hash+map判重——暴力且优雅

    字符串Hash 今天我们要讲解的是用于处理字符串匹配查重的一个算法,当我们处理一些问题如给出10000个字符串输出其中不同的个数,或者给一个长度100000的字符串,找出其中相同的字符串有多少个(这样 ...

  5. hdu 4821 字符串hash+map判重 String (长春市赛区I题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这 ...

  6. HDU - 1067 Gap (bfs + hash) [kuangbin带你飞]专题二

    题意:    起初定28张卡牌的排列,把其中11,  21, 31, 41移动到第一列,然后就出现四个空白,每个空白可以用它的前面一个数的下一个数填充,例如43后面的空格可以用44填充,但是47后面即 ...

  7. codevs 1004 四子连棋 BFS、hash判重

    004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold       题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋 ...

  8. 洛谷 P1379 八数码难题 Label:判重&&bfs

    特别声明:紫书上抄来的代码,详见P198 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给 ...

  9. poj 3131 双向搜索+hash判重

    题意: 初始状态固定(朝上的全是W,空格位置输入给出),输入初始状态的空格位置,和最终状态朝上的位置,输出要多少步才能移动到,超过30步输出-1. 简析: 每一个格子有6种状态,分别是 0WRB, 1 ...

随机推荐

  1. C#更新SQLServer中的TimeStamp字段(时间戳) 防止同时修改一行时覆盖更新

    C#更新SQLServer中的TimeStamp字段(时间戳) 分类: C#2012-10-24 15:10 1878人阅读 评论(0) 收藏 举报 public partial class Form ...

  2. 搞笑世界杯(codevs 1060)

    题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...

  3. linux rpm问题:怎样查看rpm安装包的安装路径

      x rpm问题:怎样查看rpm安装包的安装路径 2010-07-12 21:19:15 标签:rpm linux 路径 休闲 职场 rpm -qpl xxxxxx.rpm 1.如何安装rpm软件包 ...

  4. DB2 Add hidden Identity columns

    An identity column contains a unique numeric value for each row in the table. DB2® can automatically ...

  5. 记录一个mysql按日期分组统计的查询

    http://blog.csdn.net/llwan/article/details/7738991

  6. java-解决业务操可能数据冲突问题

    问题提出,由于业务会出现多人同时操作,或者业务人员反复的操作,因此在业务流程中,需要对业务操作数据进行保护,由于使用数据库锁可能会引起一些难以预料的问题,因此考虑使用内存锁,设计思想:在内存中使用一个 ...

  7. Python无类再理解--metaclass,type

    上次理解过一次,时间久了,就忘了.. 再学习一次.. http://blog.jobbole.com/21351/ ======================= 但是,Python中的类还远不止如此 ...

  8. linux网络协议

    网络协议 本章节主要介绍linxu网络模型.以及常用的网络协议分析以太网协议.IP协议.TCP协议.UDP协议 一.网络模型 TCP/IP分层模型的四个协议层分别完成以下的功能: 第一层 网络接口层 ...

  9. ListView中每个item条目在被单击选中时能够高亮显示

    在布局文件中设定: android:listSelector="@android:color/holo_red_light" 在代码中实现 listView.setSelector ...

  10. Codeforces Testing Round #12 C. Subsequences 树状数组

    C. Subsequences     For the given sequence with n different elements find the number of increasing s ...