【HDOJ3567】【预处理bfs+映射+康拓展开hash】
http://acm.hdu.edu.cn/showproblem.php?pid=3567
Eight II
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 130000/65536 K (Java/Others)
Total Submission(s): 4541 Accepted Submission(s): 990
In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid which can be represented as an 'X'. Tiles in grids having a common edge with the blank grid can be moved into that blank grid. This operation leads to an exchange of 'X' with one tile.
We use the symbol 'r' to represent exchanging 'X' with the tile on its right side, and 'l' for the left side, 'u' for the one above it, 'd' for the one below it.

A state of the board can be represented by a string S using the rule showed below.

The problem is to operate an operation list of 'r', 'u', 'l', 'd' to turn the state of the board from state A to state B. You are required to find the result which meets the following constrains:
1. It is of minimum length among all possible solutions.
2. It is the lexicographically smallest one of all solutions of minimum length.
The input of each test case consists of two lines with state A occupying the first line and state B on the second line.
It is guaranteed that there is an available solution from state A to B.
The first line is in the format of "Case x: d", in which x is the case number counted from one, d is the minimum length of operation list you need to turn A to B.
S is the operation list meeting the constraints and it should be showed on the second line.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<string>
using namespace std;
string str1, str2;
bool vis[];
int dx[] = { ,,,- };
int dy[] = { ,-,, };
char cs[] = { 'd','l','r','u' };
int pre[][];
int op[][];
int jc[];
int kt(int s) //康托展开
{
int code = ;
int st[];
for (int i = ; i >= ; i--, s /= )
st[i] = s % ;
for (int i = ; i<; i++)
{
int cnt = ;
for (int j = i + ; j<; j++)
if (st[j]<st[i]) cnt++;
code += jc[ - i] * cnt;
}
return code;
}
int skt = ;
int mypow(int x, int y) {
int ans = ;
while (y) {
if (y & )ans *= x;
x *= x;
y /= ;
}
return ans;
}
void bfs(string str,int x) {
memset(vis, , sizeof(vis));
queue<int>pq;
queue<int>pq2;
queue<int>pq3;
while (!pq.empty()) {
pq.pop();
}
while (!pq3.empty()) {
pq3.pop();
}
while (!pq2.empty()) {
pq2.pop();
}
int tmps = ;
for (int i = ; i < ; i++) {
tmps = tmps * + str[i] - '';
}
pq.push(tmps);
pq2.push(x);
int kt000 = kt(tmps);
pq3.push(kt000);
vis[kt000] = ;
while (!pq.empty()) {
int str0 = pq.front(); pq.pop();
//cout << str0 << endl;
int s0 = pq2.front(); pq2.pop();
int kt010 = pq3.front(); pq3.pop();
for (int i = ; i < ; i++) {
int x0 = s0 / ;
int y0 = s0 % ;
int tx = x0 + dx[i];
int ty = y0 + dy[i];
int s00 = tx * + ty;
if (tx >= && ty >= && ty < && tx < ) {
int str00=str0;
int skt1 = ((str0) / (mypow(, ( - s0)))) % ;
str00 -= skt1*mypow(,(-s0));
int skt2= ((str0) / (mypow(, ( - s00)))) % ;
str00 += skt2 * mypow(,(-s0));
str00 -= skt2 * mypow(, ( - s00));
str00 += skt1 * mypow(, ( - s0));
//str00[s00] = str0[s0];
int kt0 = kt(str00);
//skt++;
// cout << skt << endl;
// cout << kt0 << endl;
if (!vis[kt0]) {
vis[kt0] = ;
op[x][kt0] = i;
pre[x][kt0] = kt010;
pq.push(str00);
pq2.push(s00);
pq3.push(kt0);
}
}
}
} }
int main() {
int t;
jc[] = ;
for (int i = ; i < ; i++) {
jc[i] = jc[i - ] * i;
}
int case1 = ;
string str[];
str[] = "";
bfs(str[], );
// cout << "%%%%%\n";
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
str[] = "";
bfs(str[], );
scanf("%d", &t);
while (t--) {
cin >> str1 >> str2;
int u;
for (int i = ; i < ; i++) {
if (str1[i] == 'X') {
str1[i] = '';
u = i;
}
if (str2[i] == 'X') {
str2[i] = '';
}
}
char hash0[];
for (int i = ; i < ; i++) {
hash0[str1[i] - ''] = str[u][i];
}
string tmp = "";
for (int i = ; i < ; i++) {
tmp += hash0[str2[i] - ''];
}
int s1=, s2=;
for (int i = ; i < ; i++) {
s1 = s1 * + str[u][i] - '';
}
for (int i = ; i < ; i++) {
s2 = s2 * + tmp[i] - '';
}
int sta = kt(s1);
int en = kt(s2);
stack<int>stk;
while (!stk.empty())stk.pop();
while (sta != en) {
stk.push(en);
en = pre[u][en];
}
printf("Case %d: %d\n", case1++, stk.size());
while (!stk.empty()) {
int sss = stk.top();
stk.pop();
if (sss != sta) {
printf("%c",cs[op[u][sss]]);
}
}
printf("\n");
}
return ;
}
【HDOJ3567】【预处理bfs+映射+康拓展开hash】的更多相关文章
- ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))
祝大家新年快乐,相信在新的一年里一定有我们自己的梦! 这是一个简化的魔板问题,只需输出步骤即可. 玩具(Toy) 描述 ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具. 该玩具酷似魔方, ...
- ACM/ICPC 之 BFS(离线)+康拓展开 (HDU1430-魔板)
魔板问题,一道经典的康拓展开+BFS问题,为了实现方便,我用string类来表示字符串,此前很少用string类(因为不够高效,而且相对来说我对char数组的相关函数比较熟),所以在这里也发现了很多容 ...
- hdu-1043 bfs+康拓展开hash
因为是计算还原成一种局面的最短步骤,应该想到从最终局面开始做bfs,把所有能到达的情况遍历一遍,把值存下来. bfs过程中,访问过的局面的记录是此题的关键,9*9的方格在计算过程中直接存储非常占内存. ...
- hdu1430 魔板(康拓展开 bfs预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开
[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...
- bnuoj 1071 拼图++(BFS+康拓展开)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=1071 [题意]:经过四个点的顺逆时针旋转,得到最终拼图 [题解]:康拓展开+BFS,注意先预处理,得 ...
- 【HDOJ1043】【康拓展开+BFS】
http://acm.hdu.edu.cn/showproblem.php?pid=1043 Eight Time Limit: 10000/5000 MS (Java/Others) Memo ...
- hdoj1043 Eight(逆向BFS+打表+康拓展开)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 思路: 由于自己对康拓展开用的太少,看到这个题没想到康拓展开,最开始打算直接转换为数字,但太占内 ...
- 转换地图 (康托展开+预处理+BFS)
Problem Description 在小白成功的通过了第一轮面试后,他来到了第二轮面试.面试的题目有点难度了,为了考核你的思维能量,面试官给你一副(2x4)的初态地图,然后在给你一副(2x4)的终 ...
随机推荐
- GitHub下的文件放到Linux系统下
1.在GitHub账号下clone URL 项目. 2.到Linux服务器下执行以下操作: (1) mkdir test (2) cd test/ (3) git clone 复制的项目URL
- laravel基于Bootstrap的成功和失败的提示信息和验证提示信息
message.blade.php <!-- 成功提示框 --> @if(Session::has("success")) <div class="al ...
- Mybatis的二级缓存注意点
--声明:一下内容都不一定是正确的,只是自己测试的结果,请自己的动手操作得出自己的结论 1.开启Mybatis的二级缓存,不仅要在SqlMapConfig.xml中进行开启总开关,还要在对应的XXXM ...
- mybatis 无法自动补全,没有获得dtd文件
由于网络原因,eclipse无法下载到http://mybatis.org/dtd/mybatis-3-mapper.dtd,导致eclipse的编辑器无法自动补全标签. 解决办法:将dtd文件下载到 ...
- HashMap实现原理分析--面试详谈
1. HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组 数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二分查找时间复杂度小,为O(1 ...
- node 慕课笔记
global global.testVar2 = 200; 在别的文件中可以任意调用到 因为global是全局变量相当于js的window一样的
- C++基础知识:继承
1.继承的概念 面向对象中的继承指类之间的父子关系子类拥有父类的所有成员变量和成员函数子类就是一种特殊的父类子类对象可以当作父类对象使用子类可以拥有父类没有的方法和属性 2.C++中的访问级别与继承p ...
- shell脚本总结
1.变量 A: 定义变量A=1,只会对自己所在的shell进程有效 B: 定义变量export B=1,会对自己所在的shell进程及其子进程生效 C: 在script.sh脚本中定义的变量,在当前 ...
- aspnet core 2.0 发布之后没有 views文件夹
在项目文件里面 增加这个节点: MvcRazorCompileOnPublish 设置为false 是会发布views <PropertyGroup> <PackageTargetF ...
- Centos7安装RabbitMQ解决Erlang依赖报错
通过yum等软件仓库都可以直接安装RabbitMQ,但版本一般都较为保守. RabbitMQ官网提供了新版的rpm包(http://www.rabbitmq.com/download.html),但是 ...