广度优先搜索--BFS

广度优先搜索

要求

迷宫另外一种解法

求迷宫中从坐标(0, 0)到(3,2)最少步数?

0代表路,1代表障碍物,2标识走过的路径,3代表目标点。

思路

将每走一步的下一步可能的值依次加入队列,在从队列中取出走下一步,重复以上动作,直到找到目标。

代码

#include <stdio.h>

#define N 5
#define M 4

int a[N][M] = {
                {0, 0,  1, 0},
                {0, 0,  0, 0},
                {0, 0,  1, 0},
                {0, 1,  3, 0},
                {0, 0,  0, 1}
               };

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

struct node que[2501];  // 地图大小不超过50 * 50
int head = 0, tail = 0;
int book[N][M] = {0};

void enqueue(struct node p) {
    que[tail] = p;
    tail++;
}

struct node dequeue(void) {
    head++;
    return que[head-1];
}

int is_empty(void) {
    return head == tail;
}

void print_a() {
    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < M; ++j)
        {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

void print_que() {
    for (int i = head; i < tail; ++i) {
        struct node p = que[i];
        printf("(%d, %d, %d) ", p.x, p.y, p.s);
    }
    printf("\n");
}

int main() {

    int step = 0;
    struct node p = {0, 0, 1};
    enqueue(p);
    a[p.x][p.y] = 2;

    while(!is_empty()) {
        print_que();

        p = dequeue();
        printf("(%d %d %d) \n", p.x, p.y, p.s);

        if ( (a[p.x][p.y+1] == 0 || a[p.x][p.y+1] == 3) && a[p.x][p.y+1] != 2 && p.y+1 < M ){
            printf("right \n");
            if (a[p.x][p.y+1] == 3){
                printf("------------ %d\n", p.s);
                break;
            }
            struct node k = {p.x, p.y+1, que[head-1].s + 1};
            enqueue(k);
            a[p.x][p.y+1] = 2;
        }

        if ( (a[p.x+1][p.y] == 0 || a[p.x+1][p.y] == 3) && a[p.x+1][p.y] != 2 && p.x+1 < N ){
            printf("down \n");
            if (a[p.x+1][p.y] == 3) {
                printf("------------ %d\n", p.s);
                break;
            }
            struct node k = {p.x+1, p.y, que[head-1].s + 1};
            enqueue(k);
            a[p.x+1][p.y] = 2;
        }

        if ( (a[p.x][p.y-1] == 0 || a[p.x][p.y-1] == 3) && a[p.x][p.y-1] != 2 && p.y-1 >= 0 ){
            printf("left \n");
            if (a[p.x][p.y-1] == 3) {
                printf("------------ %d\n", p.s);
                break;
            }
            struct node k = {p.x, p.y-1, que[head-1].s + 1};
            enqueue(k);
            a[p.x][p.y-1] = 2;
        }

        if ( (a[p.x-1][p.y] == 0 || a[p.x-1][p.y] == 3) && a[p.x-1][p.y] != 2 && p.x-1 >= 0 ){
            printf("up \n");
            if (a[p.x-1][p.y] == 3) {
                printf("------------ %d\n", p.s);
                break;
            }
            struct node k = {p.x-1, p.y, que[head-1].s + 1};
            enqueue(k);
            a[p.x-1][p.y] = 2;
        }
        print_a();
    }
    printf("--- end --\n");
    return 0;
}
python 代码
N = 5
M = 4

a = [[0, 0,  1, 0],
     [0, 0,  0, 0],
     [0, 0,  1, 0],
     [0, 1,  3, 0],
     [0, 0,  0, 1]]

q = []


def mprint(x):
    for i in range(N):
        print(x[i])


def bfs(i, j, step):
    q.append((i, j, step+1))
    a[i][j] = 2

    while len(q) != 0:
        print(q)
        print("(%d %d %d)" % q[0])

        i, j, step = q.pop(0)

        if j+1 < M and (a[i][j+1] == 0 or a[i][j+1] == 3):
            print("right")
            if a[i][j+1] == 3:
                print("--------------- %d" % step)
                break
            a[i][j+1] = 2
            q.append((i, j+1, step+1))

        if i+1 < N and (a[i+1][j] == 0 or a[i+1][j] == 3):
            print("down")
            if a[i+1][j] == 3:
                print("--------------- %d" % step)
                break
            a[i+1][j] = 2
            q.append((i+1, j, step+1))

        if j-1 >= 0 and (a[i][j-1] == 0 or a[i][j-1] == 3):
            print("left")
            if a[i][j-1] == 3:
                print("--------------- %d" % step)
                break
            a[i][j-1] = 2
            q.append((i, j-1, step+1))

        if i-1 >= 0 and (a[i-1][j] == 0 or a[i-1][j] == 3):
            print("up")
            if a[i-1][j] == 3:
                print("--------------- %d" % step)
                break
            a[i-1][j] = 2
            q.append((i-1, j, step+1))

        mprint(a)

bfs(0, 0, 0)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值