HDOJ2188 Hamilton world round (DFS)

Source: Internet
Author: User

 


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;
}

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.