Comparison of recursive and non-recursive algorithms for depth-first search in Maze addressing

Source: Internet
Author: User

Comparison of recursive and non-recursive algorithms for depth-first search in Maze addressing

Qiao is clumsy (welcome reprint, but please specify Source: Http://blog.csdn.net/qiaoruozhuo )

This paper only explores the comparison of recursive and non-recursive algorithms for depth-first search in maze addressing, and other related codes are described in the maze problem (clumsy) http://blog.csdn.net/qiaoruozhuo/article/details/41020745

The recursive algorithm of depth-first search is very easy to implement, just set a driver function, and then recursively call the child function can be.

The code is as follows:

int Deepsearchway ()//search path: Deep search

{

Copymigong ();

if (c_map[begin[0]][begin[1]] = = OPEN && Search (begin[0],begin[1]))

{

C_MAP[BEGIN[0]][BEGIN[1]] = ROAD;

return true;

}

return false;

}

int search (int x, int y)//Depth Search recursive sub-function

{

Inti

C_map[x][y] = PASSED;

if (Isend (x, y))//Find exit

{

C_map[x][y] = ROAD;

return true;

}

for (i=0; i<4; i++)//Determine whether the current path points are around

{

if (C_map[x+zx[i]][y+zy[i]] ==open && Search (X+zx[i], y+zy[i]))

{

C_map[x][y] = ROAD;

return true;

}

}

return false;

}

The non-recursive algorithm of depth-first search needs to set up a stack manually, store the searched points in the stack, exit the stack at the point of exit, and find out the function of exits.

The code is as follows:

int deepsearchway_2 ()//search path: Deep search

{

Intx, y;

int top = 0; Stack top pointer

int sum = 0;//cumulative number of points searched

Copymigong ();

way[0].x = begin[0];

WAY[0].Y = begin[1];

Way[0].pre = 0;

C_MAP[WAY[0].X][WAY[0].Y] = PASSED; The point has passed

while (top >= 0)

{

if (Way[top].pre < 4)

{

x = way[top].x + Zx[way[top].pre];

y = way[top].y + zy[way[top].pre];

if (c_map[x][y] = = OPEN)//If a direction can be passed, the point is included in the stack

{

sum++;

top++;

way[top].x = x;

Way[top].y = y;

Way[top].pre = 0;

C_map[x][y] = PASSED;

if (Isend (x, y))//Find exit

{

Putstack (top); Display the stack path in the maze

printf ("\ n Depth-first search feasible path, total search for%d points \ n", sum);

return true;

}

}

else//otherwise change direction

way[top].pre++;

}

Else

{

top--;

}

}

return false;

}

void Putstack (int top)//stack path displayed in the maze

{

Copymigong ();

while (top >= 0)

{

C_MAP[WAY[TOP].X][WAY[TOP].Y] = ROAD;

top--;

}

}

The depth-first search shortest path, in addition to storing the stack of the current traversal node, requires an additional stack storage shortest path. To avoid searching for a vertex repeatedly, I set the path length for each vertex, and only the current path length is less than the original path length, the vertex is searched.

After the exit function is not exited, if the current path length is less than the minimum path length, the minimum path length is updated, otherwise the direct fallback stack goes to the previous point and continues the search.

The shortest path is output until all possible paths have been searched.

The code is as follows:

int deepsearchway_3 ()//search Path: Deep search (Shortest path)

{

Intx, Y, I, J;

int top = 0; Stack top pointer

int pathlen[m+2][n+2] = {0};

struct Stype shortway[m*n];

Intflag = false; Whether the marker can reach the end point

Intsum = 0;//cumulative number of points searched

for (x=0; x<m+2; x + +)//Set the initial path length for each point to be the maximum value

for (y=0; y<n+2; y++)

Pathlen[x][y] = MaxLen;

Copymigong ();

Minlen= MaxLen; Shortest path

way[0].x = begin[0];

WAY[0].Y = begin[1];

Way[0].pre = 0;

PATHLEN[BEGIN[0]][BEGIN[1]] = 0;

while (top >= 0)

{

if (Way[top].pre < 4)

{

x = way[top].x + Zx[way[top].pre];

y = way[top].y + zy[way[top].pre];

if (c_map[x][y] = = OPEN && pathlen[x][y] > Top+1)//If a direction is passed and is the shortest path, the point is included in the stack

{

sum++;

top++;

way[top].x = x;

Way[top].y = y;

Way[top].pre = 0;

Pathlen[x][y] = top;

if (Isend (x, y))//Find exit

{

if (Top < Minlen)

{

minlen= top;

for (i=0; i<=top; i++)

{

Shortway[i]= Way[i];

}

}

Flag = true;

top--;

}

}

else//otherwise change direction

way[top].pre++;

}

Else

{

top--;

}

}

if (flag)

{

for (i=0; i<=minlen; i++)

{

Way[i] = Shortway[i];

}

Putstack (Minlen); Display the stack path in the maze

}

printf ("\ n Depth-first search shortest path, total search for%d points \ n", sum);

return flag;

}

Comparison of recursive and non-recursive algorithms for depth-first search in Maze addressing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.