HDU 3567 Eight II(八数码 II)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt }
h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: center; font-family: 宋体; color: rgb(26,92,200); font-weight: bold; font-size: 24.0000pt }
span.10 { font-family: "Times New Roman" }
span.15 { font-family: "Times New Roman"; color: rgb(26,92,200) }
span.16 { font-family: "Times New Roman"; color: rgb(26,92,200) }
p.pre { margin: 0pt; margin-bottom: .0001pt; text-align: left; font-family: 宋体; font-size: 12.0000pt }
span.msoIns { text-decoration: underline; color: blue }
span.msoDel { text-decoration: line-through; color: red }
table.MsoNormalTable { font-family: "Times New Roman"; font-size: 10.0000pt }
div.Section0 { }

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。结果需符合如下要求:
1.所有解中长度最短的方案。
2.所有长度最短解中字典序最小的方案。

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 <= 200),表示测试用例的数量。

每个用例有两行,第一行为状态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++

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <string>
  4 #include <queue>
  5 #define INF 0x7F7F7F7F
  6 #define MX 362880
  7 int tre[10], ftr[9], data[9], map[MX][4];
  8 bool isUS[MX];
  9
 10 std::string a[94][94];
 11
 12 int lowBit(int a) { return -a&a; }
 13 void add(int i) {
 14     while (i < 10) { ++tre[i]; i += lowBit(i); }
 15 }
 16 int sum(int i) {
 17     int opt = 0;
 18     while (i) { opt += tre[i]; i -= lowBit(i); }
 19     return opt;
 20 }
 21
 22
 23 int pkt() {
 24     int i, opt = 0;
 25     memset(tre, 0, sizeof tre);
 26     for (i = 8; ~i; --i) {
 27         opt += sum(data[i])*ftr[i];
 28         add(data[i]);
 29     }
 30     return opt;
 31 }
 32 void kte(int a) {
 33     int i, j, tmp[9];
 34     for (i = 0; i < 9; ++i) tmp[i] = i + 1;
 35     for (i = 0; i < 8; ++i) {
 36         j = a / ftr[i]; a %= ftr[i];
 37         data[i] = tmp[j];
 38         memcpy(tmp + j, tmp + j + 1, sizeof(int)*(8 - j));
 39     }
 40     data[i] = tmp[0];
 41 }
 42
 43 void push(int now, int i, int j, int w, std::queue<int> &q) {
 44     data[i] ^= data[j]; data[j] ^= data[i]; data[i] ^= data[j];
 45     int nxt = map[now][w] = pkt();
 46     data[i] ^= data[j]; data[j] ^= data[i]; data[i] ^= data[j];
 47     if (!isUS[nxt]) q.push(nxt);
 48     isUS[nxt] = 1;
 49 }
 50 void init() {//d, l, r, u
 51     int i, j, now;
 52     ftr[7] = 1;
 53     for (i = 6, j = 2; ~i; --i, ++j) ftr[i] = ftr[i + 1] * j;
 54     memset(map, -1, sizeof map);
 55     std::queue<int> q;
 56     for (i = 0; i < MX; ++i) {
 57         if (isUS[i]) continue;
 58         q.push(i); isUS[i] = 1;
 59         while (!q.empty()) {
 60             now = q.front(); q.pop();
 61             kte(now);
 62             for (j = 0; data[j] != 9; ++j);
 63             if (j < 6) push(now, j, j + 3, 0, q);
 64             if (j % 3) push(now, j, j - 1, 1, q);
 65             if ((j + 1) % 3) push(now, j, j + 1, 2, q);
 66             if (j > 2) push(now, j, j - 3, 3, q);
 67         }
 68     }
 69 }
 70
 71 int len[MX][2], lst[MX][2], mid;
 72 char pre[MX][2], car[4] = { ‘d‘, ‘l‘, ‘r‘, ‘u‘ }, opt[MX];
 73 void fPush(int now, int i, int w, std::queue<int> &q) {
 74     if (mid != INF) return;
 75     int nxt = map[now][i];
 76     if (nxt == -1) return;
 77     char c = w ? car[3 - i] : car[i];
 78     if (w) {
 79         if (len[nxt][0] != INF) return;
 80         if (len[nxt][w] < len[now][w] + 1) return;
 81         if (len[nxt][w] == len[now][w] + 1) {
 82             if (c < pre[nxt][w]) {
 83                 pre[nxt][w] = c;
 84                 lst[nxt][w] = now;
 85             }
 86             return;
 87         }
 88     }
 89     else {
 90         if (len[nxt][w] <= len[now][w] + 1) return;
 91         if (len[nxt][1] != INF) mid = nxt;
 92     }
 93     pre[nxt][w] = c;
 94     len[nxt][w] = len[now][w] + 1;
 95     lst[nxt][w] = now;
 96     q.push(nxt);
 97 }
 98 void fid(int st, int ed) {
 99     if (st == ed) puts("0");
100     else {
101         int now, i, j;
102         memset(len, INF, sizeof len); memset(pre, ‘z‘, sizeof pre);
103         len[st][0] = len[ed][1] = 0;
104         mid = INF;
105         std::queue<int> q; q.push(st); q.push(ed);
106         while (!q.empty()) {
107             if (mid != INF) break;
108             now = q.front(); q.pop();
109             if (len[now][1] == INF) {
110                 for (i = 0; i < 4; ++i) fPush(now, i, 0, q);
111             }
112             else if (len[now][0] == INF) {
113                 for (i = 3; ~i; --i) fPush(now, i, 1, q);
114             }
115             else break;
116         }
117         now = mid;
118         printf("%d\n", len[now][0] + len[now][1]);
119         for (i = now, j = 0; i != st; i = lst[i][0], ++j) opt[j] = pre[i][0];
120         for (i = j - 1; ~i; --i) putchar(opt[i]);
121         for (i = now; i != ed; i = lst[i][1]) putchar(pre[i][1]);
122
123     }
124     puts("");
125 }
126 int main() {
127     init();
128     int t, i, j, st, ed;
129     char tmp[10];
130
131     for (i = scanf("%d ", &t); i <= t; ++i) {
132         gets(tmp);
133         for (j = 0; j < 9; ++j) data[j] = tmp[j] == ‘X‘ ? 9 : tmp[j] - ‘0‘;
134         st = pkt();
135         gets(tmp);
136         for (j = 0; j < 9; ++j) data[j] = tmp[j] == ‘X‘ ? 9 : tmp[j] - ‘0‘;
137         ed = pkt();
138         printf("Case %d: ", i);
139         fid(st, ed);
140     }
141     return 0;
142 }
时间: 2024-12-10 03:13:58

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

HDU 1043 Eight(八数码)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: center; font-family: 宋体; color: rgb(26,92,200); font-weight: bold; fo

HDU 1043 POJ 1077 八数码问题

以下内容转载自:http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 八数码的八境界 研究经典问题,空说不好,我们拿出一个实际的题目来演绎.八数码问题在北大在线测评系统中有一个对应的题,题目描述如下: Eight Time Limit: 1000MS    Memory Limit: 65536K  Special Judge Description The 15-puzzle has been aroundfor ove

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

题意  输出八数码问题从给定状态到12345678x的路径 用康托展开将排列对应为整数  即这个排列在所有排列中的字典序  然后就是基础的BFS了 #include <bits/stdc++.h> using namespace std; const int N = 5e5, M = 9; int x[4] = { -1, 1, 0, 0}; int y[4] = {0, 0, -1, 1}; int fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320

HDU 3567 Eight II

Eight II Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 356764-bit integer IO format: %I64d      Java class name: Main Eight-puzzle, which is also called "Nine grids", comes from an old game. In this ga

HDU 3567 Eight II BFS预处理

题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问题实际上是每个小块位置的变化,上面的数字只是用来标记位置的, 所以通过映射将初末序列进行置换就好了,然后因为每次的x字符的置换位置不一样 所以需要以123456789这个初始串打9遍表就好了733ms #include <iostream> #include <cstdio> #inc

HDU - 3567 Eight II IDA*

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 represente

HDU 1043 Eight八数码解题思路(bfs+hash 打表 IDA* 等)

题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原为初始状态 思路: 参加网站比赛时拿到此题目,因为之前写过八数码问题,心中暗喜,于是写出一套暴力bfs+hash,结果TLE呵呵 思路一:bfs+hash(TLE) 1 #include <cstdio> 2 #include <cstring> 3 #include <queu

Hdu 1043 Eight (八数码问题)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目描述: 3*3的格子,填有1到8,8个数字,还有一个x,x可以上下左右移动,问最终能否移动到12345678x的状态? hint:每一个3*3的格子从上到右,从左到右,一行一行读. 解题思路: 比较简单的八数码问题,大一暑假老师讲过,一直手懒脑懒并没有亲自尝试过.因为是多实例,先从12345678x的状态bfs出来所有可以到达的状态,并且记录下来路径.八数码最重要的就是保存状态,如果这个

hdu 1034 Eight 传说中的八数码问题。真是一道神题,A*算法+康托展开

Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13506    Accepted Submission(s): 3855 Special Judge Problem Description The 15-puzzle has been around for over 100 years; even if you don'