uva 10085 The most distant state

本文介绍了一种使用广度优先搜索(BFS)算法解决八数码问题的方法,旨在找到从初始状态可达的最远目标状态。通过精心设计的状态表示和哈希策略确保搜索效率。

题目地址:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1026

题目描述:

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 6 4
7 0 5
=>
1 2 3
8 0 4
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
1
2 6 4
1 3 7
0 5 8
Sample Output
Puzzle #1
8 1 5
7 3 6
4 0 2
UURDDRULLURRDLLDRRULULDDRUULDDR

题意:

求源状态的最远的目标状态。

题解:

这道题还是栽在hash上了,本来一开始我想直接开一个大小为876543210的数组来标记哪些状态标记过,哪些状态没有标记过,但是其运行的实际效果却要很长时间,尽管这样代码的hash策略足够的简单,也可以说是一个完美一一对应的hash,但是实际运行效果还是慢!!无奈换成lrj 书上八数码的hash链表,时间却降下来了,在这里似乎程序的运行时间不仅仅取决于代码的逻辑结构(尽管的hash策略足够简单,甚至都没有各式各样的循环),并且还取决于代码的空间占用。当你试着调整我代码中关于队列 hash链表大小的设置时 不同的大小其运行的时间也是不一样的。

代码:

#include<stdio.h>
#include<string.h>
#define MM 400000
int N=0,cases=0;
typedef struct state
{
    int maze[3][3];
    char path[100];
    int zx;
    int zy;
    int len;
    state()
    {
        memset(path, '\0', sizeof(path));
        len = 0;
    }
}state, * state_link; 
state sque[MM];
int head = 0, rear = 0;
state sur,des;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int head_hash[MM],next_hash[MM];
int queue_clear()
{
    head = 0;
    rear = 0;
    return(0);
}
int push(state & elem)
{
    sque[rear++] = elem;
    return(0);
}
bool queue_empty()
{
    if(head > rear) return true;
    else return false;
}
state pop()
{
    return sque[head++];
}
int set_zxzy(state & elem)
{
    for(int i = 0; i <= 2; i++)
        for(int j = 0; j <= 2; j++)
            if(elem.maze[i][j] == 0)
            {
                elem.zx = i;
                elem.zy = j;
                return 0;
            }
    return 0;
}
int mv_state(state & elem, int i)
{
    char ch = 'U';
    int nx = 0, ny = 0;
    switch(i)
    {
    case 0:
        ch ='R';
        nx = elem.zx;
        ny = elem.zy + 1;
        break;
    case 1:
        ch = 'L';
        nx = elem.zx;
        ny = elem.zy - 1;
        break;
    case 2:
        ch = 'D';
        nx = elem.zx + 1;
        ny = elem.zy;
        break;
    case 3:
        ch = 'U';
        nx = elem.zx - 1;
        ny = elem.zy;
        break;
    }
    elem.maze[elem.zx][elem.zy] = elem.maze[nx][ny];
    elem.maze[nx][ny] = 0;
    elem.zx = nx;
    elem.zy = ny;
    elem.path[elem.len] = ch;
    elem.len++;
    elem.path[elem.len] = '\0';
    return(0);
}
int un_mv_state(state & elem, int i)
{
    switch(i)
    {
    case 0:
        mv_state(elem, 1);
        break;
    case 1:
        mv_state(elem, 0);
        break;
    case 2:
        mv_state(elem, 3);
        break;
    case 3:
        mv_state(elem, 2);
        break;
    }
    elem.len-=2;
    elem.path[elem.len] = '\0';
    return 0;
}
int get_hash(state & elem)
{
    int code = 0;
    for(int i = 0; i <= 9 - 1; i++)
        code = code * 10 + elem.maze[i/3][i%3];
    return code % MM;
}
bool is_visit(state & elem)
{
    int addr_ins = rear;
    int hash_code = get_hash(elem);
    int addr = head_hash[hash_code];
    while(addr)
    {
        if(memcmp(elem.maze, sque[addr].maze, sizeof(elem.maze)) == 0) return true;//find it!!
        addr = next_hash[addr];
    }
    next_hash[addr_ins] = head_hash[hash_code];
    head_hash[hash_code] = addr_ins;
    return false;//not find and insert it
}
int update_des(state & elem)
{
    if(elem.len > des.len) des = elem;
    return 0;
}
int BFS()
{
    while(!queue_empty())
    {
        state elem = pop();
        int ava_cnt = 0;
        for(int i = 0; i <= 4 - 1; i++)
        {
            int nx = elem.zx + dx[i];
            int ny = elem.zy + dy[i];
            if(nx >=0 && nx <= 2 && ny >= 0 && ny <= 2)
            {
                mv_state(elem, i);
                if(!is_visit(elem))
                {
                    ava_cnt ++;
                    push(elem);
                }
                un_mv_state(elem,i);
            }
        }
        if(ava_cnt == 0) update_des(elem);
    }
    return(0);
}
int print_state(state & elem)
{
    printf("%d %d %d\n",elem.maze[0][0], elem.maze[0][1], elem.maze[0][2]);
    printf("%d %d %d\n",elem.maze[1][0], elem.maze[1][1], elem.maze[1][2]);
    printf("%d %d %d\n",elem.maze[2][0], elem.maze[2][1], elem.maze[2][2]);
    printf("%s\n",elem.path);
    return 0;
}
int main()
{
    scanf("%d",&N);
    while(N--)
    {
        cases++;
        memset(des.path,'\0',sizeof(des.path));
        memset(head_hash, 0, sizeof(head_hash));
        memset(next_hash, 0, sizeof(next_hash));
        des.len = 0;
        scanf("%d%d%d",&sur.maze[0][0],&sur.maze[0][1],&sur.maze[0][2]);
        scanf("%d%d%d",&sur.maze[1][0],&sur.maze[1][1],&sur.maze[1][2]);
        scanf("%d%d%d",&sur.maze[2][0],&sur.maze[2][1],&sur.maze[2][2]);
        set_zxzy(sur);
        queue_clear();
        push(sur);
        BFS();
        printf("Puzzle #%d\n",cases);
        print_state(des);
        printf("\n");
    }
    return(0);
}
<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") ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值