经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
C语言通过栈实现小人走迷宫
来源:jb51  时间:2022/3/2 11:42:53  对本文有异议

本文实例为大家分享了C语言通过栈实现小人走迷宫的具体代码,供大家参考,具体内容如下

新建stack.h

  1. #include "Data.h"
  2. #ifndef _STACK_H
  3. #define _STACK_H
  4. #define INIT_SIZE 10
  5. #define INIT_INCREM 10
  6. typedef struct _STACK{
  7. ?? ?ElemType *Base;
  8. ?? ?ElemType *Top;
  9. ?? ?int size;
  10. } STACK;
  11. STACK* InitStack();
  12. void DestroyStack(STACK* s);
  13. //压栈
  14. int Push(STACK* s, ElemType *e);
  15. //弹栈
  16. int Pop(STACK* s, ElemType* e);
  17. //站是否为空
  18. int IsEmpty(STACK* ?s);
  19. #endif;

新建stack.c

  1. #include "stack.h"
  2. #include<stdlib.h>
  3.  
  4. STACK* InitStack(){
  5. ?? ?STACK* s = (STACK*)malloc(sizeof(STACK));
  6. ?? ?if (s == NULL){
  7. ?? ??? ?exit(0);
  8. ?? ?}
  9. ?? ?s->Base = (ElemType*)malloc(INIT_SIZE*sizeof(ElemType));
  10. ?? ?if (s->Base == NULL){
  11. ?? ??? ?free(s->Base);
  12. ?? ??? ?free(s);
  13. ?? ??? ?exit(0);
  14. ?? ?}
  15. ?? ?s->Top = s->Base;
  16. ?? ?s->size = INIT_SIZE;
  17. ?? ?return s;
  18. }
  19.  
  20. void DestroyStack(STACK* s){
  21. ?? ?free(s->Base);
  22. ?? ?free(s);
  23.  
  24. }
  25.  
  26.  
  27. int Push(STACK* s, ElemType *e){
  28. ?? ?if (s == NULL || e==NULL){
  29. ?? ??? ?return 0;
  30. ?? ?}
  31.  
  32. ?? ?if (s->Top - s->Base >= s->size){
  33. ?? ??? ?s->Base = (ElemType*)realloc(s->Base, (s->size + INIT_INCREM)*sizeof(ElemType));
  34. ?? ??? ?if (s->Base == NULL){
  35. ?? ??? ??? ?return 0;
  36. ?? ??? ?}
  37. ?? ??? ?s->Top = s->Base + s->size;
  38.  
  39. ?? ??? ?s->size = s->size + INIT_INCREM;
  40. ?? ?
  41. ?? ?}
  42.  
  43. ?? ?
  44. ?? ?*s->Top = *e;
  45. ?? ?s->Top++;
  46. ?? ?return 1;
  47. }
  48.  
  49. int Pop(STACK* s, ElemType* e){
  50. ?? ?if (s == NULL || e==NULL){
  51. ?? ??? ?return 0;
  52. ?? ?}
  53.  
  54. ?? ?if (s->Base == s->Top){
  55. ?? ??? ?return 0;
  56. ?? ?}
  57. ?? ?s->Top--;
  58. ?? ?*e = *s->Top;
  59. ?? ?return 1;
  60. }
  61.  
  62. int IsEmpty(STACK* ?s){
  63. ?? ?return s->Base == s->Top ? 1 : 0;
  64. }

新建Data.h

  1. #ifndef _DATA_H
  2. #define _DATA_H
  3. ?? ?typedef struct
  4. ?? ?{
  5. ?? ??? ?int y;
  6. ?? ??? ?int x;
  7. ?? ?}POS;
  8.  
  9. ?? ?typedef struct{
  10. ?? ??? ?int ord;
  11. ?? ??? ?POS seat;
  12. ?? ??? ?int di;
  13. ?? ?}ElemType;
  14. #endif

新建main.c

  1. #include "Data.h"
  2. #include "stack.h"
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <stdlib.h>
  6.  
  7. ?int item[10][10]={
  8. ?? ?{1,1,1,1,1,1,1,1,1,1},
  9. ?? ?{1,0,0,1,0,0,0,1,0,1},
  10. ?? ?{1,0,0,1,0,0,0,1,0,1},
  11. ?? ?{1,0,0,0,0,1,1,0,0,1},
  12. ?? ?{1,0,1,1,1,0,0,0,0,1},
  13. ?? ?{1,0,0,0,1,0,0,0,0,1},
  14. ?? ?{1,0,1,0,0,0,1,0,0,1},
  15. ?? ?{1,0,1,1,1,0,1,1,0,1},
  16. ?? ?{1,1,0,0,0,0,0,0,0,1},
  17. ?? ?{1,1,1,1,1,1,1,1,1,1}
  18. };
  19.  
  20. static const POS inPos={1,1},outPos={8,8};
  21. int IsPass(POS CurP){
  22. ?? ?return item[CurP.y][CurP.x]==0?1:0;
  23. }
  24.  
  25. POS NextPos(POS CurP,int di){
  26. ?? ?POS p=CurP;
  27. ?? ?switch(di){
  28. ?? ??? ?case 0:
  29. ?? ??? ??? ?p.x--;//向左
  30. ?? ??? ??? ?break;
  31. ?? ??? ?case 1:
  32. ?? ??? ??? ?p.y++;//向下
  33. ?? ??? ??? ?break;
  34. ?? ??? ?case 2:
  35. ?? ??? ??? ?p.x++;//向右
  36. ?? ??? ??? ?break;
  37. ?? ??? ?case 3:
  38. ?? ??? ??? ?p.y--;//向上
  39. ?? ??? ??? ?break;
  40. ?? ?}
  41. ?? ?return p;
  42. }
  43.  
  44. void PrintItem(POS CurP){
  45. ?? ?int i,j;
  46. ?? ?system("cls");
  47.  
  48. ?? ?for(i=0;i<10;i++){
  49. ?? ??? ?for(j=0;j<10;j++){
  50. ?? ??? ??? ?if(i==CurP.y && j==CurP.x){
  51. ?? ??? ??? ??? ?printf("@");
  52. ?? ??? ??? ??? ?continue;
  53. ?? ??? ??? ?}
  54.  
  55. ?? ??? ??? ?if(item[i][j]==1){
  56. ?? ??? ??? ??? ?printf("*");
  57.  
  58. ?? ??? ??? ?}else{
  59. ?? ??? ??? ??? ?printf(" ");
  60. ?? ??? ??? ?}
  61. ?? ??? ?}
  62. ?? ??? ?printf("\n");
  63. ?? ?}
  64. }
  65.  
  66. void main(){
  67.  
  68. ?? ?STACK* s=InitStack();
  69. ?? ?ElemType e;
  70. ?? ?int setp=1;
  71. ?? ?POS CurPos=inPos;
  72. ?? ?PrintItem(inPos);
  73.  
  74. ?? ?do{
  75. ?? ??? ?if(IsPass(CurPos)){
  76. ?? ??? ??? ?e.ord=setp;
  77. ?? ??? ??? ?e.di=0;
  78. ?? ??? ??? ?e.seat=CurPos;
  79. ?? ??? ??? ?Push(s,&e);//只有能通过才压栈
  80.  
  81. ?? ??? ??? ?item[CurPos.y][CurPos.x]=2;
  82. ?? ??? ??? ?if(CurPos.y==outPos.y && CurPos.x==outPos.x){
  83. ?? ??? ??? ??? ?
  84. ?? ??? ??? ??? ?PrintItem(CurPos);
  85. ?? ??? ??? ??? ?printf("ok!\n");
  86. ?? ??? ??? ??? ?break;
  87. ?? ??? ??? ?}
  88.  
  89. ?? ??? ??? ?PrintItem(CurPos);
  90.  
  91. ?? ??? ??? ?CurPos=NextPos(e.seat,0);
  92. ?? ??? ??? ?setp++;
  93. ?? ??? ??? ?getch();
  94. ?? ??? ?}else{
  95.  
  96. ?? ??? ??? ?Pop(s,&e);//如果不能通过就弹栈
  97.  
  98. ?? ??? ??? ?if(e.di==4 && !IsEmpty(s)){
  99. ?? ??? ??? ??? ??? ?item[CurPos.y][CurPos.x]=8;
  100. ?? ??? ??? ??? ??? ?Pop(s,&e);
  101. ?? ??? ??? ?}
  102.  
  103. ?? ??? ??? ?if(e.di<3){
  104. ?? ??? ??? ??? ?e.di++;
  105. ?? ??? ??? ??? ?Push(s,&e);
  106. ?? ??? ??? ??? ?CurPos=NextPos(e.seat,e.di);
  107. ?? ??? ??? ?}
  108. ?? ??? ?}
  109. ?? ?}while(!IsEmpty(s));
  110.  
  111. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号