您的位置:首页 > 其它

UVA 10085 The most distant state

2015-08-24 14:10 423 查看

分析

八数码,规则只能在3×3的棋盘里移动棋子,棋子只能移到空位。

本题需要求的是在移动的棋盘不重复的情况下可以移动的方式以及棋面。

这是一题隐式图搜索。

给定一个棋面状态,向空白处移动,可以视为空白处向四个方向移动。这样以空白处为研究对象,搜路,直到搜遍整个棋盘。可以发现,相对这题更合适
bfs
,广度优先搜索而不是深度优先搜索,因为题目所需要的只是最后一个可达状态。

下面简要的提出思路。

从队列中取出一个状态
求空白处
将空白处向四个方向移动得到新的状态
遍历状态无重复,压入队列,记录路径(父节点)


如何储存状态?这里的棋盘是3×3,开辟一个3×3的数组,其实也是长度为9的数,因为每一个棋盘都是独一无二。这里再加以可以使用hash法,将数排列。

这里使用了链表来存储。打印路径时,利用父节点递归到根节点再打印即可。

代码

#include <cstdio>
#include <cstring>
#define MAX 1000003
typedef int State[9];
State st[MAX];
int front, rear, fa[MAX], path[MAX], head[MAX], next[MAX];
int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
char cdir[] = "UDLR";

void print(int c) { if (c > 1) { print(fa[c]); printf("%c", cdir[path[c]]); } }

bool insert(int s)
{
int h = 0; for (int i = 0; i < 9; i++) h = h * 10 + st[s][i]; h %= MAX;
int u = head[h];
while (u) {
if (memcmp(st[u], st[s], sizeof(st[s])) == 0) return false;
u = next[u];
}
next[s] = head[h];
head[h] = s;
return true;
}

void bfs()
{
front = 1, rear = 2;
while (front < rear) {
State &s = st[front];
int x, y, z;
for (z = 0; z < 9; z++) if (!s[z]) break;
x = z / 3, y = z % 3;
for (int d = 0; d < 4; d++) {
int nx = x + dir[d][0];
int ny = y + dir[d][1];
int nz = nx * 3 + ny;
if (nx >= 0 && nx < 3 && ny >= 0 && ny < 3) {
State &t = st[rear];
memcpy(&t, &s, sizeof(s));
t[nz] = s[z]; t[z] = s[nz];
if (insert(rear)) { fa[rear] = front; path[rear] = d; rear++; }
}
}
front++;
}
}

void solve()
{
memset(head, 0, sizeof(head));
bfs();
for (int i = 0; i < 9; i++) {
printf("%d", st[rear-1][i]);
if ((i+1) % 3) printf(" ");
else           printf("\n");
}
print(rear-1);
printf("\n\n");
}

int main()
{
int T, cas = 0;
scanf("%d", &T);
while (T--) {
for (int i = 0; i < 9; i++) scanf("%d", &st[1][i]);
printf("Puzzle #%d\n", ++cas);
solve();
}
}


题目

Description

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.

283123
164=>84
75765
StartGoal
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

1
2 6 4
1 3 7
0 5 8


Sample Output

Puzzle #1
8 1 5
7 3 6
4 0 2
UURDDRULLURRDLLDRRULULDDRUULDDR
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: