《阿哈!算法》4-3 解救小哈——广度优先搜索

广度优先搜索:使用队列实现

广度优先搜索代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
const  int max=1000;
struct note{
    int x;      //横坐标
    int y;      //纵坐标
    int f;      //父亲在队列中的编号,如果输出路径
    int s;      //步数
};

int main() {
    int startx,starty,p,q,n,m,flag,tx,ty;
    struct note que[2501];
    int head,tail;
    int a[51][51]={0};  //用来存储地图
    int book[51][51]={0};   //记录哪些点已经在队列中,防止一个点被重复扩展,并全部初始化为0
    int next[4][2]={
        {0,1},          //向右走
        {1,0},          //向下走
        {0,-1},         //向左走
        {-1,0}          //向上走
    };
    
    scanf("%d %d",&n,&m);   //n行m列
    //读入迷宫
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%d",&a[i][j]);
    scanf("%d %d %d %d",&startx,&starty,&p,&q);
    //队列初始化
    head=1;
    tail=1;
    //往队列插入迷宫入口坐标
    que[tail].x=startx;
    que[tail].y=starty;
    que[tail].f=0;
    que[tail].s=0;
    tail++;
    book[startx][starty]=1;
    
    flag=0; //用来标记是否已经到达目标点,0表示还没有达到,1表示达到
    //当队列不为空的时候循环
    while(head<tail){
        //枚举四个方向
        for (int k=0; k<=3; k++) {
            //计算下一个点的坐标
            tx=que[head].x+next[k][0];
            ty=que[head].y+next[k][1];
            //判断是否越界
            if(a[tx][ty]==0&&book[tx][ty]==0){
                //把这个点标记为已经走过
                //注意宽搜每个点只入队一次,所以和深搜不同,无需将book数组还原
                book[tx][ty]=1;
                //插入新的点到队列中
                que[tail].x=tx;
                que[tail].y=ty;
                que[tail].f=head; //因为这个点是从head扩展出来,所以它的父亲是head,求路径用
                que[tail].s=que[head].s+1;  //步数是父亲的步数+1
                tail++;
            }
            //如果到目标点,停止扩展,任务结束,退出循环
            if(tx==p&&ty==q){
                flag=1;
                break;
            }
        }
        if(flag==1)
            break;
        head++;         //当一个点扩展结束后,head++才能对后面的点再进行扩展
    }
    //打印队列中末尾最后一个点的步数
    //tail是指向队列队尾的下一个位置,所以这需要-1
    printf("%d\n",que[tail-1].s);
    return 0;
}



 


深度优先搜索代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>


const  int max=1000;
int p,q,min=999999999,n,m;
int book[51][51],a[51][51];

void dfs(int x,int y,int step){
    int next[4][2]={
        {0,1},          //向右走
        {1,0},          //向下走
        {0,-1},         //向左走
        {-1,0}          //向山走
    };
    int tx,ty;
    //判断是否到达小哈的位置
    if (x==p&&y==q) {
        //更新最小值
        if(step<min)
            min=step;
        return;
    }
    
    //枚举4种走法
    for (int k=0; k<=3; k++) {
        //计算下一个点的坐标
        tx=x+next[k][0];
        ty=y+next[k][1];
        //判断是否越界
        if(tx<1||tx>n||ty<1||ty>m)
            continue;
        //判断该点是否为障碍物或者已经在路径中
        if (a[tx][ty]==0&&book[tx][ty]==0) {
            book[tx][ty]=1;     //标记这个点已经走过
            dfs(tx, ty, step+1);    //尝试下一个点
            book[tx][ty]=0;     //尝试结束,取消这个点的标记
        }
    }
}
int main() {
    int startx,starty;
    scanf("%d %d",&n,&m);   //n行m列
    //读入迷宫
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%d",&a[i][j]);
    //读入起点坐标和终点坐标
    scanf("%d %d %d %d",&startx,&starty,&p,&q);
    //从起点开始搜索
    book[startx][starty]=1; //标记起点已经在路径,防止后面重复走
    dfs(startx, starty, 0);
    
    printf("%d\n",min);
    return 0;
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值