fork(1) download
  1. #include <stdio.h>
  2. typedef struct TNode TNode;
  3.  
  4. struct TNode
  5. {
  6. TNode** nodes;
  7. int checkPoints;
  8. int walls;
  9. int nodeCount;
  10. int scanned;
  11. int index;
  12. };
  13.  
  14. int scan(TNode* node, int scanned){
  15. node->scanned++;
  16. printf("%d | ", node->index);
  17. int n = node->nodeCount - 1;
  18. for (n; n >= 0; n--)
  19. {
  20. printf("%d ,", node->nodes[n]->index);
  21. if (node->nodes[n]->scanned < scanned)
  22. {
  23. scan(node->nodes[n], scanned);
  24. }
  25. }
  26. printf("\n\r");
  27. return 0;
  28. }
  29.  
  30. void initialization(int N, TNode * nodes)
  31. {
  32. int n = N;
  33. for (n; n >= 0; --n)
  34. {
  35. nodes[n].checkPoints = 0;
  36. nodes[n].nodes = 0;
  37. nodes[n].walls = 0;
  38. nodes[n].nodeCount = 0;
  39. nodes[n].scanned = 0;
  40. nodes[n].index = n;
  41. }
  42. }
  43.  
  44. void freeMemory(int N, TNode * nodes)
  45. {
  46. int n = N;
  47. for (n; n >= 0; --n)
  48. {
  49. free(nodes[n].nodes);
  50. }
  51. }
  52.  
  53. void createGraph(int lastPath, int * J, int * K, TNode * nodes)
  54. {
  55. int i = lastPath;
  56. for (i; i >= 0; --i)
  57. {
  58. int nodeJ = J[i];
  59. int nodeK = K[i];
  60. //nodeJ
  61. int lastNode = nodes[nodeJ].nodeCount;
  62. nodes[nodeJ].nodeCount++;
  63. if (lastNode == 0)
  64. {
  65. nodes[nodeJ].nodes = (TNode**) malloc(sizeof(TNode*) * nodes[nodeJ].nodeCount);
  66. }
  67. else
  68. {
  69. realloc(nodes[nodeJ].nodes, sizeof(TNode*) * nodes[nodeJ].nodeCount);
  70. }
  71.  
  72. nodes[nodeJ].nodes[lastNode] = &nodes[nodeK];
  73. //nodeK
  74. lastNode = nodes[nodeK].nodeCount;
  75. nodes[nodeK].nodeCount++;
  76. if (lastNode == 0)
  77. {
  78. nodes[nodeK].nodes = (TNode**) malloc(sizeof(TNode*) * nodes[nodeK].nodeCount);
  79. }
  80. else
  81. {
  82. realloc(nodes[nodeK].nodes, sizeof(TNode*) * nodes[nodeK].nodeCount);
  83. }
  84. nodes[nodeK].nodes[lastNode] = &nodes[nodeJ];
  85. }
  86. }
  87.  
  88. void setCheckPoints(int M, TNode * nodes)
  89. {
  90. int c = M - 1;
  91. for (c; c >= 0; --c)
  92. {
  93. nodes[c].checkPoints++;
  94. }
  95. }
  96.  
  97. int generate(int J [], int K [], int N, int L [], int M){
  98. if (M>0)
  99. {
  100. int intersections = N + 1;
  101. TNode *nodes = (TNode *) malloc((intersections) * sizeof(TNode));
  102. int lastPath = N - 1;
  103. initialization(N, nodes);
  104. createGraph(lastPath, J, K, nodes);
  105. setCheckPoints(M, nodes);
  106. //Scan
  107. int c = 0;
  108. for (c; c < M; c++)
  109. {
  110. scan(&nodes[c], c);
  111. }
  112. freeMemory(N, nodes);
  113. free(nodes);
  114. return 0;
  115. }
  116. else
  117. {
  118. return 0;
  119. }
  120. }
  121. int main(void) {
  122. int J[8] = { 0, 1, 2, 3, 3, 2, 6, 6 };
  123. int K[8] = { 1, 2, 3, 4, 5, 6, 8, 7 };
  124. int L[2] = { 0, 1};
  125. generate(J, K, 8, L, 2);
  126. return 0;
  127. }
  128.  
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
0 | 1 ,

1 | 0 ,2 ,2 | 1 ,3 ,3 | 2 ,4 ,4 | 3 ,

5 ,5 | 3 ,


6 ,6 | 2 ,8 ,8 | 6 ,

7 ,7 | 6 ,