HDU 3567 Eight II(八数码 II)

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 130000/65536 K (Java/Others)

 

Problem Description - 题目描述

  Eight-puzzle, which is also called "Nine grids", comes from an old game.

  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.

八数码,又名“九宫格”,其来源于一个古老的游戏。

此游戏中,给定3x3的底板与8个滑块。滑块数字为从1到8,且每个滑块覆盖一个格子。如你所见,这里有一个用’X’表示的空格。与空格拥有公共边的滑块可以移动到空格中。此操作会使’X’与滑块互换位置。

我们用’r’表示’X’和右边的滑块交换,’l’为左边,’u’为上边,’d’为下边。

CN

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

每个起始状态可以用如下规则的字符串S表示。

CN

  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.

这个问题需要使用操作'r','u','l','d' 使其从状态A到状态B。结果需符合如下要求:
.所有解中长度最短的方案。
.所有长度最短解中字典序最小的方案。

CN

Input - 输入

  The first line is T (T <= 200), which means the number of test cases of this problem.

  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.

第一行为T(T <= ),表示测试用例的数量。

每个用例有两行,第一行为状态A,第二行为状态B。
状态A到B必定有解。

CN

Output - 输出

  For each test case two lines are expected.

  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.

每个测试用例输出两行。

第一行的格式为 "Case x: d",x表示测试用例编号,从1开始,d表示从A到B操作列表的最短长度。
S为符合约束的操作列表,应放在第二行。

CN

Sample Input - 输入样例

2
12X453786
12345678X
564178X23
7568X4123

Sample Output - 输出样例

Case 1: 2
dd
Case 2: 8
urrulldr

题解

  其他解法不明觉厉,就用了双向BFS。

  和HDU 1043 Eight一个套路的康托展开,主要注意搜索的时候保障字典序最小,以及结束判断。

A方向的搜索才能结束全部的搜索状态,A方向优先距离,不考虑路径刷新。

B方向优先考虑距离,再考虑更低的字典序刷新路径。

  水平差,代码渣……

代码 C++

 #include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#define INF 0x7F7F7F7F
#define MX 362880
int tre[], ftr[], data[], map[MX][];
bool isUS[MX]; std::string a[][]; int lowBit(int a) { return -a&a; }
void add(int i) {
while (i < ) { ++tre[i]; i += lowBit(i); }
}
int sum(int i) {
int opt = ;
while (i) { opt += tre[i]; i -= lowBit(i); }
return opt;
} int pkt() {
int i, opt = ;
memset(tre, , sizeof tre);
for (i = ; ~i; --i) {
opt += sum(data[i])*ftr[i];
add(data[i]);
}
return opt;
}
void kte(int a) {
int i, j, tmp[];
for (i = ; i < ; ++i) tmp[i] = i + ;
for (i = ; i < ; ++i) {
j = a / ftr[i]; a %= ftr[i];
data[i] = tmp[j];
memcpy(tmp + j, tmp + j + , sizeof(int)*( - j));
}
data[i] = tmp[];
} void push(int now, int i, int j, int w, std::queue<int> &q) {
data[i] ^= data[j]; data[j] ^= data[i]; data[i] ^= data[j];
int nxt = map[now][w] = pkt();
data[i] ^= data[j]; data[j] ^= data[i]; data[i] ^= data[j];
if (!isUS[nxt]) q.push(nxt);
isUS[nxt] = ;
}
void init() {//d, l, r, u
int i, j, now;
ftr[] = ;
for (i = , j = ; ~i; --i, ++j) ftr[i] = ftr[i + ] * j;
memset(map, -, sizeof map);
std::queue<int> q;
for (i = ; i < MX; ++i) {
if (isUS[i]) continue;
q.push(i); isUS[i] = ;
while (!q.empty()) {
now = q.front(); q.pop();
kte(now);
for (j = ; data[j] != ; ++j);
if (j < ) push(now, j, j + , , q);
if (j % ) push(now, j, j - , , q);
if ((j + ) % ) push(now, j, j + , , q);
if (j > ) push(now, j, j - , , q);
}
}
} int len[MX][], lst[MX][], mid;
char pre[MX][], car[] = { 'd', 'l', 'r', 'u' }, opt[MX];
void fPush(int now, int i, int w, std::queue<int> &q) {
if (mid != INF) return;
int nxt = map[now][i];
if (nxt == -) return;
char c = w ? car[ - i] : car[i];
if (w) {
if (len[nxt][] != INF) return;
if (len[nxt][w] < len[now][w] + ) return;
if (len[nxt][w] == len[now][w] + ) {
if (c < pre[nxt][w]) {
pre[nxt][w] = c;
lst[nxt][w] = now;
}
return;
}
}
else {
if (len[nxt][w] <= len[now][w] + ) return;
if (len[nxt][] != INF) mid = nxt;
}
pre[nxt][w] = c;
len[nxt][w] = len[now][w] + ;
lst[nxt][w] = now;
q.push(nxt);
}
void fid(int st, int ed) {
if (st == ed) puts("");
else {
int now, i, j;
memset(len, INF, sizeof len); memset(pre, 'z', sizeof pre);
len[st][] = len[ed][] = ;
mid = INF;
std::queue<int> q; q.push(st); q.push(ed);
while (!q.empty()) {
if (mid != INF) break;
now = q.front(); q.pop();
if (len[now][] == INF) {
for (i = ; i < ; ++i) fPush(now, i, , q);
}
else if (len[now][] == INF) {
for (i = ; ~i; --i) fPush(now, i, , q);
}
else break;
}
now = mid;
printf("%d\n", len[now][] + len[now][]);
for (i = now, j = ; i != st; i = lst[i][], ++j) opt[j] = pre[i][];
for (i = j - ; ~i; --i) putchar(opt[i]);
for (i = now; i != ed; i = lst[i][]) putchar(pre[i][]); }
puts("");
}
int main() {
init();
int t, i, j, st, ed;
char tmp[]; for (i = scanf("%d ", &t); i <= t; ++i) {
gets(tmp);
for (j = ; j < ; ++j) data[j] = tmp[j] == 'X' ? : tmp[j] - '';
st = pkt();
gets(tmp);
for (j = ; j < ; ++j) data[j] = tmp[j] == 'X' ? : tmp[j] - '';
ed = pkt();
printf("Case %d: ", i);
fid(st, ed);
}
return ;
}

HDU 3567 Eight II(八数码 II)的更多相关文章

  1. HDU 1043 Eight(八数码)

    HDU 1043 Eight(八数码) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Problem Descr ...

  2. hdu 1043 Eight 经典八数码问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...

  3. HDU 3567 Eight II

    Eight II Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 3 ...

  4. HDU 3567 Eight II BFS预处理

    题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问 ...

  5. Hdu 1043 Eight (八数码问题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目描述: 3*3的格子,填有1到8,8个数字,还有一个x,x可以上下左右移动,问最终能否移动 ...

  6. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  7. HDU 1043 Eight 八数码问题 A*算法(经典问题)

    HDU 1043 Eight 八数码问题(经典问题) 题意 经典问题,就不再进行解释了. 这里主要是给你一个状态,然后要你求其到达\(1,2,3,4,5,6,7,8,x\)的转移路径. 解题思路 这里 ...

  8. HDU 1043 Eight (BFS&#183;八数码&#183;康托展开)

    题意  输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数  即这个排列在全部排列中的字典序  然后就是基础的BFS了 #include <bits/stdc++.h ...

  9. HDU 1043 八数码(A*搜索)

    在学习八数码A*搜索问题的时候须要知道下面几个点: Hash:利用康托展开进行hash 康托展开主要就是依据一个序列求这个序列是第几大的序列. A*搜索:这里的启示函数就用两点之间的曼哈顿距离进行计算 ...

随机推荐

  1. shiro使用redis作为缓存,出现要清除缓存时报错 java.lang.Exception: Failed to deserialize at org.crazycake.shiro.SerializeUtils.deserialize(SerializeUtils.java:41) ~[shiro-redis-2.4.2.1-RELEASE.jar:na]

    shiro使用redis作为缓存,出现要清除缓存时报错 java.lang.Exception: Failed to deserialize at org.crazycake.shiro.Serial ...

  2. python 循环结构 while for...in

    # ### 循环结构 while for...in """ 循环结构的特点:减少代码的冗余,提高代码的效率 语法形式: """ # 打印1~ ...

  3. ubuntu下zip文件操作

    转自 https://blog.csdn.net/hpu11/article/details/71524013 .zip $ zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹 ...

  4. Python基础(六) python生成xml测试报告

    思路: 1.使用xslt样式,这样可以很好的和xml结合,做出漂亮的报告 2.生成xml结构 xslt样式是个很有意思,也很强大的,现在用的很多,很方便就能做出一个漂亮的报告,可以百度一下,语法相当简 ...

  5. 二维码图片以字符串的形式保存DB,已文件流显示页面上

    以下是生成二维码的方法,我只用其中一个方法 这个需要引用ZXing.DLL 链接:https://pan.baidu.com/s/1mCTwHiAm_awtsPcibAotZw 提取码:ufp6 pu ...

  6. PeopleSoft translate value 排序

    这个function 可以对record.field 的dropdownlist 排序,也可以把描述前边加个数字.但是有时候用户不接受.所以调用这个方法是比较好的选择. Function Order_ ...

  7. /etc/resolv.conf

    /etc/resolv.conf它是DNS客户机配置文件,用于设置DNS服务器的IP地址及DNS域名,还包含了主机的域名搜索顺序.该文件是由域名解析 器(resolver,一个根据主机名解析IP地址的 ...

  8. luogu P1578 奶牛浴场

    很好的一道题 王知昆爷爷的论文(讲的特别清楚) https://wenku.baidu.com/view/bc8311f69e314332396893f7.html 先贴上AC代码 #include& ...

  9. QQ/微信中被禁止访问的网页怎么打开

    为什么关心这种技术?因为我经常听到身边搞微商.搞微信项目的朋友都在叫苦连天,由于微信域名屏蔽.微信域名被拦截.弄得他们尸横遍野,损失的连过年回家的路费都没了,曾经的叱咤风云一下变成了今日的倒亏损.腾讯 ...

  10. GO函数

    函数定义 Go语言中定义函数使用func关键字. func 函数名(参数)(返回值){ 函数体 } 函数名:由字母.数字.下划线组成.但函数名的第一个字母不能是数字.在同一个包内,函数名也称不能重名( ...