迷宫最短路径算法(使用队列)
来源:互联网 发布:mac不能安装adobe 编辑:程序博客网 时间:2024/04/29 01:26
(上接迷宫递归算法)
顺便也把图里求迷宫最短路径算法贴出来,主要思想是利用队列,采用广度优先搜索法,当第一次出现目的点时,中断搜索,并输出路径。程序还是主要使用的C语言,对于队列操作我又重写了下基本操作代码比如入队、出队等,没办法对C++不熟啊!!
个人认为需要说明的是:
1. 为了记录路径,借鉴了树的双亲表示法,所以队列中的数组元素定义如下
typedef struct ...{
int location; //(x-1)*3+y
int parent; //记录上一个节点即路径中前驱节点,方便最后输出
}QueueNode;
int location; //(x-1)*3+y
int parent; //记录上一个节点即路径中前驱节点,方便最后输出
}QueueNode;
2.仍然采用了上面的映射思想,公式也是 location=(x-1)*3+y
void Maze_Shortest(int maze[][5],point &start,point &des)
...{
queue Queue;
InitializeQueue(Queue);
EnterQueue(Queue,(start.x-1)*5+start.y,Queue.head);
int direction;
bool FindFlag=false;
int dir[4][2]=...{...{1,0},...{0,1},...{-1,0},...{0,-1}};
while (!EmptyQueue(Queue)&&!FindFlag)
...{
int curX=Queue.array[Queue.head].location/3+1;
int curY=Queue.array[Queue.head].location%3;
for (direction=0;direction<4;direction++) //遍历该点四周四个邻接点
...{
int nextX=curX+dir[direction][0];
int nextY=curY+dir[direction][1];
if (!maze[nextX][nextY]) //如果为0 排除了墙和已经踩过的路
...{
EnterQueue(Queue,(nextX-1)*3+nextY,Queue.head); //入队,入队元素是映射后节点!
if (nextX==des.x&&nextY==des.y)
...{
FindFlag=true;
break;
}
}
}
Queue.head++;
}
if (EmptyQueue(Queue))
...{
printf("找不到路径 ");
}
if(FindFlag)
...{
int pre=Queue.array[--Queue.rear].parent;
Queue.array[1].parent=0;
printf("%d<- ",Queue.array[Queue.rear].location);
while (pre) //迭代输出路径,可惜是逆向输出,不过可以用栈解决
...{
printf("%d<- ",Queue.array[pre].location);
pre=Queue.array[pre].parent;
}
}
...{
queue Queue;
InitializeQueue(Queue);
EnterQueue(Queue,(start.x-1)*5+start.y,Queue.head);
int direction;
bool FindFlag=false;
int dir[4][2]=...{...{1,0},...{0,1},...{-1,0},...{0,-1}};
while (!EmptyQueue(Queue)&&!FindFlag)
...{
int curX=Queue.array[Queue.head].location/3+1;
int curY=Queue.array[Queue.head].location%3;
for (direction=0;direction<4;direction++) //遍历该点四周四个邻接点
...{
int nextX=curX+dir[direction][0];
int nextY=curY+dir[direction][1];
if (!maze[nextX][nextY]) //如果为0 排除了墙和已经踩过的路
...{
EnterQueue(Queue,(nextX-1)*3+nextY,Queue.head); //入队,入队元素是映射后节点!
if (nextX==des.x&&nextY==des.y)
...{
FindFlag=true;
break;
}
}
}
Queue.head++;
}
if (EmptyQueue(Queue))
...{
printf("找不到路径 ");
}
if(FindFlag)
...{
int pre=Queue.array[--Queue.rear].parent;
Queue.array[1].parent=0;
printf("%d<- ",Queue.array[Queue.rear].location);
while (pre) //迭代输出路径,可惜是逆向输出,不过可以用栈解决
...{
printf("%d<- ",Queue.array[pre].location);
pre=Queue.array[pre].parent;
}
}
- 迷宫最短路径算法(使用队列)
- 也谈迷宫算法(最短路径 队列)+源程序
- erl_stack_queue-队列求迷宫最短路径
- 小算法系列-迷宫最短路径
- 迷宫的最短路径-BFS算法
- 迷宫最短路径
- 迷宫最短路径
- 迷宫最短路径
- 迷宫-最短路径
- 迷宫最短路径
- poj3984迷宫问题 广搜+最短路径+模拟队列
- 队列应用2:求解迷宫问题,最短路径
- 数据结构_队列_迷宫的最短路径
- 数据结构-顺序队列解决最短迷宫路径问题
- 迷宫的最短路径
- 迷宫的最短路径
- 广搜走迷宫最短路径
- 迷宫的最短路径
- Adobe Flex最佳学习路线
- 迷宫递归算法
- 献给八十年代的我们
- 对过去的一年里,所承担的嵌入式开发任务的回忆
- 了解 Web 服务规范: 第 1 部分:SOAP
- 迷宫最短路径算法(使用队列)
- 雅思复习计划
- “阿里外传”之一:阿里巴巴有只宠物,叫雅虎
- 写点程序以外的事情,最近又打了好几盘魔兽了,终究是自己深深热爱的游戏,而且这几天还在看电视剧一刚!
- PE转换为IMAGE映象工具的源码
- 多线程编程
- SQL中All, Any, Some的意思
- 心情日记
- 关于以后 WEB 开发的一个想法