Hamilton Bypass World problem (dfs+ record Path)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2181

The question of Hamilton's detour around the world

Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2362 Accepted Submission (s): 1490


Problem description A regular solid 12-face body, its 20 vertices mark the world-famous 20 cities, you start from a city after each city just once back to the departure city.

Line I of the first 20 lines of input has 3 numbers representing 3 cities adjacent to the first city. After line 20th, there are 1 numbers m,m<=20,m>=1.m=0 exit.

Output outputs from the first M cities go through every city 1 times and back to m all routes, if there are multiple routes, output in dictionary order, 1 lines per line. The first output per line is the number of routes. Then one: After listing the cities that pass. See sample output

Sample INPUT2 5 201 3 122 4 103 5 81 4 65 7 196 8 174 7 98 10 163 9 1110 12 152 11 1312 14 2013 15 1811 14 169 15 177 16 1 814 17 196 18 201 13 1950

Sample Output1:5 1 2 3 4 8 7 17 18 14 15 16 9 10 11 12 13 20 19 6 52:5 1 2 3 4 8 9 10 11 12 13 20 19 18 14 15 16 17 7 6 53:5 1 2 3 10 9 16 17 18 14 15 11 12 13 20 19 6 7 8 4 54:5 1 2 3 10 11 12 13 20 19 6 7 17 18 14 15 16 9 8 4 55:5 1 2 12 11 10 3 4 8 9 16 15 14 13 20 19 18 17 7 6 56:5 1 2 12 11 15 14 13 20 19 18 17 16 9 10 3 4 8 7 6 57:5 1 2 12 11 15 16 9 10 3 4 8 7 17 18 14 13 20 19 6 58:5 1 2 12 11 15 16 17 18 14 13 20 19 6 7 8 9 10 3 4 59:5 1 2 12 13 20 19 6 7 8 9 16 17 18 14 15 11 10 3 4 510:5 1 2 12 13 20 19 18 14 15 11 10 3 4 8 9 16 17 7 6 511:5 1 20 13 12 2 3 4 8 7 17 16 9 10 11 15 14 18 19 6 512:5 1 20 13 12 2 3 10 11 15 14 18 19 6 7 17 16 9 8 4 513:5 1 20 13 14 15 11 12 2 3 10 9 16 17 18 19 6 7 8 4 5 14:5 1 20 13 14 15 16 9 10 11 12 2 3 4 8 7 17 18 19 6 515:5 1 20 13 14 15 16 17 18 19 6 7 8 9 10 11 12 2 3 4 516:5 1 20 13 14 18 19 6 7 17 16 15 11 12 2 3 10 9 8 4 517:5 1 20 19 6 7 8 9 10 11 15 16 17 18 14 13 12 2 3 4 518:5 1 20 19 6 7 17 18 14 13 12 2 3 10 11 15 16 9 8 4 519:5 1 20 19 18 14 13 12 2 3 4 8 9 10 11 15 16 17 7 6 520:5 1 20 19 18 17 16 9 10 11 15 14 13 12 2 3 4 8 7 6 521:5 4 3 2 1 20 13 12 11 10 9 8 7 17 16 15 14 18 19 6 522:5 4 3 2 1 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 523:5 4 3 2 12 11 10 9 8 7 6 19 18 17 16 15 14 13 20 1 524:5 4 3 2 12 13 14 18 17 16 15 11 10 9 8 7 6 19 20 1 525:5 4 3 10 9 8 7 6 19 20 13 14 18 17 16 15 11 12 2 1 526:5 4 3 10 9 8 7 17 16 15 11 12 2 1 20 13 14 18 19 6 527:5 4 3 10 11 12 2 1 20 13 14 15 16 9 8 7 17 18 19 6 528:5 4 3 10 11 15 14 13 12 2 1 20 19 18 17 16 9 8 7 6 529:5 4 3 10 11 15 14 18 17 16 9 8 7 6 19 20 13 12 2 1 530:5 4 3 10 11 15 16 9 8 7 17 18 14 13 12 2 1 20 19 6 531:5 4 8 7 6 19 18 17 16 9 10 3 2 12 11 15 14 1 3 20 1 532:5 4 8 7 6 19 20 13 12 11 15 14 18 17 16 9 10 3 2 1 533:5 4 8 7 17 16 9 10 3 2 1 20 13 12 11 15 14 18 19 6 534 : 5 4 8 7 17 18 14 13 12 11 15 16 9 10 3 2 1 20 19 6 535:5 4 8 9 10 3 2 1 20 19 18 14 13 12 11 15 16 17 7 6 536:5 4 8 9 10 3 2 12 11 15 16 17 76 19 18 14 13 20 1 537:5 4 8 9 16 15 11 10 3 2 12 13 14 18 17 7 6 19 20 1 538:5 4 8 9 16 15 14 13 12 11 10 3 2 1 20 19 1 8 17 7 6 539:5 4 8 9 16 15 14 18 17 7 6 19 20 13 12 11 10 3 2 1 540:5 4 8 9 16 17 7 6 19 18 14 15 11 10 3 2 12 13 20 1 5 41:5 6 7 8 4 3 2 12 13 14 15 11 10 9 16 17 18 19 20 1 542:5 6 7 8 4 3 10 9 16 17 18 19 20 13 14 15 11 12 2 1 543:5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 544:5 6 7 8 9 16 17 18 19 20 1 2 12 13 14 15 11 10 3 4 545:5 6 7 17 16 9 8 4 3 10 11 15 14 18 19 20 13 12 2 1 546:5 6 7 17 16 15 11 10 9 8 4 3 2 12 13 14 18 19 20 1 547:5 6 7 17 16 15 11 12 13 14 18 19 20 1 2 3 10 9 8 4 548:5 6 7 17 16 15 14 18 19 20 13 12 11 10 9 8 4 3 2 1 549:5 6 7 17 18 19 20 1 2 3 10 11 12 13 14 15 16 9 8 4 550:5 6 7 17 18 19 20 13 14 15 16 9 8 4 3 10 11 12 2 1 551:5 6 19 18 14 13 20 1 2 12 11 15 16 17 7 8 9 10 3 4 552:5 6 19 18 14 15 11 10 9 16 17 7 8 4 3 2 12 13 20 1 553:5 6 19 18 14 15 11 12 13 20 1 2 3 10 9 16 17 7 8 4 554: 5 6 19 18 14 15 16 17 78 9 10 11 12 13 20 1 2 3 4 555:5 6 19 18 17 7 8 4 3 2 12 11 10 9 16 15 14 13 20 1 556:5 6 19 18 17 7 8 9 16 15 14 13 20 1 2 12 11 10 3 4 557:5 6 19 20 1 2 3 10 9 16 15 11 12 13 14 18 17 7 8 4 558:5 6 19 20 1 2 12 13 14 18 17 7 8 9 16 15 11 10 3 4 559:5 6 19 20 13 12 11 10 9 16 15 14 18 17 7 8 4 3 2 1 560:5 6 19 20 13 14 18 17 7 8 4 3 10 9 16 15 11 12 2 1 5 : DFS record path to use test instructions skillfully, this problem gives a fixed number of points, then the statistics of the time direct for (int i = 0; i <; i++) can record 20 points of the father, if you do not know how the path on the last point, Then, by other features to judge the end of the regression from the parent node and then the dictionary order, the matrix storage edge is the best way to automatically use the dictionary order
1#include <cstdio>2#include <cstring>3#include <string>4#include <iostream>5#include <algorithm>6 using namespacestd;7 #defineN 258 intMp[n][n];9 BOOLVis[n];Ten intFa[n]; One intAns[n]; A voidDfsintTmintSintSumint&CNT) - { -     if(sum== - ){ the             if(!mp[s][tm])return; -printf"%d:", CNT); -         intf =s; -         //int c = 1; +          for(inti =1; i< -; i++){ -Ans[i] =Fa[f]; +f =Fa[f]; A         } at          for(inti = +; i >0; i--){ -printf"%d", Ans[i]); -         } -printf"%d%d\n", S,TM); -CNT = cnt+1; -         return; in     } -      for(inti =1; I <= -; i++){ to         if(!vis[i]&&Mp[s][i]) { +                //puts ("haha"); -Vis[i] =1; theFa[i] =s; *DFS (tm,i,sum+1, CNT); $Vis[i] =0;Panax Notoginseng         } -     } the     return; + } A intMain () the { +Memset (MP,0,sizeof(MP)); -      for(inti =1; I <= -; i++) $     { $         intx, y, Z; -scanf"%d%d%d",&x,&y,&z); -MP[I][X] = Mp[x][i] =1; theMp[i][y] = Mp[y][i] =1; -MP[I][Z] = Mp[z][i] =1;Wuyi     } the     ints; -      while(~SCANF ("%d",&s)) Wu     { -         if(s==0)return 0; Aboutmemset (Vis,0,sizeof(Vis)); $memset (FA,0,sizeof(FA)); -memset (ans,0,sizeof(ans)); -         intCNT =1; -Vis[s] =1; AFa[s] =s; +DFS (S,s,1, CNT); the     } -     return 0; $}

Hamiltonian bypass World problem (dfs+ record path)

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.