HDU 2181 哈密顿绕行世界问题

题目链接  http://acm.hdu.edu.cn/showproblem.php?pid=2181

题意是中文,不解释

解法一:使用结构体存

WA原因,递归传递的时候改变了num,导致递归在回溯的时候保存数组出错

代码:

 1 #include <iostream>

 2 #include <stdio.h>

 3 #include <string.h>

 4 #include <stdlib.h>

 5 #include <algorithm>

 6 #include <queue>

 7 using namespace std;

 8 

 9 struct Ball

10 {

11     int a, b, c;

12 }ball[30];

13 bool visit[30];

14 int an[30], sta;

15 int res[30];

16 int lop=0;

17 

18 void DFS(int n, int num)

19 {

20     res[num] = n;

21     int a = ball[n].a, b = ball[n].b, c = ball[n].c;

22     if(num == 19)

23     {

24         if(a==sta || b==sta || c==sta)

25         { 

26            printf("%d:  ", ++lop); 

27             for(int i = 0; i <= 19; i++)

28                 printf("%d ", res[i]);

29             printf("%d\n", sta);

30         }

31         return;

32     }

33     int aa = ++num, bb = num, cc = num;// 因为这里WA了无数发,原来下面是不能使用

34     if(!visit[a])

35     {

36         visit[a] = 1;

37         DFS(ball[n].a, aa);

38         visit[a] = 0;

39     }

40     if(!visit[b])

41     {

42         visit[b] = 1;

43         DFS(b, bb);

44         visit[b] = 0;

45     }

46 

47     if(!visit[c])

48     {

49         visit[c] = 1;

50         DFS(c, cc);

51         visit[c] = 0;

52     }

53 

54 }

55 int main()

56 {

57     //freopen("1.txt", "r", stdin);

58    // freopen("3.txt", "w", stdout);

59     int n;

60     for(int i = 1; i <= 20; i++)

61     {

62         scanf("%d %d %d", &ball[i].a, &ball[i].b, &ball[i].c);

63     }

64     while(scanf("%d", &n) && n)

65     {

66         scanf("%d", &n);

67         sta = n;

68         memset(visit, false, sizeof(visit));

69         visit[n] = 1;

70         lop=0;

71         DFS(n, 0);

72     }

73     return 0;

74 }
View Code

解法二: 使用经典的数组存

代码:

 1 #include <iostream>

 2 #include <stdio.h>

 3 #include <string.h>

 4 using namespace std;

 5 

 6 /**

 7  * map[][] :用来保存城市之间的连通情况

 8  * used[] :用来 保存城市的使用情况

 9  * res[] :用来保存路径

10  * cas :目标城市的标号

11  */

12 const int maxn = 30;

13 bool map[maxn][maxn];

14 bool used[maxn];

15 int res[maxn];

16 int cas;

17 int num = 1;

18 

19 /**

20  * dep :当前访问城市

21  * count :当前访问的城市数

22  */

23 void dfs(int dep, int count)

24 {

25     int i;

26 

27     //第count个所到达的城市是dep

28     res[count] = dep;

29 

30     if (count == 19)   //19步能涉及20个城市

31     {

32         if (map[dep][cas])

33         {

34             printf("%d: ", num++);

35 

36             for (i = 0; i < 20; ++i)

37             {

38                 printf(" %d", res[i]);

39             }

40 

41             printf(" %d", cas);

42             printf("\n");

43         }

44         return;

45     }

46 

47     for (i = 1; i <= 20; ++i)

48     {

49         /**

50          * map[dep][j] :从城市dep到城市j的连通情况

51          */

52         //如果城市dep与城市j连通&&城市j没有使用过

53         if (map[dep][i] && !used[i])

54         {

55             used[i] = true;

56             dfs(i, count + 1);

57             used[i] = false;

58         }

59     }

60 }

61 int main()

62 {

63     //freopen("1.txt", "r", stdin);

64     memset(map, 0, sizeof(map));

65     memset(res, 0, sizeof(res));

66     memset(used, false, sizeof(used));

67     int i;

68     int a, b, c;

69 

70     for (i = 1; i <= 20; ++i)

71     {

72         scanf("%d%d%d", &a, &b, &c);

73         map[i][a] = true;

74         map[i][b] = true;

75         map[i][c] = true;

76     }

77 

78     while (scanf("%d", &cas) &&cas)

79     {

80         used[cas] = true;

81         dfs(cas, 0);

82     }

83 }
View Code

 

你可能感兴趣的:(HDU)