走迷宫值路径记录

利用bfs走迷宫是最基础的方法,记录访问过的每个点然后回溯最短路径的点

首先把问题简单化一下,只有墙壁和路,代码比较简单,只要会bfs基本上看得懂

// 走迷宫记录路径
// 概述:有一个N*M大小的迷宫,其中'#'代表墙壁无法通过,'.'代表路,每走一步都将花费1s的时间,求从起点(0,0)到终点(N-1,M-1)的最短路径,并将路径打印
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int INF = 1000005;
const int MAX_N = 105;
struct Point
{
    int x;
    int y;
}first, next,pre[MAX_N][MAX_N];
char pic[MAX_N][MAX_N];
int N, M;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int value[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];

int bfs()
{
    queue<Point> que;
    que.push(first);
    while(que.size())
    {
        first = que.front();
        que.pop();
        if(first.x == N - 1 && first.y == M -1)
            return value[N - 1][M - 1];
        for(int i = 0; i < 4; i++)
        {
            next.x = first.x + dx[i];
            next.y = first.y + dy[i];
            if(next.x >= 0 && next.x < N && next.y >= 0 && next.y < M && pic[next.x][next.y] != '#' && vis[next.x][next.y])
            {
                value[next.x][next.y] = value[first.x][first.y] + 1;
                pre[next.x][next.y].x = first.x;
                pre[next.x][next.y].y = first.y;
                vis[next.x][next.y] = false;
                que.push(next);
            }
        }
    }

    return INF;
}

void print_pre(int i, int j)
{
    if(pre[i][j].x == -1)
        return;
    print_pre(pre[i][j].x, pre[i][j].y);
    printf("%ds:(%d,%d)->(%d,%d)\n",value[i][j], pre[i][j].x, pre[i][j].y, i, j);
}

int main()
{
    while(cin >> N >> M)
    {
        for(int i = 0; i < N; i++)
            cin >> pic[i];
        first.x = 0;
        first.y = 0;
        memset(vis, true, sizeof(vis));     // true表示该点未访问,false表示该点已访问
        vis[0][0] = false;
        pre[0][0].x = pre[0][0].y = -1;
        int ans = bfs();
        if(ans == INF)
            printf("sorry,can't out!\n");
        else
        {
            printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans);
            print_pre(N - 1, M - 1);
        }
    }

    return 0;
}

hangd1026题就是这种类型的题目,只是多了一个条件,路上可能会有怪物,每个怪物有N点生命值(1<=n<=9),要花费n秒杀死这个怪物。

题目链接

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int INF = 100005;
const int MAX_N = 105;
struct Node
{
    int x;
    int y;
    int num;
    bool operator<(const Node &a) const
    {
        return num > a.num;
    }
}first, nextNode;
struct Point
{
    int x;
    int y;
}pre[MAX_N][MAX_N];
char pic[MAX_N][MAX_N];
int value[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];
int N, M;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int bfs()
{
    priority_queue<Node> pque;
    pque.push(first);
    while(pque.size())
    {
        first = pque.top();
        pque.pop();
        if(first.x == N - 1 && first.y == M - 1)
            return value[N - 1][M - 1];
        for(int i = 0; i < 4; i++)
        {
            nextNode.x = first.x + dx[i];
            nextNode.y = first.y + dy[i];
            if(nextNode.x >= 0 && nextNode.x < N && nextNode.y >= 0 && nextNode.y < M && pic[nextNode.x][nextNode.y] != 'X' && vis[nextNode.x][nextNode.y])
            {
                if(pic[nextNode.x][nextNode.y] == '.')
                {
                    nextNode.num = first.num + 1;
                }
                else if(isdigit(pic[nextNode.x][nextNode.y]))
                {
                    nextNode.num = first.num + 1 + (int)(pic[nextNode.x][nextNode.y] - '0');
                }
                pre[nextNode.x][nextNode.y].x = first.x;
                pre[nextNode.x][nextNode.y].y = first.y;
                value[nextNode.x][nextNode.y] = nextNode.num;
                vis[nextNode.x][nextNode.y] = false;
                pque.push(nextNode);
            }
        }

    }

    return INF;
}

void print_pre(int i, int j)
{
    if(pre[i][j].x == -1)
        return;
    print_pre(pre[i][j].x, pre[i][j].y);
    if(pic[i][j] == '.')
        printf("%ds:(%d,%d)->(%d,%d)\n",value[i][j], pre[i][j].x, pre[i][j].y, i, j);
    else
    {

        int t = pic[i][j] - '0';
        int kase = value[pre[i][j].x][pre[i][j].y];
        printf("%ds:(%d,%d)->(%d,%d)\n",++kase, pre[i][j].x, pre[i][j].y, i, j);
        for(int k = 0; k < t; k++)
        {
            printf("%ds:FIGHT AT (%d,%d)\n", ++kase, i, j);
        }

    }
}

int main()
{
    while(cin >> N >> M)
    {
        for(int i = 0; i < N; i++)
            cin >> pic[i];
        memset(vis, true, sizeof(vis));
        first.x = 0;
        first.y = 0;
        first.num = 0;
        value[0][0] = 0;
        vis[0][0] = false;
        pre[0][0].x = pre[0][0].y = -1;
        int ans = bfs();
        if(ans != INF)
        {
            printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans);
            print_pre(N - 1, M - 1);
        }
        else
            printf("God please help our poor hero.\n");
        printf("FINISH\n");
    }

    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值