The Most Distant State
Input: standard input
Output: standard output
The 8-puzzle is a square tray in which eight square tiles are placed. The remaining ninth square is uncovered. Each tile has a number on it. A tile that is adjacent to the blank space can be slid into that space. A game consists of a starting state and a specified goal state. The starting state can be transformed into the goal state by sliding (moving) the tiles around. The 8-puzzle problem asks you to do the transformation in minimum number of moves.
2 | 8 | 3 |
|
|
| 1 | 2 | 3 |
1 | 6 | 4 | => | 8 |
| 4 | ||
7 |
| 5 |
|
|
| 7 | 6 | 5 |
Start |
|
|
| Goal |
However, our current problem is a bit different. In this problem, given an initial state of the puzzle your are asked to discover a goal state which is the most distant (in terms of number of moves) of all the states reachable from the given state.
Input
The first line of the input file contains an integer representing the number of test cases to follow. A blank line follows this line.
Each test case consists of 3 lines of 3 integers each representing the initial state of the puzzle. The blank space is represented by a 0 (zero). A blank line follows each test case.
Output
For each test case first output the puzzle number. The next 3 lines will contain 3 integers each representing one of the most distant states reachable from the given state. The next line will contain the shortest sequence of moves that will transform the given state to that state. The move is actually the movement of the blank space represented by four directions : U (Up), L (Left), D (Down) and R (Right). After each test case output an empty line.
Sample Input
12 6 4
1 3 7
0 5 8
Sample Output
Puzzle #18 1 5
7 3 6
4 0 2
UURDDRULLURRDLLDRRULULDDRUULDDR
题意:小白中的八数码问题。 只是目标状态是最远的状态。。
思路: BFS + 判重。。
#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
int t;
int d[4] = {-3, -1, 3, 1};
char dd[10] = {"ULDR"};
char start[10];
int mark[10];
struct QUE
{
char tra[10];
char go[1005];
int gonum;
} q, p;
map<string, int> vis;
queue<QUE> Q;
void bfs() {
vis.clear();
while (!Q.empty()) {Q.pop();}
q.go[0] = '\0'; strcpy(q.tra, start); q.gonum = 0;
vis[q.tra] = 1;
Q.push(q);
while (!Q.empty())
{
p = Q.front();
Q.pop();
int zero;
for (zero = 0; zero < 9; zero ++) {
if (mark[p.tra[zero] - '0'] == 0) {
break;
}
}
for (int i = 0; i < 4; i ++) {
if (i == 1 && zero % 3 == 0) continue;
if (i == 3 && (zero + 1) % 3 == 0) continue;
q = p;
int tran = zero + d[i];
if (tran >= 0 && tran < 9) {
swap (q.tra[tran], q.tra[zero]);
q.go[q.gonum ++] = dd[i];
q.go[q.gonum] = '\0';
if (!vis[q.tra]) {
vis[q.tra] = 1;
Q.push(q);
}
}
}
}
}
int main() {
int tt = 1;
int imsb;
scanf("%d%*c", &t);
while (t --) {
getchar();
for (int i = 0; i < 9; i ++) {
scanf("%d", &imsb);
mark[i] = imsb;
start[i] = i + '0';
}
start[9] = '\0';
bfs();
printf ("Puzzle #%d\n", tt ++);
for (int i = 0; i < 9; i ++) {
if ((i + 1) % 3 == 0)
printf("%d\n", mark[p.tra[i] - '0']);
else
printf("%d ", mark[p.tra[i] - '0']);
}
printf("%s\n\n", p.go);
}
return 0;
}