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

03-17
<think>好的,用户想了解在编程或技术背景下“distant”相关的概念或用法。首先,我需要回忆一下在计算机科学和信息技术中常见的包含“distant”的术语或概念。可能的方面包括网络通信中的距离、分布式系统、远程操作相关的技术,或者算法中的距离度量等。 首先想到的是“远程过程调用”(Remote Procedure Call, RPC),这里的“远程”对应distant,指调用不同地址空间(通常在不同机器上)的过程或函数。另外,“分布式计算”(Distributed Computing)也涉及多台计算机协同工作,可能分布在不同的地理位置,这也是distant的一种体现。 接着是网络方面的术语,比如“延迟”(Latency),即数据从一点传输到另一点的时间,这可能涉及到地理距离的影响,比如远距离通信中的延迟问题。还有“地理分布式系统”(Geographically Distributed Systems),例如内容分发网络(CDN),通过将服务器部署在全球各地来减少用户访问的延迟。 在数据结构和算法中,可能有“编辑距离”(Edit Distance)或“汉明距离”(Hamming Distance),这些用于衡量两个字符串之间的差异程度。虽然这里的distance不一定指物理距离,但属于技术中的距离概念。 此外,还有“远程终端”(Remote Terminal)或“远程桌面”(Remote Desktop),这些技术允许用户访问和控制位于远端的计算机,属于distant的应用场景。 需要确认这些术语的正确性,例如RPC确实涉及远程调用,分布式系统的定义是否符合用户需求。同时,考虑用户可能的深层需求,比如他们是否在寻找特定的技术实现,或者想了解相关概念的应用场景。还要注意用户提供的引用中的“module”和“protocol implementation”,可能引导到模块化或协议相关的远程通信技术。 最后,要确保回答的结构清晰,涵盖不同技术领域中的distant应用,并生成相关问题,帮助用户进一步探索。同时,检查是否有遗漏的重要概念,如物联网中的远程传感器、云计算中的远程服务等,确保覆盖全面。</think>在编程或技术语境中,“distant”通常与远程通信、分布式系统或空间/逻辑距离相关的概念相关。以下是几个关键方向: ### 1. **远程过程调用(RPC)** - 允许程序调用另一个地址空间(通常是另一台机器)的函数或方法,例如 gRPC[^1]。其核心是隐藏网络通信细节,使远程调用像本地调用一样。 - 示例代码片段: ```python # 使用 gRPC 的简单客户端调用 import grpc channel = grpc.insecure_channel('localhost:50051') stub = helloworld_pb2_grpc.GreeterStub(channel) response = stub.SayHello(helloworld_pb2.HelloRequest(name='Alice')) ``` ### 2. **分布式系统(Distributed Systems)** - 系统组件分布在多个物理或逻辑节点上,通过消息传递协作,例如: - **分布式数据库**:如 Cassandra,数据分片存储在不同节点。 - **区块链**:节点通过共识算法达成一致性。 ### 3. **网络延迟与地理距离** - 远距离通信需处理信号传播延迟(如跨洋光缆延迟约 60ms/1000km),影响协议设计(如 TCP 拥塞控制)。 ### 4. **距离度量算法** - **编辑距离(Edit Distance)**:衡量字符串差异,用于拼写检查。 $$ \text{edit\_distance}(s, t) = \min\{ \text{插入}, \text{删除}, \text{替换} \} $$ - **欧氏距离(Euclidean Distance)**:用于机器学习聚类(如 K-means): $$ d(\mathbf{p}, \mathbf{q}) = \sqrt{\sum_{i=1}^n (p_i - q_i)^2} $$ ### 5. **远程设备控制** - 如 IoT 中通过 MQTT 协议控制远距离传感器: ```python import paho.mqtt.client as mqtt client = mqtt.Client() client.connect("broker.hivemq.com", 1883) client.publish("sensor/temperature", "25°C") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值