走迷宫(广搜做法)

走迷宫

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 94   Solved: 83
[ Submit][ Status][ Web Board]

Description

X身在一个M*N大小的迷宫中,迷宫中的障碍物是不能通行的。迷宫中用S代表X,用E代表出口,#代表障碍物,*代表可以通行的道路。X只可以向上、下、左、右四个方向行走,并且每一步只能走一个单位的长度。现给定M,N以及迷宫的样子,求解X走到出口的过程中最少需要改变行走方向几次?如果X无法到达出口就输出 -1,否则输出需要的最小改变方向次数,X会尽量走改变方向较少的路线,只要相邻两次行走方向不一样就算改变方向一次。 (1<=M,N<=100)

Input

有多组输入数据,每组第一行输入两个正整数M和N,
接下来输入M*N的迷宫,迷宫元素只包含S、E、*、#四种字符。
输入以文件为结束(EOF)。

Output

对于每组数据,如果X能从S能到达出口E,输出最少改变方向次数,否则输出-1,每组输出占一行。

Sample Input

5 5
S****
####*
*****
*####
****E

Sample Output

4

HINT

因为障碍物(#)不能通行,所以S只能通过*到达E,最少需要转弯4次

//走迷宫广搜做法,跟深搜大同小异只不过广搜比深搜快一点搜索题有时候深搜过不去超时就换广搜

d是方向数组下标改变方向了d就不同于i所以sum++,总之是到很水很基础的搜索题

# include <iostream>
# include <cstring>
# include <queue>
using namespace std;
 
char str[105][105];
 
int sx, sy, px, py;
struct node
{
    int x, y, d;
    int sum;
};
int f[105][105];
int n , m;
queue<struct node> q;
int flag = 0;
int t = 0x3f3f3f3f;
int dis[][2] = {0, 1, 0, -1, 1, 0, -1, 0};
void bfs(int x, int y)
{
    struct node temp,head;
    temp.x = x;
    temp.y = y;
    temp.d = -1;
    temp.sum = 0;
    q.push(temp);
    while(!q.empty())
    {
        temp = q.front();
        head = temp;
   //     cout << "head.x =" << head.x << " head.y = " << head.y << " head.d = " << head.d << " head.sum = " << head.sum << endl;
        q.pop();
        if(temp.x == px && temp.y == py)
        {
            if(t > temp.sum)
            {
                t = temp.sum;
            }
            flag = 1;
        //    cout << temp.sum << endl;
 
        }
        for(int i = 0; i < 4; i++)
        {
            head.x = temp.x + dis[i][0];
            head.y = temp.y + dis[i][1];
 
            if(head.x  < 0 || head.x >= n ||  head.y < 0 ||  head.y >= m|| str[head.x][head.y] != '*' )
            {
                continue;
            }
            if(!f[head.x][head.y])
            {
                f[head.x][head.y] = 1;
                if(head.d == -1)
                {
                    head.d = i;
                    head.sum  = temp.sum;
                    q.push(head);
                }
                else
                {
                    if(head.d != i)
                    {
                        head.d = i;
                        head.sum  = temp.sum + 1;
                        q.push(head);
                    }
                    else
                    {
                        q.push(head);
                    }
                }
            }
        }
    }
 
    return ;
}
 
int main(int argc, char *argv[])
{
    while(cin >> n >> m)
    {
        t = 0x3f3f3f3f;
        flag = 0;
        memset(f, 0,sizeof(f));
        memset(str, 0, sizeof(str));
        for(int i = 0; i < n; i++)
        {
            cin >> str[i];
        }
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(str[i][j] == 'S')
                {
                    sx = i;
                    sy = j;
                    str[i][j] = '*';
                }
                if(str[i][j] == 'E')
                {
                    px = i;
                    py = j;
                    str[i][j] = '*';
                }
            }
        }
        bfs(sx, sy);
        if(!flag)
        {
            cout << -1 << endl;
        }
        else
        {
            cout << t << endl;
        }
 
    }
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值