【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)的终 ...
随机推荐
- windows中mysql5.7保存emoji表情
1.找到my.ini文件,修改一下配置: [client] default-character-set=utf8mb4 [mysqld] character-set-client-handshake ...
- 用socket.io将Node后台与M站相联系
目的:用socket.io将Node后台与M站相联系,实现当Node后台添加一条数据时,调用该接口的M站不用手动刷新自动出现新增的数据 具体实现:当在后台系统position列表中添加/修 ...
- 【资料收集】PCA降维
重点整理: PCA(Principal Components Analysis)即主成分分析,是图像处理中经常用到的降维方法 1.原始数据: 假定数据是二维的 x=[2.5, 0.5, 2.2, 1. ...
- 8.3 C++格式标识和操纵器
参考:http://www.weixueyuan.net/view/6409.html 总结: 我们需要借助格式标识符来控制cout对象的输出格式. 在ios_base类中,系统已经定义了很多格式标识 ...
- delete请求
Action(){ int HttpRetCode; //定义一个变量,用于接收HTTP返回的状态码 web_add_header("Session-Id", "2e25 ...
- angular2.0 官网架构文档
Angular 是一个用 HTML 和 JavaScript 或者一个可以编译成 JavaScript 的语言(例如 Dart 或者 TypeScript ),来构建客户端应用的框架. 该框架包括一系 ...
- python列举django中间件的5个请求方法
process——request:请求进来时,权限认证. process——view:路由匹配之后,能够得到试图的试图函数. process——exceptions:异常是执行. process——t ...
- JAVA AES加密解密
import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java ...
- 记第十四届省赛参赛体会&第十三届
emmm....时间还是很久远了 还是流水账 这次比赛我还是挺开心的 因为感觉我们余神就是一把宝剑,然后我是她的Buff 前面四道题就挺顺利都1A过了,十年余神就是强无敌呀 最后两分钟过了第五题,银牌 ...
- mybatis-generator eclipse插件 使用方法
mybatis-generator eclipse插件离线安装包 网址:http://download.csdn.net/download/gxl442172663/7624747 云盘地址:http ...