《阿哈!算法》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;
}



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章