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

来源:互联网 发布:太原java培训机构 编辑:程序博客网 时间:2024/06/07 23:21

最少步数

时间限制: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)分别表示起点的行、列,终点的行、列。
输出
输出最少走几步。
样例输入
23 1  5 73 1  6 7
样例输出
1211






#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;
}





















































 
原创粉丝点击