fork download
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. void add_pointers(char parent,char child,char pointers[][2])
  5. {
  6. int cnt=0;
  7. while(pointers[cnt++][0]!=-1);
  8. pointers[cnt-1][0] = parent;
  9. pointers[cnt-1][1] = child;
  10. pointers[cnt][0] = pointers[cnt][1] = -1;
  11. }
  12. void print_route(char pointers[][2])
  13. {
  14. char tofind = 'G';
  15.  
  16. char route[1024];
  17. route[0] = tofind;
  18. int n_route = 1;
  19.  
  20. while(tofind != 'S'){
  21. for(int cnt=0;pointers[cnt][0]!=0;cnt++){
  22. if(pointers[cnt][1] == tofind){
  23. route[n_route] = pointers[cnt][0];
  24. n_route++;
  25. tofind = pointers[cnt][0];
  26. break;
  27. }
  28. }
  29. }
  30.  
  31. for(int cnt=n_route-1;cnt>=0;cnt--){
  32. printf("%c",route[cnt]);
  33. if(cnt!=0){
  34. printf(" -> ");
  35. }
  36. }
  37. printf("\n");
  38. }
  39. int has_node(char buf[][2],char node[2])
  40. {
  41. for(int cnt=0;buf[cnt][0]!=-1;cnt++){
  42. if(buf[cnt][0] == node[0]){
  43. return buf[cnt][1];
  44. }
  45. }
  46. return -1;
  47. }
  48. bool add_node(char buf[][2],char node[2])
  49. {
  50. if(node[0] == 'G'){
  51. char bk[2] = {buf[0][0],buf[0][1]};
  52. int cnt=1;
  53. while(bk[0]!=-1){
  54. char bk2[2] = {buf[cnt][0],buf[cnt][1]};
  55. buf[cnt][0] = bk[0];
  56. buf[cnt][1] = bk[1];
  57. bk[0] = bk2[0];
  58. bk[1] = bk2[1];
  59. cnt++;
  60. }
  61. buf[cnt][0] = -1;
  62. buf[0][0] = node[0];
  63. buf[0][1] = node[1];
  64. }else{
  65. int cnt=0;
  66. while(buf[cnt++][0]!=-1);
  67. buf[cnt-1][0] = node[0];
  68. buf[cnt-1][1] = node[1];
  69. buf[cnt][0] = -1;
  70. }
  71. return true;
  72. }
  73. bool remove_node(char buf[][2],char node[2])
  74. {
  75. for(int cnt=0;buf[cnt][0]!=-1;cnt++){
  76. if(buf[cnt][0]==node[0]){
  77. int cnt2=cnt+1;
  78. for(;buf[cnt2][0]!=-1;cnt2++){
  79. buf[cnt2-1][0] = buf[cnt2][0];
  80. buf[cnt2-1][1] = buf[cnt2][1];
  81. }
  82. buf[cnt2-1][0] = -1;
  83. return true;
  84. }
  85. }
  86. return true;
  87. }
  88. int print_list(char list[][2])
  89. {
  90. int printed = 0;
  91. for(int cnt=0;list[cnt][0]!=-1;cnt++){
  92. if(cnt!=0){
  93. printf(",");
  94. }
  95. printf("%c(%d)",list[cnt][0],list[cnt][1]);
  96. printed++;
  97. }
  98. return printed;
  99. }
  100. int main()
  101. {
  102. char graph[][3] = {
  103. {'S','B',0},
  104. {'S','C',0},
  105. {'S','D',0},
  106. {'B','A',0},
  107. {'A','C',0},
  108. {'C','D',0},
  109. {'D','G',0},
  110. {-1,-1,-1},
  111. };
  112.  
  113. char open[2048][2] = {{'S',0},{-1,-1}};
  114. char closed[2048][2] = {{-1,-1}};
  115. char pointers[2048][2] = {{-1,-1}};
  116.  
  117. printf("OPEN\t\tCLOSED\n");
  118. while(open[0][0] != -1){
  119.  
  120. int printed = print_list(open);
  121. printf(printed>=2?"\t":"\t\t");
  122. print_list(closed);
  123. printf("\n");
  124.  
  125. char n[2] = {open[0][0],open[0][1]};
  126. if(n[0] == 'G'){
  127. print_route(pointers);
  128. return 1;
  129. }else{
  130. remove_node(open,n);
  131. add_node(closed,n);
  132.  
  133. for(int cnt=0;graph[cnt][0]!=0;cnt++){
  134. if(graph[cnt][0] == n[0]){
  135. char m[2] = {graph[cnt][1],n[1]+graph[cnt][2]};
  136.  
  137. if(has_node(open,m)==-1 && has_node(closed,m)==-1){
  138. add_pointers(n[0],m[0],pointers);
  139. add_node(open,m);
  140. }
  141. }
  142. }
  143. }
  144. }
  145. printf("ルートを発見できませんでした。\n");
  146.  
  147. return 0;
  148. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:2:19: error: conio.h: No such file or directory
prog.c: In function ‘print_route’:
prog.c:21: error: ‘for’ loop initial declaration used outside C99 mode
prog.c:31: error: ‘for’ loop initial declaration used outside C99 mode
prog.c: In function ‘has_node’:
prog.c:41: error: ‘for’ loop initial declaration used outside C99 mode
prog.c: At top level:
prog.c:48: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘add_node’
prog.c:73: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘remove_node’
prog.c: In function ‘print_list’:
prog.c:91: error: ‘for’ loop initial declaration used outside C99 mode
prog.c: In function ‘main’:
prog.c:130: warning: implicit declaration of function ‘remove_node’
prog.c:131: warning: implicit declaration of function ‘add_node’
prog.c:133: error: ‘for’ loop initial declaration used outside C99 mode
stdout
Standard output is empty