zoukankan      html  css  js  c++  java
  • HDU 2181 哈密顿绕行世界问题【DFS】

    题意:给出一个十二面体,它的每个顶点是一个城市,从一个城市m出发并回到m,输出所有可行的路径

    先把边记录下来,再深搜

     1 #include<iostream>  
     2 #include<cstdio>  
     3 #include<cstring> 
     4 #include <cmath> 
     5 #include<stack>
     6 #include<vector>
     7 #include<map> 
     8 #include<set>
     9 #include<queue> 
    10 #include<algorithm>  
    11 using namespace std;
    12 
    13 typedef long long LL;
    14 const int INF = (1<<30)-1;
    15 const int mod=1000000007;
    16 const int maxn=1000005;
    17 
    18 int g[105][105];
    19 int ans[maxn];
    20 int s,kase,m;
    21 
    22 void dfs(int a){
    23     if(a==m&&s==21){
    24         printf("%d:  ",++kase);
    25         for(int i=0;i<20;i++) printf("%d ",ans[i]);
    26         printf("%d
    ",ans[20]);
    27         return;
    28     }
    29     
    30     for(int i=1;i<=20;i++){
    31         if(g[a][i]){
    32             ans[s++]=i;
    33             g[a][i]=g[i][a]=0;
    34             dfs(i);
    35         //    printf("dfs(%d)
    ",i);
    36             g[a][i]=g[i][a]=1;
    37             s--;
    38         }
    39     }
    40     
    41 }
    42 
    43 
    44 int main(){
    45     memset(g,0,sizeof(g));
    46     for(int i=1;i <= 20; i++){
    47         int a,b,c;
    48         scanf("%d %d %d",&a,&b,&c);
    49         g[i][a]=1;
    50         g[i][b]=1;
    51         g[i][c]=1;
    52     }
    53     while(scanf("%d",&m)!=EOF&&m){
    54         s=1;ans[0]=m;kase=0;
    55         dfs(m);    
    56     }
    57     return 0;
    58 }
    View Code
  • 相关阅读:
    leetcode 78. 子集 JAVA
    leetcode 91. 解码方法 JAVA
    leetcode 75. 颜色分类 JAVA
    leetcode 74 搜索二维矩阵 java
    leetcode 84. 柱状图中最大的矩形 JAVA
    last occurance
    first occurance
    classical binary search
    LC.234.Palindrome Linked List
    LC.142. Linked List Cycle II
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4529533.html
Copyright © 2011-2022 走看看