[编程题] 地下迷宫 滴滴编程题

小青蛙有一天不小心落入了一个地下迷宫,小青蛙希望用自己仅剩的体力值P跳出这个地下迷宫。为了让问题简单,假设这是一个n*m的格子迷宫,迷宫每个位置为0或者1,0代表这个位置有障碍物,小青蛙达到不了这个位置;1代表小青蛙可以达到的位置。小青蛙初始在(0,0)位置,地下迷宫的出口在(0,m-1)(保证这两个位置都是1,并且保证一定有起点到终点可达的路径),小青蛙在迷宫中水平移动一个单位距离需要消耗1点体力值,向上爬一个单位距离需要消耗3个单位的体力值,向下移动不消耗体力值,当小青蛙的体力值等于0的时候还没有到达出口,小青蛙将无法逃离迷宫。现在需要你帮助小青蛙计算出能否用仅剩的体力值跳出迷宫(即达到(0,m-1)位置)。
输入描述:
输入包括n+1行:
第一行为三个整数n,m(3 <= m,n <= 10),P(1 <= P <= 100)
接下来的n行:
每行m个0或者1,以空格分隔

输出描述:
如果能逃离迷宫,则输出一行体力消耗最小的路径,输出格式见样例所示;如果不能逃离迷宫,则输出”Can not escape!”。 测试数据保证答案唯一

输入例子1:
4 4 10 1 0 0 1 1 1 0 1 0 1 1 1 0 0 1 1

输出例子1:
[0,0],[1,0],[1,1],[2,1],[2,2],[2,3],[1,3],[0,3]

思路:这是一道广度优先遍历的题目。使用队列实现。首先定义一个结构体,包含坐标x,y,p,表示改点的坐标和体力值。 为了实现路径记录,再添加一个志向前一个节点的指针。 输出路径时,沿着链表输出。
代码如下:

#include<iostream>
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;

struct Node
{
    int x;
    int y;
    int p;
    Node *pre;
    Node() :x(0),y(0),p(0),pre(NULL) {}
};

int main()
{
    int n,m,p;
    int mat[11][11];
    cin>>n>>m>>p;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cin>>mat[i][j];
        }
    }

    int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0} };
    queue<Node*> que;
    Node *node = new Node();
    node->x = node->y = 0;
    node->p = p;
    node->pre = nullptr;
    que.push(node);
    while(!que.empty())
    {
        node = que.front();
        que.pop();
        if(node->x == 0 && node->y == m-1)
        {
            break;
        }
        int nx, ny;
        for(int i = 0; i < 4; i++)
        {
            nx = node->x + dir[i][0];
            ny = node->y + dir[i][1];

            if(nx < 0 || nx >= n || ny < 0 || ny >= m || mat[nx][ny] >=2 || mat[nx][ny] == 0) continue;

            Node *tmp = new Node();
            tmp->x = nx;
            tmp->y = ny;
            if(dir[i][0] == 0)
            {
                tmp->p = node->p - 1;
            }
            else if(dir[i][0] == 1)
            {
                tmp->p = node->p;
            }
            else if(dir[i][0] == -1)
            {
                tmp->p = node->p - 3;
            }
            mat[nx][ny] = 2;
            tmp->pre = node;
            que.push(tmp);
        }
    }
    if(node->p < 0) cout<<"Can not escape!"<<endl;
    else
    {

        vector<pair<int,int> > res;
        while(node != NULL)
        {
            pair<int,int> pp = make_pair(node->x,node->y);
            res.push_back(pp);
            node = node->pre;
        }
        for(int i = res.size() - 1; i >= 0; i--)
        {
            cout<<"["<<res[i].first<<","<<res[i].second<<"]";
            if(i > 0) cout<<",";
            else cout<<endl;
        }
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhengjihao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值