题目传送门

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

分析;状态转移简单,主要是在图的存储以及判重问题,原来队列里装二维数组内存也可以,判重用神奇的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. iOS开发MAC下配置Svn和Git

    如果你对iOS开发中的版本控制还不了解那么你可以先看看这篇(大致看一遍就ok) http://www.cnblogs.com/iCocos/p/4767692.html   关于版本控制使用起来并不难 ...

  2. jquery阻止事件冒泡的3种方式

    第一种:return false, 缺点:直接返回了函数,函数后面的语句没法执行了: $('.btn').on('click',function(event){ do something ... re ...

  3. C语言字母频率统计

    在进行密码破解时有时候需要得到字母出现的频率信息,下面我将简单的使用C语言来读取一个文件,然后统计该文件内的字母出现的频率. 1.在D盘下新建一个文本文件(文件名为"A.txt") ...

  4. javaWeb---文件上传(commons-FileUpload组件)

    FileUpload是Apache组织(www.apache.org)提供的免费的上传组件,但是FileUpload组件本身还依赖于commons组件,所以从Apache下载此组件的时候还需要连同co ...

  5. 写了个简单的pdo的封装类

    <?php class PD { //造对象 public $dsn = "mysql:dbname=test2;host=localhost"; //数据库类型,数据库名和 ...

  6. jquery学习笔记----元素筛选

    1.eq()  筛选指定索引号的元素2.first() 筛选出第一个匹配的元素3.last() 筛选出最后一个匹配的元素4.hasClass() 检查匹配的元素是否含有指定的类5.filter() 筛 ...

  7. execl一个工作薄中有几个个工作表,将这几个个工作表分别保存到不同execl文件中

    用宏运行: Sub QEJebel()    Dim sh As Worksheet    Dim Pa As String    Pa = ThisWorkbook.Path    For Each ...

  8. Android屏幕旋转总结

    转自:http://www.myexception.cn/operating-system/1452058.html 1. ProjectConifg.mk中定义宏MTK_LCM_PHYSICAL_R ...

  9. Bootstrap简介

    接下来的一段时间,想研究一下现有的网页框架,第一个不容错过的就是Bootstrap,Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CS ...

  10. 【翻译七】java-同步

    Synchronization Threads communicate primarily by sharing access to fields and the objects reference ...