UVa 10085 - The most distant state

7 篇文章 0 订阅

八数码问题的变形,bfs+哈希,打印路径时花了点时间,用哈希表next数组的原理进行回退就可以将问题解决。

代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>

using namespace std;
const int MAXSIZE = 1000003;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int back[MAXSIZE], path[MAXSIZE][9], dist[MAXSIZE];
int head[MAXSIZE], next[MAXSIZE];
char ac[MAXSIZE], save[100];
int Hash(int s)
{
    int v = 0;
    for(int i=0; i<9; i++)
        v = v * 10 + path[s][i];
    return v % MAXSIZE;
}
int insert(int s)
{
    int h = Hash(s);
    int u = head[h];
    while(u)
    {
        if(!memcmp(path[u], path[s], sizeof(path[s])))
            return 0;
        u = next[u];
    }
    next[s] = head[h];
    head[h] = s;
    return 1;
}
int bfs()
{
    memset(head, 0, sizeof(head));
    int front = 1, rear = 2;
    while(front < rear)
    {
        int z;
        for(z=0; z<9; z++)
            if(!path[front][z])
                break;
        int x = z/3, y = z%3;
        for(int i=0; i<4; i++)
        {
            int newx = x + dx[i], newy = y +dy[i];
            int newz = newx * 3 +newy;
            if(newx >= 0 && newx < 3 && newy >= 0 && newy < 3)
            {
                memcpy(path[rear], path[front], sizeof(path[front]));
                path[rear][newz] = path[front][z];
                path[rear][z] = path[front][newz];
                dist[rear] = dist[front] + 1;
                if(insert(rear))
                {
                    back[rear] = front;
                    switch(i)
                    {
                    case 0:
                        ac[rear] = 'U';
                        break;
                    case 1:
                        ac[rear] = 'D';
                        break;
                    case 2:
                        ac[rear] = 'L';
                        break;
                    case 3:
                        ac[rear] = 'R';
                        break;
                    }
                    rear++;
                }
            }
        }
        ++front;
    }
    return rear - 1;
}
int main()
{
#ifdef test
    freopen("sample.txt", "r", stdin);
#endif
    int num, goal;
    scanf("%d", &num);
    for(int i=1; i<=num; i++)
    {
        int sum = 0;
        back[1] = 0;
        for(int j=0; j<9; j++)
            scanf("%d", &path[1][j]);
        goal = bfs();
        printf("Puzzle #%d\n", i);
        for(int j=0; j<3; j++)
        {
            printf("%d", path[goal][j*3]);
            for(int k=1; k<3; k++)
                printf(" %d", path[goal][j*3+k]);
            puts("");
        }
        while(goal)
        {
            save[++sum] = ac[goal];
            goal = back[goal];
        }
        for (int i=sum-1; i; i--)
            printf("%c", save[i]);
        printf("\n\n");
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值