Solutions DFS maze (output path) hdoj1010

#include <the iostream> 
#include <Stack> 
#include <stdio.h> 
the using namespace STD; 

int Maze [10] [10]; 
int VIS [10] [10]; 
int n-, m; 

int the dir [. 4] [ 2] = {{0,1}, {0, -1}, {1,0}, {- 1,0}}; 

struct Point 
{ 
    int X, Y; 
} P; 

Stack <Point> path, TEMP; 

COUNT int; 

void DFS (int X, Y int) 
{ 
    IF (X == Y == n--&&. 1. 1-m) // --- following successful processing path issues 
	{ 
		COUT << "****** ************ path "COUNT << << ++" ****************** "<< endl; 
		the while (path!. empty ()) // point inside the path taken out, placed inside temp 
		{// path from the stack - the direction of the bottom of the stack, the path is from the end - the order starting from 
			point P1 = path.top (); 
			path. POP (); 
			TEMP.push(p1);
		}
		(! temp.empty ()) while 
		path inside temp {// output, which is all from start to end sequence 
			Point P1 = temp.top (); 
			temp.pop (); 
			path.push (P1); / / back path inside the path, because the back traces back !!! 
			COUT << "(" << P1.x << "," << << p1.y ")" << endl; 
		} 
		return; 
	} 

	IF (X <0 || X> = Y n-|| <|| 0 Y> = m) 
        return; 

    for (int I = 0; I <. 4; I ++) // detected from the four directions 
	{ 
		int X = NX the dir + [I] [0]; 
		int NY + Y = the dir [I] [. 1]; // NX, NY: select a direction, then further before new coordinate 
		if (0 <= nx && nx <n && 0 <= && NY NY <m && Maze [NX] [NY] == 0 && VIS [NX] [NY] == 0) 
		{// condition: nx, ny is not out of bounds, maze [nx] [ny] = 0 the point is not an obstacle may take, vis [nx] [ny] = 0 Description (nx,ny) is not accessed, access 

			vis [nx] [ny] = 1; // set visited 
			PX = NX; 
			Py = NY; 
			path.push (the p-); // let the current point push 

			dfs (nx, ny); // further probe

			vis [nx] [ny] = 0; // back 
			path.pop (); // backtracking because it is, so the current point belongs to the return point, the stack need 
		} 
	} 

} 

int main () 
{ 
    COUNT = 0; 
    The freopen ( "in.txt", "R & lt", stdin); 

    PX = 0; 
    Py = 0; 
    path.push (P); 

    m >> n-CIN >>; 
    for (int I = 0; I <n-; I ++) 
    { 
        for (int J = 0; J <m; J ++) 
        { 
            VIS [I] [J] = 0; 
            CIN >> Maze [I] [J]; 
        } 
    } 
    DFS (0,0); 
    return 0; 

}

General Task: 1, determined feasible, the second output feasible paths.

In.txt comes as a maze.

5 6
0 1 0 0 0 1
0 0 0 1 0 0
1 0 1 0 0 1
1 0 0 1 0 1
1 1 0 0 0 0

to sum up:

1. The basic usage of the stack as follows:

Header file: #include <stack>

Defined stack: stack <point> path;

Stack element point p: path.push (x);

The top element from the stack: path.pop ();

Get the top element: point p1 = path.top ();

Analyzing stack empty: path.empty ();

 

2. There are two reasons a stack trace path to be used when two stacks:

First, the time to reach the end, all the elements need to be output, after the output element also needs to be put back in the stack path, because after the end of this recursion, backtracking even if the stack is empty, there is no way back when so the need for a temporary stack temp, so that all the elements of the stack, and then back into the path.

Second, since the path from the stack to the bottom of the stack, a reverse path into the stack requires a temporary one in the stack temp, temp path in the correct order from the stack, this is the origin to the destination from the.

 

So that the need pruning.

1. Parity pruning. T seconds went to said path of any length with a certain distance and Manhattan parity.

2. little pruning. If a point can go <= T, the path must not exist.

Guess you like

Origin www.cnblogs.com/blackprience/p/11130481.html