基础宽搜 迷宫最少步数问题

最少步数

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 4
描述

这有一个迷宫,有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)。)

输入
第一行输入一个整数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






#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define LL long long
#define INF 0x3f3f3f3f
#define mem(a, b) memset(a, b, sizeof(a))
#define PI 3.1415926
using namespace std;
const int N = 199;


  struct step
  {
      int x;
      int y;
  };

  queue<step> q;

int d[N][N], sx, sy, gx, gy, x[4] = {1, 0, -1, 0}, y[4] = {0, 1, 0, -1};
bool v[N][N];
char Map[199][199] = {{"111111111"},
                      {"100100101"},
                      {"100110001"},
                      {"101011011"},
                      {"100001001"},
                      {"110101001"},
                      {"110101001"},
                      {"110100001"},
                      {"111111111"}};

int bfs()
{
    step a;
    a.x = sx;
    a.y = sy;
    d[a.x][a.y] = 0;
    v[a.x][a.y] = 1;
    q.push(a);
    while(!q.empty())
    {
        a = q.front();
        q.pop();
        if(a.x == gx && a.y == gy)
            return d[gx][gy];
        for(int i = 0; i < 4; i++)
        {
            step n;
            n.x = a.x + x[i];
            n.y = a.y + y[i];
            if(n.x >= 0 && n.x < 9 && n.y >= 0 && n.y < 9 && Map[n.x][n.y] == '0' && !v[n.x][n.y])
            {
                v[n.x][n.y] = true;
                q.push(n);
                d[n.x][n.y] = d[a.x][a.y] + 1;
            }
        }

    }
}

  void vis()
  {
      mem(v, false);
      mem(d, 0);
  }

  int main()
  {
      int t;
      cin >> t;
      while(t--)
      {
          vis();
          cin >> sx >> sy >> gx >> gy;
          cout << bfs() << endl;
      }
  }




#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define LL long long
#define INF 0x3f3f3f3f
#define mem(a, b) memset(a, b, sizeof(a))
#define PI 3.1415926
using namespace std;
const int N = 199999;

typedef pair<int, int> P;
queue<P> que;
int d[1999][1999], x[4] = {1, 0, -1, 0}, y[4] = {0, 1, 0, -1}, sx, sy, gx, gy;\
//用来标记地图是否走过
//记住只要是搜索都应该有此标记数组,一般为bool类型
bool vis[10][10];
char Map[199][199] = {{"111111111"},
                      {"100100101"},
                      {"100110001"},
                      {"101011011"},
                      {"100001001"},
                      {"110101001"},
                      {"110101001"},
                      {"110100001"},
                      {"111111111"}};
int bfs()
{
    queue<P> que;
    que.push(P(sx, sy));
    //这样输入下一组数据的时候呢?你这一步就写死了,不如换做标记数组
//    Map[sx][sy] = '1';
    vis[sx][sy] = true;
    d[sx][sy] = 0;

    //条件换成队列非空
    while(!que.empty())
    {

        //每次都要去掉队列的头部元素
        P p = que.front();
        que.pop();

        if(p.first == gx && p.second == gy)
            return d[gx][gy];
        for(int i = 0; i < 4; i++)
        {
            int nx = p.first + x[i];
            int ny = p.second + y[i];
            if(nx >= 0 && nx < 9 && ny >= 0 && ny < 9 && Map[nx][ny] == '0' && !vis[nx][ny])
            {
                que.push(P(nx, ny));
                vis[nx][ny] = true;
                d[nx][ny] = d[p.first][p.second] + 1;
            }
        }
    }

}

//我添加的程序用来初始化
void init(){
    mem(vis, false);
    mem(d, 0);
}

int main()
{
    //****************debug*******************
//    for(int i = 0; i < 8; ++i){
//        for(int j = 0; j < 8; ++j){
//            cout << Map[i][j] << ' ' ;
//        }
//        cout << endl;
//    }
    //****************end**********************
    //开始应该输入有多少组测试样例
    int t;
    cin >> t;
    while(t--){
        cin >> sx >> sy >> gx >> gy;
        init();
        cout << bfs() << endl;
    }
    return 0;
}





















































 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]和引用\[2\]的描述,我们可以得知这是一个求解迷宫最少步数问题迷宫由R行C列的格子组成,其中有些格子是障碍物不能走,有些是空地可以走。我们需要从左上角走到右下角,求解最少需要走多少步。只能在水平方向或垂直方向走,不能斜着走。 根据引用\[2\]的输入描述,我们可以得到迷宫的行数和列数R和C。接下来是R行,每行C个字符,代表整个迷宫。空地格子用'.'表示,有障碍物的格子用'#'表示。迷宫的左上角和右下角都是'.'。 根据引用\[3\]的输入描述,我们可以得到迷宫的行数和列数n和m。接下来n行,每行一个长为m的字符串,表示整个迷宫的布局。字符'.'表示空地,'#'表示墙,'S'表示起点,'T'表示出口。 要求走出迷宫最少步数,我们可以使用广度优先索算法来解决。从起点开始,依次遍历相邻的空地格子,将其加入队列,并记录步数。直到找到出口为止,返回最少步数。 因为题目中没有给出具体的迷宫布局,所以无法给出具体的最少步数。但是根据算法的思路,我们可以根据给定的迷宫布局进行计算,得出最少步数。 #### 引用[.reference_title] - *1* *2* [【蓝桥杯】2.走出迷宫最少步数——DFS](https://blog.csdn.net/m0_49755093/article/details/122547313)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [追梦算法----走出迷宫最少步数2](https://blog.csdn.net/m0_56501550/article/details/123735127)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值