简单的走迷宫的算法,BFS就是把当前的顶点的邻接顶点都入队列,然后队列首端出队列,重复,知道所有的通路,然后一个一个的往回找即可。
入口位置出队列,同时入口的邻接顶点入对列:
队列的顶端元素出对列,然后依次把顶端元素的邻接位置入队列,这里2的邻接顶点为空
然后重复上述情况即可
同时要保存当前顶点的父顶点,保存到edge_to里边,这个数组以顶点的值为索引。最后倒序打印出出口的父顶点即可。
#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
S | 1 | 0 | 1 | G |
1 | 0 | 1 | 1 | 1 |
1 | 0 | 1 | 0 | 0 |
1 | 0 | 1 | 1 | 1 |
1 | 1 | 1 | 0 | 1 |
1 | 1 | 1 | 1 | 1 |
1(S) | 2 | 3 | 4 | 5(G) |
6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 |
S表示起点,G表示终点,0表示障碍,1代表通路。给迷宫的每一格子编号,从1开始到最后30.
队列的出对入队情况是这样的:
入口位置,入队列:
s |
s(de) | 2 | 6 |
s(de) | 2(de) | 6 |
s(de) | 2(de) | 6(de) | 11 |
同时要保存当前顶点的父顶点,保存到edge_to里边,这个数组以顶点的值为索引。最后倒序打印出出口的父顶点即可。
BUG:
在做DFS发现的一个bug,因为这里没有走每一行的行尾的元素,错误没有显现,应该在row_()函数里边加一个if语句,在dfs里边有,不再重复。