BFS走迷宫

简单的走迷宫的算法,BFS就是把当前的顶点的邻接顶点都入队列,然后队列首端出队列,重复,知道所有的通路,然后一个一个的往回找即可。
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<string.h>

#define MAXLEN  100

int edge_to[MAXLEN];
int maze[MAXLEN][MAXLEN];
bool visited[MAXLEN][MAXLEN];   // 0 UN-VISITED, 1 VISITED
char str[MAXLEN];
int queue[MAXLEN];
int source = 0;
int goal = 0;
int row, col;

void scanf_maze()
{
    int j, i;

    if(scanf("%d %d", &row, &col) == 2)
    {
        if(row <= 0 || col <= 0)
            return;
        for(i = 1; i <= row; i++)
        {
            scanf("%s", str);
            for(j = 1; j <= col; j++)
            {
                // find the source and the goal;
                if(str[j -1] == 's')
                    source = (i - 1) * col + j;
                else if(str[j - 1] == 'g')
                    goal = (i - 1) * col + j;

                maze[i][j] = str[j-1] - '0';
            }
        }
    }
    
//    for(i = 0; i < row + 2; i++)
//    {
//        for(j = 0; j < col + 2; j ++)
//            printf("%d ", maze[i][j]);
//        putchar('\n');
//    }
}
// return the adjcent vertex satisfied the condition:
// not been visited and not a varrier(a passage).
inline int row_(int num)
{
    return (num / col) + 1;
}
inline int col_(int num)
{
    if(((num % col) == 0) && num != 0)
        return col;
    return num % col;
}
int adj(int cur_vertex, int adj)
{
    switch(adj) // adj = 0 the one upper; adj = 1 the one left; adj = 2 the one right; adj =3 the one down
    {
        case 0: if(maze[row_(cur_vertex)-1][col_(cur_vertex)] && !visited[row_(cur_vertex)-1][col_(cur_vertex)]) return (cur_vertex - col);break;
        case 1: if(maze[row_(cur_vertex)][col_(cur_vertex)-1] && !visited[row_(cur_vertex)][col_(cur_vertex)-1]) return (cur_vertex - 1);break;
        case 2: if(maze[row_(cur_vertex)][col_(cur_vertex)+1] && !visited[row_(cur_vertex)][col_(cur_vertex)+1]) return (cur_vertex + 1);break;
        case 3: if(maze[row_(cur_vertex)+1][col_(cur_vertex)] && !visited[row_(cur_vertex)+1][col_(cur_vertex)]) return (cur_vertex + col);break;
        default: break;
    }
    return -1;
}
void bfs()
{
    int front = 0, rear = 0, i, result;
    
    queue[rear++] = source;
    visited[row_(source)][col_(source)] = 1;    // source vertex has been visited

    while(front < rear)
    {
        for(i = 0; i < 4; i++)
        {
            if((result = adj(queue[front], i)) >= 0)
            {
                edge_to[result] = queue[front];// its parent node
                queue[rear++] = result; // adjcent vertex enqueue
                visited[row_(result)][col_(result)] = 1;
            }
        }
        front++; // current vertex dequeue
    }
}
void path_to(int goal_)
{
    if(edge_to[goal_] == source)
    {
        printf("%d-%d", source, goal_);
        return;
    }

    path_to(edge_to[goal_]);
    printf("-%d", goal_);
}
int main()
{ 
    memset(edge_to, 0, sizeof(edge_to));
    //memset(maze, 1, MAXLEN * MAXLEN * sizeof(int));   // 1 is the passage, 0 is the barriar
    memset(maze, 0, sizeof(maze));
    memset(str, 0, MAXLEN);
    memset(visited, 0, sizeof(visited));

    scanf_maze(); 
    bfs();
    path_to(goal);
    
    putchar('\n');
    return 0;
}

输入输出:

[$] ./maze
6 5
s101g
10111
10100
10111
11101
11111

1-6-11-16-21-22-23-18-13-8-9-4-5

S101G
10111
10100
10111
11101
11111
1(S)2345(G)
678910
1112131415
1617181920
2122232425
2627282930

S表示起点,G表示终点,0表示障碍,1代表通路。给迷宫的每一格子编号,从1开始到最后30.

队列的出对入队情况是这样的:

入口位置,入队列:

s                   
入口位置出队列,同时入口的邻接顶点入对列:

s(de)26                 
队列的顶端元素出对列,然后依次把顶端元素的邻接位置入队列,这里2的邻接顶点为空
s(de)2(de)6                 
然后重复上述情况即可
s(de)2(de)6(de)11                

同时要保存当前顶点的父顶点,保存到edge_to里边,这个数组以顶点的值为索引。最后倒序打印出出口的父顶点即可。


BUG:

在做DFS发现的一个bug,因为这里没有走每一行的行尾的元素,错误没有显现,应该在row_()函数里边加一个if语句,在dfs里边有,不再重复。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值