本文共 2046 字,大约阅读时间需要 6 分钟。
lee最短路算法
The Lee algorithm is one possible solution for maze routing problems. It always gives an optimal solution, if one exists, but is slow and requires large memory for dense layout.
Lee算法是迷宫路由问题的一种可能解决方案。 如果存在的话,它总是提供最佳的解决方案,但是速度很慢,并且需要较大的内存才能进行密集的布局。
The algorithm is a breadth-first
based algorithm that uses queues
to store the steps. It usually uses the following steps:
该算法是基于breadth-first
算法,该算法使用queues
来存储步骤。 它通常使用以下步骤:
C++ has the queue already implemented in the <queue>
library, but if you are using something else you are welcome to implement your own version of queue.
C ++在<queue>
库中已经实现了<queue>
,但是如果您使用其他方法,则欢迎实现自己的队列版本。
int dl[] = {-1, 0, 1, 0}; // these arrays will help you travel in the 4 directions more easilyint dc[] = {0, 1, 0, -1};queue X, Y; // the queues used to get the positions in the matrixX.push(start_x); // initialize the queues with the start positionY.push(start_y);void lee(){ int x, y, xx, yy; while(!X.empty()) // while there are still positions in the queue { x = X.front(); // set the current position y = Y.front(); for(int i = 0; i < 4; i++) { xx = x + dl[i]; // travel in an adiacent cell from the current position yy = y + dc[i]; if('position is valid') //here you should insert whatever conditions should apply for your position (xx, yy) { X.push(xx); // add the position to the queue Y.push(yy); mat[xx][yy] = -1; // you usually mark that you have been to this position in the matrix } } X.pop(); // eliminate the first position, as you have no more use for it Y.pop(); }}
翻译自:
lee最短路算法
转载地址:http://yqrwd.baihongyu.com/