hdu - 1026 - Ignatius and the Princess I

本文探讨了一种解决N×M迷宫问题的方法,从起点(0,0)到终点(N-1,M-1),每一步可能需要等待特定时间。使用优先队列实现广度优先搜索,输出每个步骤的详细路径。通过代码实现,直观展示路径规划与时间消耗的关系。

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

题意:一个N*M的图,一秒内可上下左右选个方向走一步,一进去,若有数字,就要花数字大小的时间停留在那个格子,输出从起点(0, 0)到终点(N-1, M-1)每一秒的路径。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026

——>>会选择优先队列来做就基本上没问题了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int maxn = 100 + 10;
struct node
{
    int x;
    int y;
    int step;
    int fax;
    int fay;
    node(){}
    node(int xx, int yy): x(xx), y(yy){}
    bool operator < (const node& e) const
    {
        return step > e.step;
    }
    void init(int i, int j)
    {
        x = i;
        y = j;
        step = fax = fay = -1;
    }
}no[maxn][maxn];
char MAP[maxn][maxn];
int N, M, cnt;
int dx[] = {-1, 1,  0, 0};
int dy[] = { 0, 0, -1, 1};

bool bfs()
{
    priority_queue<node> pq;
    pq.push(no[0][0]);
    node temp;
    while(!pq.empty())
    {
        temp = pq.top();
        pq.pop();
        for(int i = 0; i < 4; i++)
        {
            int newx = temp.x + dx[i];
            int newy = temp.y + dy[i];
            if(newx >= 0 && newx < N && newy >= 0 && newy < M && MAP[newx][newy] != 'X' && no[newx][newy].step == -1)
            {
                no[newx][newy].step = temp.step + 1;
                if(MAP[newx][newy] != '.') no[newx][newy].step += MAP[newx][newy] - '0';
                no[newx][newy].fax = temp.x;
                no[newx][newy].fay = temp.y;
                pq.push(no[newx][newy]);
                if(newx == N - 1 && newy == M - 1) return 1;
            }
        }
    }
    return 0;
}

void print(node u)
{
    if(u.fax == 0 && u.fay == 0)
    {
        printf("It takes %d seconds to reach the target position, let me show you the way.\n", no[N-1][M-1].step);
        printf("%ds:(0,0)->(%d,%d)\n", cnt++, u.x, u.y);
        for(int i = 0; i < MAP[u.x][u.y] - '0'; i++) printf("%ds:FIGHT AT (%d,%d)\n", cnt++, u.x, u.y);
        return;
    }
    print(no[u.fax][u.fay]);
    printf("%ds:(%d,%d)->(%d,%d)\n", cnt++, u.fax, u.fay, u.x, u.y);
    for(int i = 0; i < MAP[u.x][u.y] - '0'; i++) printf("%ds:FIGHT AT (%d,%d)\n", cnt++, u.x, u.y);
}

int main()
{
    int i, j;
    while(scanf("%d%d", &N, &M) == 2)
    {
        for(i = 0; i < N; i++)
            for(j = 0; j < M; j++)
            {
                cin>>MAP[i][j];
                no[i][j].init(i, j);
            }

        no[0][0].step = 0;
        cnt = 1;
        if(bfs()) print(no[N-1][M-1]);
        else printf("God please help our poor hero.\n");
        printf("FINISH\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值