小 � C 最近在研究机器人,他想看看自己的机器人够不够智能,于是他将机器人放在一个 � × � n×m 的迷宫中,

小 C 最近在研究机器人,他想看看自己的机器人够不够智能,于是他将机器人放在一个 n×m 的迷宫中,看看机器人能不能在最短的时间内到达目的地,可是小 C 不知道最短的时间是多少,现在请你帮他算算机器人到达目的地的最短时间是多少?输出一个整数表示机器人到达目的地的最短时间,如果机器人不能到达目的地,输出 −1。如果能到达,输出方案,满足用时最少,且字典序最小。

我的代码

#include<bits/stdc++.h>
using namespace std;
#define MAX 1010
struct node{
    int x,y,step;
}s;
int n,m;
int vis[MAX][MAX];
char mp[MAX][MAX];
int dis[4][2]={1,0,-1,0,0,1,0,-1}; 
int bfs()
{
    queue<node>q;    
    node next,p,e;
    p.x=s.x;
    p.y=s.y;
    p.step=0;
    vis[p.x][p.y]=1;
    q.push(p);
    while(!q.empty())
    {
        e=q.front();
        q.pop();
        if(mp[e.x][e.y]=='T') return e.step;
        for(int i=0;i<4;i++){
            next.x=e.x+dis[i][0];
            next.y=e.y+dis[i][1];
            if(mp[next.x][next.y]=='T') return e.step+1;
            if(mp[next.x][next.y]=='.'&&vis[next.x][next.y]==0&&next.x>0&&next.x<=n&&next.y>0&&next.y<=m)
            { 
                vis[next.x][next.y]=1;
                next.step=e.step+1; 
                q.push(next);
            }
        } 
    }
    return 0;
}
int main()
{
    while(cin>>n>>m)
    {
        memset(vis,0,sizeof(vis));
        memset(mp,0,sizeof(mp));
        for(int i=1;i<=n;i++)
        {
            cin>>mp[i]+1;
            for(int j=1;j<=m;j++)
            {
                if(mp[i][j]=='S')
                {
                    s.x=i;
                    s.y=j;
                }
            }
        }
        int ans=bfs();
        if(ans==0) cout<<"-1"<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}

请问一下如何输出路径啊?谢谢

  • 机器人从入口到出口,在m×n矩阵中依次经过的所有点,即是路径。

  • 最短路径,即是用时最少,也是占位最少的方案。

  • 题目没作要求,我认为用列表或者字符串输出方案路径都可以。但要注意的是,如有相同方案,则输入字典序小的


引用 皆我百晓生 小程序回复内容作答:

要输出路径,你可以在bfs函数中添加一个数组pre[][]来记录每个位置的前驱位置,然后在找到目的地后,通过回溯pre数组来还原路径。具体的修改如下:

#include<bits/stdc++.h>
using namespace std;
#define MAX 1010
struct node{
    int x,y,step;
}s;
int n,m;
int vis[MAX][MAX];
char mp[MAX][MAX];
int dis[4][2]={1,0,-1,0,0,1,0,-1}; 
int pre[MAX][MAX]; // 添加pre数组
int bfs()
{
    queue<node>q;    
    node next,p,e;
    p.x=s.x;
    p.y=s.y;
    p.step=0;
    vis[p.x][p.y]=1;
    q.push(p);
    while(!q.empty())
    {
        e=q.front();
        q.pop();
        if(mp[e.x][e.y]=='T') return e.step;
        for(int i=0;i<4;i++){
            next.x=e.x+dis[i][0];
            next.y=e.y+dis[i][1];
            if(mp[next.x][next.y]=='T') {
                pre[next.x][next.y] = i; // 记录前驱位置
                return e.step+1;
            }
            if(mp[next.x][next.y]=='.'&&vis[next.x][next.y]==0&&next.x>0&&next.x<=n&&next.y>0&&next.y<=m)
            { 
                vis[next.x][next.y]=1;
                next.step=e.step+1; 
                pre[next.x][next.y] = i; // 记录前驱位置
                q.push(next);
            }
        } 
    }
    return 0;
}

void printPath(int x, int y) {
    if (x == s.x && y == s.y) return; // 到达起点,递归结束
    int dir = pre[x][y];
    int nx = x - dis[dir][0];
    int ny = y - dis[dir][1];
    printPath(nx, ny);
    cout << '(' << nx << ',' << ny << ') -> '; // 输出路径
}

int main()
{
    while(cin>>n>>m)
    {
        memset(vis,0,sizeof(vis));
        memset(mp,0,sizeof(mp));
        memset(pre, -1, sizeof(pre)); // 初始化pre数组为-1
        for(int i=1;i<=n;i++)
        {
            cin>>mp[i]+1;
            for(int j=1;j<=m;j++)
            {
                if(mp[i][j]=='S')
                {
                    s.x=i;
                    s.y=j;
                }
            }
        }
        int ans=bfs();
        if(ans==0) cout<<'-1'<<endl;
        else {
            cout << ans << endl;
            printPath(s.x, s.y); // 输出路径
            cout << '(' << s.x << ',' << s.y << ')' << endl; // 输出终点
        }
    }
    return 0;
}

这样修改后,程序会先输出最短时间,然后输出路径,最后输出终点位置。路径的输出是从起点到终点的顺序,每个位置用坐标表示。

【以下回答由 GPT 生成】

如果ans大于等于1,表示机器人可以到达目的地。 可以在原有代码的最后,通过添加以下代码段来输出机器人在迷宫中的最短路径:

#include<bits/stdc++.h>
using namespace std;

if(ans >= 1){
    int cur_x = s.x, cur_y = s.y;
    vector<pair<int, int>> path;
    while(ans--){
        for(int i=0; i<4; i++){
            int next_x = cur_x + dis[i][0];
            int next_y = cur_y + dis[i][1];
            if(mp[next_x][next_y] == 'T'){
                path.push_back({next_x, next_y});
                cur_x = next_x;
                cur_y = next_y;
                break;
            }
        }
    }
    reverse(path.begin(), path.end());
    cout << "路径: ";
    for(auto p: path){
        cout << "(" << p.first << "," << p.second << ") ";
    }
    cout << endl;
}

在输出最短时间之后,这段代码会计算并输出机器人在迷宫中的最短路径,其中path是一个保存坐标位置的向量。 请根据实际情况对代码进行调整,确保在输出路径之前,先要检查是否存在最短路径。



【相关推荐】


  • 你可以看下这个问题的回答https://ask.csdn.net/questions/7675131
  • 除此之外, 这篇博客: c语言写扫雷新手详解中的 (1)我们想要实现一个比较简单的9*9的扫雷,最起码我们需要棋盘,可以进行放雷和扫雷,因为需要两个功能,这里我们尽量让他们的功能独立起来,因此定义两个二维数组,并且我们需要考虑到,如果扫雷的时候,我们需要排查最边上的格子,如果直接进行排查,将会发生越界,因为你只定义了9*9,其实排查边上的格子的时候你已经扫描到了11*11大小的格子,因此定义的时候,我们最好定义一个11*11的棋盘,但是我们只打印9*9的出来,这样就可以防止他越界,示例代码如下: 部分也许能够解决你的问题。

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^