迷宫问题 BFS入门水题

1102:迷宫问题

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:84

解决: 41

题目描述

小明置身于一个迷宫,请你帮小明找出从起点到终点的最短路程。

小明只能向上下左右四个方向移动。

输入格式

输入包含多组测试数据。输入的第一行是一个整数T,表示有T组测试数据。

每组输入的第一行是两个整数N和M(1<=N,M<=100)。

接下来N行,每行输入M个字符,每个字符表示迷宫中的一个小方格。

字符的含义如下:

‘S’:起点

‘E’:终点

‘-’:空地,可以通过

‘#’:障碍,无法通过

输入数据保证有且仅有一个起点和终点。

输出

对于每组输入,输出从起点到终点的最短路程,如果不存在从起点到终点的路,则输出-1。

样例输入

1

5 5

S-###

-----

##---

E#---

---##

样例输出

9

#include<stdio.h>
#include<iostream>
#include<queue>
#define maxn 105
using namespace std;
    int t;
    int m,n;
    int sx,sy;//起点
    int ex,ey;//终点
    char map[maxn][maxn]; //存图
    int vis[maxn][maxn];//判断是否遍历过
    int dist[4][2]={{-1,0},{0,-1},{1,0},{0,1}};//四个方向
    int ans;
struct  node
{
    int x;
    int y;
    int step;
};
bool check(int x,int y)
{
    if(x>=0&&x<m&&y>=0&&y<n&&map[x][y]!='#'&&vis[x][y]==0)
        return true;
    return false;
}
void bfs()
{
    queue<node> q;
    node a;
    node next;
    a.x=sx;
    a.y=sy;
    a.step=0;
    vis[a.x][a.y]=1;
    q.push(a);       //始点入列
    while(!q.empty())//队列非空
    {
        a=q.front();//取首元素
        q.pop();<span style="font-family:'Lucida Console';">    //取出后删除首元素</span>
        for(int i=0;i<4;i++)//向四个方向遍历
    {
        next=a;
        next.x+=dist[i][0];
        next.y+=dist[i][1];
        next.step=a.step+1;
        if(next.x==ex&next.y==ey)<span style="font-family:'Lucida Console';">//判断是否是终点</span>
        {
            ans=next.step;
            return ;
        }
        if(check(next.x,next.y))//遍历标记,且入列
        {
            vis[next.x][next.y]=1;
            q.push(next);
        }

    }
    }
      ans=-1;//<span style="font-family:'Lucida Console';">死路</span>

}
int main()
{

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        for(int i=0;i<m;i++)

        {
            scanf("%s",&map[i]);
            for(int j=0;j<n;j++)
            {
                if(map[i][j]=='S')//得起点坐标
                {
                    sx=i;
                    sy=j;
                }
                 if(map[i][j]=='E')//得终点坐标
                 {
                     ex=i;
                     ey=j;
                 }
            }

        }
        bfs();
        printf("%d\n",ans);

    }
    return 0;

}

迷宫问题 BFS入门水题

时间: 2024-12-07 21:09:27

迷宫问题 BFS入门水题的相关文章

poj2387-Til the Cows Come Home dijkstra入门水题

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29539   Accepted: 9927 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ 3369 Meteor Shower (BFS,水题)

题意:给定 n 个炸弹的坐标和爆炸时间,问你能不能逃出去.如果能输出最短时间. 析:其实这个题并不难,只是当时没读懂,后来读懂后,很容易就AC了. 主要思路是这样的,先标记所有的炸弹的位置,和时间,在数组中标记就好,只要赋值给它的爆炸时间就好,注意如果有多个,要赋值最小的那个, 然后用BFS走就行了. 代码如下: #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #

poj3254 状压dp 每行独立 入门水题

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18789   Accepted: 9880 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yum

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

hdu1240 bfs 水题

原题链接 思路:水题,直接搜 1 #include "map" 2 #include "queue" 3 #include "math.h" 4 #include "stdio.h" 5 #include "string.h" 6 #include "iostream" 7 #include "algorithm" 8 #define abs(x) x > 0

4.7-4.9补题+水题+高维前缀和

题目链接:51nod 1718 Cos的多项式  [数学] 题解: 2cosx=2cosx 2cos2x=(2cosx)^2-2 2cos3x=(2cosx)^3-3*(2cosx) 数归证明2cos(nx)能表示成关于2cosx的多项式,设为f(n) f(1)=x,f(2)=x^2-2(其中的x就是2cosx) 假设n=1~k时均成立(k>=3) 当n=k+1时 由cos((k+1)x)=cos(kx)cos(x)-sin(kx)sin(x) cos((k-1)x)=cos(kx)cos(x)

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

hdu 1728 逃离迷宫 (BFS)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14376    Accepted Submission(s): 3458 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

poj 1002:487-3279(水题,提高题 / hash)

487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 236746   Accepted: 41288 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phras