The standard DFS can be easily implemented using the stack in STL.
[Cpp]
/* HDOJ2188
Author: Chen jiairun
2013-04-12
*/
# Include <iostream>
# Include <stack>
Using namespace std;
Int Case;
Stack <int> S;
Int map [22] [5]; // Record Data
Int used [22]; // indicates whether the process has passed
Int start; // record start point
Void DFS (){
Int top, I;
If (S. size ()> 21) // exit if there are more than 21 locations
Return;
If (S. top () = start & (S. size () <21 & S. size ()! = 1) // exit if the start point is not the end point
Return;
If (S. size () = 21 & S. top () = start) {// if an end point is encountered
Int temp [22];
For (I = 21; I> 0; I --){
Temp [I] = S. top ();
S. pop ();
}
// Output
Cout <Case <":";
For (I = 1; I <= 21; I ++ ){
Cout <temp [I];
If (I! = 21)
Cout <"";
}
Cout <endl;
For (I = 1; I <= 21; I ++ ){
S. push (temp [I]);
}
Case ++;
Return;
}
Top = S. top ();
For (I = 1; I <= 3; I ++ ){
If (! Used [map [top] [I]) {
Used [map [top] [I] = 1; // The flag already exists.
S. push (map [top] [I]); // inbound Stack
DFS (); // Recursion
Used [map [top] [I] = 0; // remove the flag
S. pop (); // output Stack
}
}
}
Int main (){
Int I;
// Freopen ("1.txt"," r ", stdin );
While (cin> map [1] [1] & map [1] [1]) {
Cin> map [1] [2]> map [1] [3];
For (I = 2; I <= 20; I ++)
Cin> map [I] [1]> map [I] [2]> map [I] [3];
Cin> start;
For (I = 1; I <= 20; I ++) // Initialization
Used [I] = 0;
S. push (start );
Case = 1;
DFS ();
}
Return 0;
}