【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)的终 ...
随机推荐
- python运算符号
运算符 比较运算 赋值运算 逻辑运算 成员运算
- Win10系列:UWP界面布局进阶3
与以往的Windows操作系统不同,Windows 10操作系统在正式版当中取消了任务栏中的"开始"按钮,将大部分的应用程序图标放置在开始屏中,同时将系统设置等常用功能整合到了Ch ...
- 验证码 kaptcha 参数详解
Constant 描述 默认值 kaptcha.border 图片边框,合法值:yes , no yes kaptcha.border.color 边框颜色,合法值: r,g,b (and optio ...
- tidb使用坑记录
转载自:https://www.cnblogs.com/linn/p/8459327.html tidb使用坑记录 1.对硬盘要求很高,没上SSD硬盘的不建议使用 2.不支持分区,删除数据是个大坑. ...
- 字符与字符串3——char 的大小
字符变量占用内存的大小,也就是char类型声明的变量,这个变量占多少字节. 一字节 char c = 'A'; printf("%d,%d\n", sizeof(c),sizeof ...
- 7 Serial Configuration 理解 (一)
reference : ug470- 7 series config.pdf 7系列器件有5种配置接口,每种配置接口对应一种或者多种配置模式和总线位宽.配置时序相对于引脚的CCLK,即使在内部产生C ...
- 理解K系列与ultra-scale的区别
总结: K系列FPGA与KU系列FPGA的主要区别,体现在: (1)工艺制程不一样,K-28nm,KU-20nm: (2)Ultra-Scale采用SSI:大容量K系列也采用SSI,SSI为了 ...
- Github拉取远端的时候提示“ssh: connect to host github.com port 22: Connection timed out”错误
在使用Github的时候,如果使用到拉取远端分支的时候或者测试ssh -T git@github.com的时候可能会出现连接失败的问题,错误描述为“ssh: connect to host githu ...
- 解析url中参数
兼容不带参数等情况 function getUrlParam(){ var params = {}; var search = location.search; search = /\?/.test( ...
- 2019.3.22 Week 12 : ZigBee and T/H chamber test
Test purposes Remove backside center ventilation holes, pls help to conduct climatic chamber test of ...