学霸的迷宫(广搜)

问题描述:
学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗。但学霸为了不要别人打扰,住在一个城堡里,城堡外面是一个二维的格子迷宫,要进城堡必须得先通过迷宫。因为班长还有妹子要陪,磨刀不误砍柴功,他为了节约时间,从线人那里搞到了迷宫的地图,准备提前计算最短的路线。可是他现在正向妹子解释这件事情,于是就委托你帮他找一条最短的路线。
输入格式
  第一行两个整数n, m,为迷宫的长宽。
  接下来n行,每行m个数,数之间没有间隔,为0或1中的一个。0表示这个格子可以通过,1表示不可以。假设你现在已经在迷宫坐标(1,1)的地方,即左上角,迷宫的出口在(n,m)。每次移动时只能向上下左右4个方向移动到另外一个可以通过的格子里,每次移动算一步。数据保证(1,1),(n,m)可以通过。
输出格式
  第一行一个数为需要的最少步数K。
  第二行K个字符,每个字符∈{U,D,L,R},分别表示上下左右。如果有多条长度相同的最短路径,选择在此表示方法下字典序最小的一个。
样例输入
3 3
001
100
110

3 3
000
000
000
样例输出
4
RDRD

4
DDRR
数据规模和约定
  有20%的数据满足:1<=n,m<=10
  有50%的数据满足:1<=n,m<=50
  有100%的数据满足:1<=n,m<=500。
问题分析:
这题一看就知道是是一个经典的广搜问题,所以说思路已定。由于结果要输出行走的过程,所以要有相应的记录。原先我是执行的逆序过程,即从(n-1,m-1)->(0,0);但是由于题目要求有多解释,按字典序最小的结果。我搞了一会不知道上下左右的优先性,所以又改为了顺序搜索(0,0)->(n-1,m-1);不过在顺序搜索时,只能记录当前可选节点的上一个节点,所以最后输出路径时要倒过来。
代码:

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;

const int MAXN=510;

struct point
{
    char c;
    int prex;
    int prey;

}map[MAXN][MAXN];

struct node
{
    int x;
    int y;
    int step;
};

int v[MAXN][MAXN];
int ans=0,n,m;
queue<node> q;
char s[1000000];
int BFS();

int main()
{
    int i,j;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
            cin>>map[i][j].c;
    }
    memset(v,0,sizeof(v));
    v[0][0]=1;
    ans=BFS();
    cout<<ans<<endl;
    int x=n-1,y=m-1,nx,ny,d=0;
    while(x!=0 || y!=0)
    {

        nx=map[x][y].prex;
        ny=map[x][y].prey;
        if(nx<x && ny==y)
        {
            s[d++]='D';
        }
        else if(nx>x && ny==y)
            s[d++]='U';
        else if(nx==x && ny<y)
            s[d++]='R';
        else
            s[d++]='L';
        x=nx;
        y=ny;
    }
    d--;
    for(i=d;i>=0;i--)
        cout<<s[i];
    return 0;
}

int BFS()
{
    node now,next;
    now.x=0;
    now.y=0;
    now.step=0;
    while(!q.empty())
        q.pop();
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.x==n-1 && now.y==m-1)
        {
            return now.step;
        }
        next.x=now.x+1;next.y=now.y;
        if(next.x>=0 && next.x<=n-1 && next.y>=0 && next.y<=m-1 && map[next.x][next.y].c=='0' && v[next.x][next.y]==0)
        {
            v[next.x][next.y]=1;
            next.step=now.step+1;
            q.push(next);
            map[next.x][next.y].prex=now.x;
            map[next.x][next.y].prey=now.y;

        }
        next.x=now.x;next.y=now.y-1;
        if(next.x>=0 && next.x<=n-1 && next.y>=0 && next.y<=m-1 && map[next.x][next.y].c=='0' && v[next.x][next.y]==0)
        {
             v[next.x][next.y]=1;
            next.step=now.step+1;
            q.push(next);
             map[next.x][next.y].prex=now.x;
            map[next.x][next.y].prey=now.y;
        }
        next.x=now.x;next.y=now.y+1;
        if(next.x>=0 && next.x<=n-1 && next.y>=0 && next.y<=m-1 && map[next.x][next.y].c=='0' && v[next.x][next.y]==0)
        {
             v[next.x][next.y]=1;
            next.step=now.step+1;
            q.push(next);
             map[next.x][next.y].prex=now.x;
            map[next.x][next.y].prey=now.y;
        }
        next.x=now.x-1;next.y=now.y;
        if(next.x>=0 && next.x<=n-1 && next.y>=0 && next.y<=m-1 && map[next.x][next.y].c=='0' && v[next.x][next.y]==0)
        {
            v[next.x][next.y]=1;
            next.step=now.step+1;
            q.push(next);
             map[next.x][next.y].prex=now.x;
            map[next.x][next.y].prey=now.y;
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值