首页 > 代码库 > dfs

dfs

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. using namespace std;
  7. #define INF 10000000
  8. int start;
  9. int over;
  10. bool sign[9];
  11. int NUM;
  12. int path[9][9];
  13. int road[9];
  14. void input()
  15. {
  16. path[0][0] = INF; path[0][1] = INF; path[0][2] = 10; path[0][3] = INF; path[0][4] = 30; path[0][5] = 100; path[0][6] = INF; path[0][7] = INF; path[0][8] = INF;
  17. path[1][0] = INF; path[1][1] = INF; path[1][2] = 5; path[1][3] = INF; path[1][4] = INF; path[1][5] = INF; path[1][6] = INF; path[1][7] = INF; path[1][8] = INF;
  18. path[2][0] = INF; path[2][1] = INF; path[2][2] = INF; path[2][3] = 50; path[2][4] = INF; path[2][5] = INF; path[2][6] = INF; path[2][7] = INF; path[2][8] = INF;
  19. path[3][0] = INF; path[3][1] = INF; path[3][2] = INF; path[3][3] = INF; path[3][4] = INF; path[3][5] = 10; path[3][6] = INF; path[3][7] = INF; path[3][8] = INF;
  20. path[4][0] = INF; path[4][1] = INF; path[4][2] = INF; path[4][3] = 20; path[4][4] = INF; path[4][5] = 60; path[4][6] = INF; path[4][7] = INF; path[4][8] = INF;
  21. path[5][0] = INF; path[5][1] = INF; path[5][2] = INF; path[5][3] = INF; path[5][4] = INF; path[5][5] = INF; path[5][6] = INF; path[5][7] = INF; path[5][8] = INF;
  22. path[6][0] = INF; path[6][1] = INF; path[6][2] = INF; path[6][3] = INF; path[6][4] = INF; path[6][5] = INF; path[6][6] = INF; path[6][7] = INF; path[6][8] = INF;
  23. path[7][0] = INF; path[7][1] = INF; path[7][2] = INF; path[7][3] = INF; path[7][4] = INF; path[7][5] = INF; path[7][6] = INF; path[7][7] = INF; path[7][8] = INF;
  24. path[8][0] = INF; path[8][1] = INF; path[8][2] = INF; path[8][3] = INF; path[8][4] = INF; path[8][5] = INF; path[8][6] = INF; path[8][7] = INF; path[8][8] = INF;
  25. }
  26. void init()
  27. {
  28. memset(road, 0, sizeof(road));
  29. NUM = 9;
  30. for(int i=0; i<NUM; i++)
  31. sign[i] =false;
  32. input();
  33. }
  34. void findRoad(int road[], int length)
  35. {
  36. for(int i=0; i<length; i++)
  37. cout << " " << road[i];
  38. cout << over << endl;
  39. }
  40. void dfs(int tp, int road[], int length)
  41. {
  42. sign[tp] = true;
  43. for(int i=0; i<NUM; i++)
  44. {
  45. if(path[tp][i] != INF && sign[i] != true)
  46. {
  47. if(tp == over)
  48. {
  49. //road[length++] = over;
  50. findRoad(road, length);
  51. }
  52. else
  53. {
  54. road[length++] = tp;
  55. // for(int i=0; i<6; i++)
  56. dfs(i, road, length);
  57. sign[i] = false;
  58. length--;
  59. }
  60. }
  61. }
  62. }
  63. int main()
  64. {
  65. init();
  66. //freopen("read.txt", "r", stdin);
  67. start = 0; over = 3;
  68. dfs(start, road, 0);
  69. cout << "ok" << endl;
  70. }



来自为知笔记(Wiz)


附件列表

     

    dfs