HDU - 3567 Eight II (bfs预处理 + 康托) [kuangbin带你飞]专题二
类似HDU1430,不过本题需要枚举X的九个位置,分别保存状态,因为要保证最少步数。要保证字典序最小的话,在扩展节点时,方向顺序为:down, left, right, up.
我用c++提交1500ms, G++提交858ms.
AC代码
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
typedef int state[9];
const int maxn = 4e5 + 5;
int vis[9][maxn];
const int dx[] = {1,0,0,-1};
const int dy[] = {0,-1,1,0};
const char dir[] = {'d','l','r','u'};
int fact[9];
void deal() { //1~8阶乘打表,方便编码
fact[0] = 1;
for(int i = 1; i < 9; ++i) fact[i] = fact[i - 1] * i;
}
int KT(int *a) {
int code = 0;
for(int i = 0; i < 9; ++i) {
int cnt = 0;
for(int j = i + 1; j < 9; ++j) if(a[j] < a[i]) cnt++;
code += fact[8 - i] * cnt;
}
return code;
}
int find_pos(int *a) {
for(int i = 0; i < 9; ++i) {
if(a[i] == 0) return i;
}
}
struct node{
int a[9];
int code, step;
int pos;
node() {
}
node(int *b, int code, int step ,int pos):code(code), step(step), pos(pos) {
memcpy(a, b, sizeof(a));
}
};
struct Dirction {
char dir;
int step;
int pre;
Dirction() {
}
Dirction(char dir, int step, int pre):dir(dir), step(step), pre(pre){
}
}d[9][maxn];
int st[9];
void bfs(int c) {
queue<node>q;
int code = KT(st);
vis[c][code] = 1;
d[c][code] = Dirction('x', 0, -1);
int pp = find_pos(st);
q.push(node(st, code, 0, pp));
while(!q.empty()) {
node p = q.front();
q.pop();
state &a = p.a;
int pos = p.pos;
int x = pos / 3, y = pos % 3;
for(int i = 0; i < 4; ++i) {
int px = x + dx[i], py = y + dy[i];
if(px < 0 || py < 0 || px >= 3 || py >= 3) continue;
int pz = px * 3 + py;
swap(a[pz], a[pos]);
code = KT(a);
if(vis[c][code]) {
swap(a[pz], a[pos]);
continue;
}
vis[c][code] = 1;
d[c][code] = Dirction(dir[i], p.step + 1, p.code);
q.push(node(a, code, p.step + 1, pz));
swap(a[pz], a[pos]);
}
}
}
int op[][9] = {
0, 1, 2, 3, 4, 5, 6, 7, 8,
1, 0, 2, 3, 4, 5, 6, 7, 8,
1, 2, 0, 3, 4, 5, 6, 7, 8,
1, 2, 3, 0, 4, 5, 6, 7, 8,
1, 2, 3, 4, 0, 5, 6, 7, 8,
1, 2, 3, 4, 5, 0, 6, 7, 8,
1, 2, 3, 4, 5, 6, 0, 7, 8,
1, 2, 3, 4, 5, 6, 7, 0, 8,
1, 2, 3, 4, 5, 6, 7, 8, 0
};
void print(int code, int c) {
if(d[c][code].pre == -1) return;
print(d[c][code].pre, c);
printf("%c", d[c][code].dir);
}
int main() {
memset(vis, 0, sizeof(vis));
deal();
for(int i = 0; i < 9; ++i) {
memcpy(st, op[i], sizeof(st));
bfs(i);
}
int a[9], b[9];
int T, kase = 1, ha[9];
char s[20];
scanf("%d", &T);
while(T--) {
int pos;
scanf("%s", s);
for(int i = 0; i < 9; ++i) {
if(s[i] == 'X') {
pos = i;
a[i] = 0;
}
else a[i] = s[i] - '0';
}
for(int i = 0; i < 9; ++i) {
ha[a[i]] = op[pos][i];
}
scanf("%s", s);
for(int i = 0; i < 9; ++i) {
if(s[i] == 'X') b[i] = 0;
else b[i] = s[i] - '0';
a[i] = ha[b[i]];
}
int code = KT(a);
printf("Case %d: %d\n", kase++, d[pos][code].step);
print(code, pos);
printf("\n");
}
return 0;
}
如有不当之处欢迎指出!
HDU - 3567 Eight II (bfs预处理 + 康托) [kuangbin带你飞]专题二的更多相关文章
- HDU - 3567 IDA* + 曼哈顿距离 + 康托 [kuangbin带你飞]专题二
这题难度颇大啊,TLE一天了,测试数据组数太多了.双向广度优先搜索不能得到字典序最小的,一直WA. 思路:利用IDA*算法,当前状态到达目标状态的可能最小步数就是曼哈顿距离,用于搜索中的剪枝.下次搜索 ...
- HDU - 1043 A* + 康托 [kuangbin带你飞]专题二
这题我第一次用的bfs + ELFhash,直接TLE,又换成bfs + 康托还是TLE,5000ms都过不了!!我一直调试,还是TLE,我才发觉应该是方法的问题. 今天早上起床怒学了一波A*算法,因 ...
- HDU - 3085 双向BFS + 技巧处理 [kuangbin带你飞]专题二
题意:有两只鬼,一个男孩女孩被困在迷宫中,男孩每秒可以走三步,女孩只能1步,鬼可以两步且可以通过墙.问男孩女孩是否可以在鬼抓住他们之前会合? 注意:每秒开始鬼先移动,然后两人开始移动. 思路:以男孩和 ...
- 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开
[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...
- HDU 3567 Eight II BFS预处理
题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问 ...
- HDU - 3533 bfs [kuangbin带你飞]专题二
看了好久的样例才看懂. 题意:有一个人要从(0,0)走到(n,m),图中有k个碉堡,每个碉堡可以向某个固定的方向每隔t秒放一次炮,炮弹不能穿越另一个碉堡,会被阻挡.人在移动的过程中不会被炮弹打到,也就 ...
- HDU - 1067 Gap (bfs + hash) [kuangbin带你飞]专题二
题意: 起初定28张卡牌的排列,把其中11, 21, 31, 41移动到第一列,然后就出现四个空白,每个空白可以用它的前面一个数的下一个数填充,例如43后面的空格可以用44填充,但是47后面即 ...
- HDU - 2102 A计划 (BFS) [kuangbin带你飞]专题二
思路:接BFS判断能否在限制时间内到达公主的位置,注意如果骑士进入传送机就会被立即传送到另一层,不会能再向四周移动了,例如第一层的位置(x, y, 1)是传送机,第二层(x, y, 2)也是传送机,这 ...
- 【算法系列学习】[kuangbin带你飞]专题二 搜索进阶 D - Escape (BFS)
Escape 参考:http://blog.csdn.net/libin56842/article/details/41909459 [题意]: 一个人从(0,0)跑到(n,m),只有k点能量,一秒消 ...
随机推荐
- SQL Server 2005的服务器角色(public)的问题
SQL Server 默认会有9个服务器角色,而且这些角色是不能删除和新增.修改的.关于这些角色相关介绍和权限,请参考 其中有一个特殊的角色public,任何登录都会属于该角色,它只拥有的权限是VIE ...
- 将centos_yum源更换为阿里云(官方文档)
http://mirrors.aliyun.com/help/centos?spm=5176.bbsr150321.0.0.d6ykiD 1.备份 mv /etc/yum.repos.d/CentOS ...
- pat 1022 digital library
#include <iostream> #include <sstream> #include <string> #include <vector> # ...
- C语言打印不出百分号'%'(以解决)
1. 问题描述 今天,我需要把百分号'%'打印出来,考虑到它是特殊符号,我就用转义字符'\',和它组合,结果是漆黑的屏幕什么也没有. 2. 解决办法 我问度娘, 她告诉我要打印百分号需要在它的前 ...
- iOS-AFNetworking封装Get(自定义HTTP Header)和Post请求及文件下载
前面提到AFNetworking是一个很强大的网络三方库,首先你需要引入AFNetworking三方库:如封装的有误还请指出,谢谢! 1.Get请求 /**Get请求 url 服务器请求地址 succ ...
- ContextLoaderListener加载过程
在web.xml中,配置ContextLoaderListener <!-- 配置Listener,用来创建Spring容器 --> <listener> <listen ...
- BZOJ 2287. [HZOI 2015]疯狂的机器人 [FFT 组合计数]
2287. [HZOI 2015]疯狂的机器人 题意:从原点出发,走n次,每次上下左右不动,只能在第一象限,最后回到原点方案数 这不煞笔提,组合数写出来发现卷积NTT,然后没考虑第一象限gg 其实就是 ...
- Selenium_WebDriver_定位元素
版权声明:本文为博主原创文章,转载请注明出处. 定位单个元素 WebDriver提供了八种元素定位方法,Java中定位语句形如:driver.findElement(By.id()): 何为元素定位? ...
- ubuntu中gdb调试工具的使用
首先有一段.c代码 1.可调试gcc编译:gcc -g -o xxx xxx.c 2.启动gdb调试 gdb xxx 3.在main函数处设置断点 break main 4.运行程序 run 5.其他 ...
- Hive metastore整体代码分析及详解
从上一篇对Hive metastore表结构的简要分析中,我再根据数据设计的实体对象,再进行整个代码结构的总结.那么我们先打开metadata的目录,其目录结构: 可以看到,整个hivemeta的目录 ...