ny 58 最少步数 【dfs || bfs】

本文介绍了一种求解迷宫中两点间最短路径的方法,通过随缘搜索(包括广度优先搜索BFS和深度优先搜索DFS)来找出从起点到终点所需的最少步数。

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

时间限制:3000 ms  |  内存限制:65535 KB

难度:4

输入

第一行输入一个整数n(0<n<=100),表示有n组测试数据;
随后n行,每行有四个整数a,b,c,d(0<=a,b,c,d<=8)分别表示起点的行、列,终点的行、列。

输出

输出最少走几步。

样例输入

2
3 1  5 7
3 1  6 7

样例输出

12
11

来源

[苗栋栋]原创

上传者

苗栋栋

描述

这有一个迷宫,有0~8行和0~8列:

 1,1,1,1,1,1,1,1,1
 1,0,0,1,0,0,1,0,1
 1,0,0,1,1,0,0,0,1
 1,0,1,0,1,1,0,1,1
 1,0,0,0,0,1,0,0,1
 1,1,0,1,0,1,0,0,1
 1,1,0,1,0,1,0,0,1
 1,1,0,1,0,0,0,0,1
 1,1,1,1,1,1,1,1,1

0表示道路,1表示墙。

现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?

(注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从(3,1)到(4,1)。)

思路:随缘搜索!

1)【bfs】:

#include<bits/stdc++.h>
#include<string.h>
#include<queue>
using namespace std;
const int maxn = 10;
int vis[maxn][maxn];
int a[9][9]={
    1,1,1,1,1,1,1,1,1,
    1,0,0,1,0,0,1,0,1,
    1,0,0,1,1,0,0,0,1,
    1,0,1,0,1,1,0,1,1,
    1,0,0,0,0,1,0,0,1,
    1,1,0,1,0,1,0,0,1,
    1,1,0,1,0,1,0,0,1,
    1,1,0,1,0,0,0,0,1,
    1,1,1,1,1,1,1,1,1
};
int x1,y11,x2,y2,ans;
int dir[4][2]={1,0,-1,0,0,1,0,-1};
struct node{
   int x,y,step;
   bool friend operator < (node a,node b)
   {
       return a.step > b.step;
   }
};
bool check(int x,int y)
{
    if(x>=0 && x<=8 && y>=0 && y<=8)
        return true;
    return false;
}
int bfs(int x,int y)
{
    priority_queue<node>que;
    node e1,e2;
    e1.x = x,e1.y = y,e1.step = 0;
    que.push(e1);
    vis[x][y]=1;
    ans=0;
    while(!que.empty())
    {
        e1 = que.top();
        que.pop();
        if(e1.x == x2 && e1.y == y2)
        {
            ans = e1.step;
            break;
        }
        for(int i=0;i<4;i++)
        {
            e2.x = e1.x + dir[i][0];
            e2.y = e1.y + dir[i][1];
            if(a[e2.x][e2.y]==0 && !vis[e2.x][e2.y] && check(e2.x,e2.y))
            {
                e2.step = e1.step + 1;
                que.push(e2);
                vis[e2.x][e2.y]=1;
            }
        }
    }
    if(ans==0)
        cout<<0<<endl;
    else
        cout<<ans<<endl;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        cin>>x1>>y11>>x2>>y2;
        bfs(x1,y11);
    }
    return 0;
}

2) 【dfs】:

#include<bits/stdc++.h>
#include<string.h>
#include<queue>
using namespace std;
const int maxn = 10;
int a[9][9]={
    1,1,1,1,1,1,1,1,1,
    1,0,0,1,0,0,1,0,1,
    1,0,0,1,1,0,0,0,1,
    1,0,1,0,1,1,0,1,1,
    1,0,0,0,0,1,0,0,1,
    1,1,0,1,0,1,0,0,1,
    1,1,0,1,0,1,0,0,1,
    1,1,0,1,0,0,0,0,1,
    1,1,1,1,1,1,1,1,1
};
int x1,y11,x2,y2,ans,sum;
int dir[4][2]={1,0,-1,0,0,1,0,-1};
void dfs(int x,int y,int ans)
{
    if(x == x2 && y == y2)
    {
        if(ans<sum)
            sum=ans;
        return ;
    }
    else
    {
        for(int i=0;i<4;i++)
        {
            int nx = x + dir[i][0];
            int ny = y + dir[i][1];
            if(a[nx][ny]==0 && ans+1 <sum)
            {
                a[nx][ny]=1;
                dfs(nx,ny,ans+1);
                a[nx][ny]=0;
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        ans=0;
        sum=0x3f3f3f3f;
        cin>>x1>>y11>>x2>>y2;
        a[x1][y11]=1;
        dfs(x1,y11,ans);
        cout<<sum<<endl;
        a[x1][y11]=0;
    }
    return 0;
}

 

### 使用DFS算法解迷宫最少步数 #### DFS算法简介 深度优先搜索(DFS)是一种用于遍历或搜索树或图的算法。该方法会尽可能深入地探索每一个分支,直到无法继续为止,之后再回退并尝试下一个可能的方向。 对于迷宫问题而言,在到达终点后要进行回溯,利用回溯找到其他路径,最终得出一个最短路径[^3]。然而需要注意的是,由于DFS的特点是在遇到第一个解决方案时并不立即停止而是继续查找所有可能性,因此它并非总是能够高效地找出最短路径;只有当所有路径都被穷尽比较后才能确认哪条是最优解。 #### 实现方式 为了使用DFS来计算从起点到目标位置所需的最小移动次数,可以采用递归函数配合栈结构来进行模拟: ```cpp #include <iostream> using namespace std; const int MAXN = 10; bool maze[MAXN][MAXN]; int minSteps = INT_MAX, currentStepCount = 0; // 方向数组定义上下左右四个方向 int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; void dfs(int x, int y){ if (x == targetX && y == targetY){ // 到达目的地 if(currentStepCount < minSteps) minSteps = currentStepCount; return ; } for(int i=0;i<4;++i){ int newX=x+dx[i],newY=y+dy[i]; if(newX>=0&&newX<M&&newY>=0&&newY<N&&!maze[newX][newY]){ maze[newX][newY]=true;//标记走过的地方防止重复走 ++currentStepCount; dfs(newX,newY); --currentStepCount; // 回溯操作 maze[newX][newY]=false; // 清除访问标志以便后续路径可用 } } } ``` 此代码片段展示了如何基于给定的地图数据`maze[][]`(其中`true`表示障碍物而`false`为空白区域),以及起始坐标(x,y)调用`dfs()`函数去寻找通往指定的目标坐标的最短距离。注意这里假设了全局变量`targetX`,`targetY`, `M`, 和 `N`已经被正确定义为终点的位置和迷宫尺寸大小。 尽管上述方法确实能解决问题,但在实际应用中通常推荐使用广度优先搜索(BFS)[^2] 来处理此类需,因为BFS可以在首次抵达终点时即刻返回最优解而不必等待整个空间被完全探索完毕。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值