UVA 10085 The most distant state

本文深入探讨了八数码问题的隐式图搜索算法,通过实例演示了如何使用广度优先搜索(BFS)解决该问题。文章详细解释了如何通过链表存储状态、如何确定空白位置并进行移动,以及如何避免重复状态。最后,提供了完整的代码实现,帮助读者理解并应用到实际问题中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分析

八数码,规则只能在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
7 5765
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值