fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define W 12
  5. #define H 10
  6. char map[H][W]={
  7. {1,1,1,1,1,1,1,1,1,1,1},
  8. {1,0,0,0,0,0,0,1,0,0,0,1},
  9. {1,0,1,1,0,0,1,1,0,1,0,1},
  10. {1,0,1,0,0,1,2,0,0,1,0,1},
  11. {1,0,0,0,0,0,1,1,1,0,0,1},
  12. {1,0,1,1,1,1,0,1,0,0,0,1},
  13. {1,0,1,0,0,0,0,1,0,0,0,1},
  14. {1,0,1,0,1,0,1,1,0,0,0,1},
  15. {1,0,0,0,1,0,0,0,0,0,0,1},
  16. {1,1,1,1,1,1,1,1,1,1,1,1},
  17. };
  18. void print_map(){
  19. int i,s;
  20. for(i=0;i<10;i++){
  21. for(s=0;s<12;s++){
  22. if(map[i][s]==1) printf("#");
  23. if(map[i][s]==0) printf(" ");
  24. if(map[i][s]==2) printf("G");
  25. if(map[i][s]==3) printf(".");
  26. }
  27. printf("\n");
  28. }
  29. }
  30.  
  31. void maze0(int x,int y){
  32. if(map[y][x]==0) {
  33. printf("(%d,%d)\n",x,y);
  34. maze0(x,y-1);
  35. maze0(x,y+1);
  36. maze0(x-1,y);
  37. maze0(x+1,y);
  38. }
  39. if(map[y][x]==1){
  40. printf("(%d,%d)X",x,y);
  41. }
  42. if(map[y][x]==2){
  43. printf("(%d,%d)OK",x,y);
  44. exit(0);
  45. }
  46. }
  47. /*すでに探索が終了している場所を周回してしまい
  48. ループしてしまうから*/
  49. void maze1(int x,int y,char his[H][W]){
  50. static char newhis[H][W];
  51. if(map[y][x]==0) {
  52. printf("(%d,%d)\n",x,y);
  53. memcpy(newhis, his, H*W);
  54. newhis[y][x]=1;
  55. if(newhis[y-1][x]!=1) maze1(x,y-1,newhis);
  56. if(newhis[y+1][x]!=1) maze1(x,y+1,newhis);
  57. if(newhis[y][x-1]!=1) maze1(x-1,y,newhis);
  58. if(newhis[y][x+1]!=1) maze1(x+1,y,newhis);
  59. }
  60. if(map[y][x]==1){
  61. printf("(%d,%d)X",x,y);
  62. }
  63. if(map[y][x]==2){
  64. printf("(%d,%d)OK",x,y);
  65. exit(0);
  66. }
  67. }
  68.  
  69. void maze2(int x,int y,char his[H][W]){
  70. static char newhis[H][W];
  71. int i,s;
  72. if(map[y][x]==0) {
  73. printf("(%d,%d)\n",x,y);
  74. memcpy(newhis, his, H*W);
  75. newhis[y][x]=1;
  76. if(newhis[y-1][x]!=1) maze2(x,y-1,newhis);
  77. if(newhis[y+1][x]!=1) maze2(x,y+1,newhis);
  78. if(newhis[y][x-1]!=1) maze2(x-1,y,newhis);
  79. if(newhis[y][x+1]!=1) maze2(x+1,y,newhis);
  80. }
  81. if(map[y][x]==1){
  82. printf("(%d,%d)X",x,y);
  83. }
  84. if(map[y][x]==2){
  85. printf("(%d,%d)OK\n",x,y);
  86. for(i=0;i<10;i++){
  87. for(s=0;s<12;s++){
  88. if(his[i][s]==1) map[i][s]=3;
  89. }
  90. }
  91. print_map();
  92. exit(0);
  93. }
  94. }
  95.  
  96. int main()
  97. {
  98. static char his[H][W];
  99. print_map();
  100. maze2(1,1,his);
  101. return 0;
  102. }
  103.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
########### 
#      #   #
# ##  ## # #
# #  #G  # #
#     ###  #
# #### #   #
# #    #   #
# # # ##   #
#   #      #
############
(1,1)
(1,0)X(1,2)
(1,3)
(1,4)
(1,5)
(1,6)
(1,7)
(1,8)
(1,9)X(0,8)X(2,8)
(2,7)X(2,9)X(3,8)
(3,7)
(3,6)
(3,5)X(2,6)X(4,6)
(4,5)X(4,7)X(5,6)
(5,5)X(5,7)
(5,8)
(5,9)X(4,8)X(6,8)
(6,7)X(6,9)X(7,8)
(7,7)X(7,9)X(8,8)
(8,7)
(8,6)
(8,5)
(8,4)X(7,5)X(9,5)
(9,4)
(9,3)X(8,4)X(10,4)
(10,3)
(10,2)
(10,1)
(10,0)X(9,1)
(9,0)X(9,2)X(8,1)
(8,0)X(8,2)
(8,3)
(8,4)X(7,3)
(7,2)X(7,4)X(6,3)OK
########### 
#.     #...#
#.##  ##.#.#
#.#  #G..#.#
#.    ###..#
#.#### #.. #
#.#... #.  #
#.#.#.##.  #
#...#....  #
############