fork download
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5.  
  6. #define MAX_STACK_SIZE 1000
  7. #define MAX_SIZE 6
  8.  
  9. typedef struct {
  10. short r;
  11. short c;
  12. } element;
  13.  
  14. typedef struct {
  15. element stack[MAX_STACK_SIZE];
  16. int top;
  17. } StackType;
  18.  
  19. void init(StackType *s)
  20. {
  21. s->top = -1;
  22. }
  23.  
  24. /**
  25.  * \brief return the true if its empty.
  26.  */
  27. bool is_empty(StackType *s)
  28. {
  29. if(s->top == -1) {
  30. return true;
  31. }
  32.  
  33. return false;
  34. }
  35.  
  36. /**
  37.  * \brief return the true if its full.
  38.  */
  39. bool is_full(StackType *s)
  40. {
  41. if(s->top == (MAX_STACK_SIZE - 1)) {
  42. return true;
  43. }
  44.  
  45. return false;
  46. }
  47.  
  48. /**
  49.  * \brief push the element to the stack.
  50.  */
  51. void push(StackType *s, element item)
  52. {
  53. int ret;
  54.  
  55. ret = is_full(s);
  56. if(ret == true) {
  57. fprintf(stderr, "stack is full.\n");
  58. return;
  59. }
  60.  
  61. s->top++;
  62. s->stack[s->top] = item;
  63. }
  64.  
  65. /**
  66.  * \brief pop the element.
  67.  */
  68. element pop(StackType *s)
  69. {
  70. int ret;
  71. element tmp_element;
  72.  
  73. ret = is_empty(s);
  74. if(ret == true) {
  75. fprintf(stderr, "stack is empty\n");
  76. exit(1);
  77. }
  78.  
  79. tmp_element = s->stack[s->top];
  80. s->top--;
  81.  
  82. return tmp_element;
  83. }
  84.  
  85. element peek(StackType *s)
  86. {
  87. if (is_empty(s) == 1) {
  88. fprintf(stderr, "stack is empty");
  89. exit(1);
  90. }
  91.  
  92. return s->stack[s->top];
  93. }
  94.  
  95. element here = { 1,0 }; //here.r = 1, here.c = 0
  96. element entry = { 1,0 }; //end.r =1 end.c = 0
  97. char maze[MAX_SIZE][MAX_SIZE] = {
  98. {'1', '1', '1', '1', '1', '1'},
  99. {'e', '0', '1', '0', '0', '1'},
  100. {'1', '0', '0', '0', '1', '1'},
  101. {'1', '0', '1', '0', '1', '1'},
  102. {'1', '0', '1', '0', '0', 'x'},
  103. {'1', '1', '1', '1', '1', '1'},
  104. };
  105.  
  106. void push_loc(StackType *s, int r, int c)
  107. {
  108. if (r < 0 || c < 0) {
  109. return;
  110. }
  111.  
  112. if ((maze[r][c] != '1') && (maze[r][c] != '.')) {
  113. element tmp;
  114. tmp.r = r;
  115. tmp.c = c;
  116. push(s, tmp);
  117. }
  118. }
  119.  
  120. int main(){
  121. int r, c;
  122. StackType s;
  123.  
  124. init(&s);
  125. here = entry;
  126.  
  127. while(maze[here.r][here.c] != 'x') {
  128. r = here.r;
  129. c = here.c;
  130.  
  131. maze[r][c] = '.';
  132. printf("Current location. r[%d], c[%d]\n", r, c);
  133.  
  134. push_loc(&s, r - 1, c);
  135.  
  136. push_loc(&s, r + 1, c);
  137.  
  138. push_loc(&s, r, c - 1);
  139.  
  140. push_loc(&s, r, c + 1);
  141.  
  142.  
  143. if (is_empty(&s)) {
  144. printf("not exitn");
  145. return 1;
  146. }
  147. else {
  148. here = pop(&s);
  149. }
  150. }
  151. printf("success");
  152.  
  153. return 0;
  154. }
  155.  
  156.  
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
Current location. r[1], c[0]
Current location. r[1], c[1]
Current location. r[2], c[1]
Current location. r[2], c[2]
Current location. r[2], c[3]
Current location. r[3], c[3]
Current location. r[4], c[3]
Current location. r[4], c[4]
success