Multiple Paths 2

Source: Internet
Author: User

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <stack>

using namespace Std;

const int SIZE = 6;

Boundary Array, four directions, in order of bottom, right, top, left
int coordinate[8][2] = {{ -1,0}, { -1,1}, {0,1}, {1,1},{1,0},{1,-1},{0,-1},{-1,-1}};

Stack<int> SX;
Stack<int> Sy;
Stack<int> sxcopy;
Stack<int> sycopy;

int mazebfs[size][size]={
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,1,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0
};

Wide-Search Maze
int mazedfs[size][size]={
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0,
0,0,1,0,0,0,
0,0,0,0,0,0,
0,0,0,0,0,0
}; A maze for deep search

int m = 6;
int n = 6;
int p = 0;
int q = 0;
int r = 5;
int s = 5;
int shortestpathlength; Length of Shortest path
int shortestpahtnumber; Number of bars in shortest path


Wide search for Shortest path length
int BFS ();

Deep search for Shortest path number
void DFS (int x, int y, int len);

int main ()
{
Find the shortest path length
Shortestpathlength = BFS ();
if (shortestpathlength = =-1)//No way to walk
{
printf ("No solution!\n");
}

Find the shortest path number and the shortest path of all output
Shortestpahtnumber = 0;
Sx.push (P);
Sy.push (q);
DFS (p, q, 0);

Output results
printf ("Shortest path length:%d\n\n", shortestpathlength);
printf ("Shortest Path Number:%d\n\n", shortestpahtnumber);
}

int BFS ()
{
Queue<int> QX; Queue with Horizontal axis
Queue<int> qy; The queue in which the ordinate is stored
Queue<int> Qlen; Queue of the remaining length
int xa, ya; Current node coordinates
int length; Reach the current node length

Qx.push (P);
Qy.push (q);
Qlen.push (0);
MAZEBFS[P][Q] = 1;
while (!qx.empty ())
{
if ((Qx.front () ==r) && (Qy.front () ==s)//Determine if mouse B is reached
{
return Qlen.front ();
}

Temporarily save Team Header value
int xx, yy, LL;
xx = Qx.front ();
yy = Qy.front ();
ll = Qlen.front ();

After the save, the team
Qx.pop ();
Qy.pop ();
Qlen.pop ();

for (int i=0; i<8; i++)
{
Calculate the new value in the first direction
Xa = xx + coordinate[i][0];
Ya = yy + coordinate[i][1];
length = ll;

New points in the maze, and no passing
if ((xa>=1) && (xa<=n) && (ya>=1) && (ya<=m) && (mazebfs[xa][ya]==0))
{
Team
Qx.push (XA);
Qy.push (YA);
length + = 1;
Qlen.push (length);

Mark a new Point
Mazebfs[xa][ya] = 1;
}
}
}

return-1; If there is no road, return 0
}

void DFS (int x, int y, int len)
{
if ((x==r) && (y==s) && (len==shortestpathlength))//Find a shortest path
{
shortestpahtnumber++;
return;
}

for (int i=0; i<8; i++)
{
int xx, yy;
xx = x + coordinate[i][0];
yy = y + coordinate[i][1];

if ((xx>=1) && (xx<=n) && (yy>=1) && (yy<=m) && (mazedfs[xx][yy]==0))
{
Sx.push (XX);
Sy.push (yy);
MAZEDFS[XX][YY] = 1;
DFS (xx, yy, len+1);

Backtracking
Sx.pop ();
Sy.pop ();
MAZEDFS[XX][YY] = 0;
}
}
}

Multiple Paths 2

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.