《阿哈!算法》4-1不撞南牆不回頭 4-2 解救小哈——深度優先搜索

深度優先搜索關鍵在於解決“當下該如何做”。至於“下一步如何做”則與當下該如何做“是一樣的。

深度優先搜索模型:

void dfs(int step){
    判斷邊界,一般在這裏輸出最終的結果
    嘗試每一種可能for(int i=1;i<=n;i++)
    {
        標記
        繼續下一步dfs(step+1);
        收回標記
    }
    返回
}

通常,需要book數組標記哪些牌已經使用或者記錄格子已經在路徑中(就是被使用)等等。使用過後一定要將book[i]收回

if (book[i]==0) {//將book[i]設爲0,表示i號撲克牌還在手上
            //開始嘗試使用撲克牌
            a[step]=i;  //將i號撲克牌放到第step個盒子中
            book[i]=1;  //將book[i]設爲1,表示i號撲克牌已經不在手上
            
            //第step個盒子已經放好撲克牌,接下來需要走到下一個盒子面前
            dfs(step+1);//通過遞歸處理第step+1個盒子
            book[i]=0;  //非常重要,一定要將剛纔嘗試的撲克牌收回,才能進行下一次嘗試
        }

if (a[tx][ty]==0&&book[tx][ty]==0) {

            book[tx][ty]=1;     //標記這個點已經走過

            dfs(tx, ty, step+1);    //嘗試下一個點

            book[tx][ty]=0;     //嘗試結束,取消這個點的標記

        }

 


4-1 代碼:

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

const  int max=1000;

int a[10],book[10],n;//c語言全局變量在沒有賦值以前默認爲0
void dfs(int step){     //step表示站在第幾個盒子面前
    if(step==n+1){      //如果站在第n+1個盒子面前,則表示前n個盒子已經放好撲克牌
        //輸出一種排列
        for(int i=1;i<=n;i++)
            printf("%d",a[i]);
        printf(" ");
        return;         //return返回之前的一步,最近一次調用dfs函數的地方
    }
    
    //此時站在第step個盒子面前,應該放哪張牌呢?按照1,2,3···n的順序試一下
    for (int i=1; i<=n; i++) {
        //判斷撲克牌i是否還在手上
        if (book[i]==0) {//將book[i]設爲0,表示i號撲克牌還在手上
            //開始嘗試使用撲克牌
            a[step]=i;  //將i號撲克牌放到第step個盒子中
            book[i]=1;  //將book[i]設爲1,表示i號撲克牌已經不在手上
            
            //第step個盒子已經放好撲克牌,接下來需要走到下一個盒子面前
            dfs(step+1);//通過遞歸處理第step+1個盒子
            book[i]=0;  //一定要將剛纔嘗試的撲克牌收回,才能進行下一次嘗試
        }
    }
}
int main() {
    scanf("%d",&n);//輸入n爲1~9之間的整數
    dfs(1);
    printf("\n");
    return 0;
}



運行結果:

4-2代碼:

#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;
}



 

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