鱼C论坛

 找回密码
 立即注册
查看: 569|回复: 1

[学习笔记] c语言板跑迷宫

[复制链接]
发表于 2024-5-25 15:17:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <stdbool.h>
  4. #include <assert.h>




  5. typedef struct stack{
  6.     int capacity;//最大容量
  7.     int top;//栈顶
  8.     int *pst;
  9. }stack;


  10. // 初始化栈
  11. void stackinit(stack* ps);
  12. // 入栈
  13. void stackpush(stack* ps, int data);
  14. // 出栈
  15. void stackpop(stack* ps);
  16. // 获取栈顶元素
  17. int stacktop(stack* ps);
  18. // 获取栈中有效元素个数
  19. int stacksize(stack* ps);
  20. // 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
  21. bool stackempty(stack* ps);
  22. // 销毁栈
  23. void stackdestroy(stack *ps);

  24. int maze[8][8]={1,1,0,1,1,1,0,1,
  25.                 1,1,0,1,1,1,0,1,
  26.                 1,1,1,1,0,0,1,1,
  27.                 1,0,0,0,1,1,1,1,
  28.                 1,1,1,0,1,1,1,1,
  29.                 1,0,1,1,1,0,1,1,
  30.                 1,0,0,0,1,0,0,1,
  31.                 0,1,1,1,1,1,1,1,};//1表示通,0表示不通,表示迷宫

  32. int map[8][8]={0,0,0,0,0,0,0,0,
  33.                0,0,0,0,0,0,0,0,
  34.                0,0,0,0,0,0,0,0,
  35.                0,0,0,0,0,0,0,0,
  36.                0,0,0,0,0,0,0,0,
  37.                0,0,0,0,0,0,0,0,
  38.                0,0,0,0,0,0,0,0,
  39.                0,0,0,0,0,0,0,0,};//地图用于表示迷宫位置是否已走过


  40. void stackinit(stack *ps){
  41.     ps->pst=NULL;
  42.     ps->top=0;
  43.     ps->capacity=0;//栈总的容量
  44. }

  45. void stackpush(stack *ps,int data){
  46.     assert(ps); //断言ps是否存在

  47.     if(ps->top == ps->capacity)
  48.     {
  49.         int newcapacity=ps->capacity==0?4 : ps->capacity*2;
  50.         int *tmp=(int *)realloc(ps->pst,newcapacity*sizeof(int));
  51.         if(tmp==NULL)
  52.         {
  53.             perror("realloc fail");
  54.             return;
  55.         }
  56.         ps->pst=tmp;
  57.         ps->capacity=newcapacity;
  58.     }
  59.     ps->pst[ps->top]=data;
  60.     ps->top++;
  61. }

  62. void stackpop(stack *ps){
  63.     assert(ps);
  64.     assert(!stackempty(ps));
  65.     ps->top--;
  66. }

  67. int stacktop(stack *ps)
  68. {
  69.     assert(ps);
  70.     int top=ps->top;
  71.     return (ps->pst)[top-1];
  72. }

  73. int stacksize(stack *ps){
  74.     assert(ps);
  75.     return ps->top;
  76. }

  77. bool stackempty(stack *ps){
  78.     assert(ps);
  79.     if(ps->top==0)
  80.     {
  81.         return true;
  82.     }
  83.     return false;
  84. }

  85. void stackdestroy(stack *ps){
  86.     assert(ps);
  87.     free(ps->pst);
  88. }

  89. int main()
  90. {
  91.     int currentpoint=1,finalpoint=64;


  92.     stack people;
  93.     stack * p=&people;
  94.     stackinit(p);
  95.     stackpush(p,currentpoint);
  96.     map[0][0]=1;//

  97.     while(currentpoint!=finalpoint)//直到终点停止
  98.     {
  99.         int i,j;//标记地图
  100.         //判断能否上
  101.         j=((currentpoint-8)%8)-1;
  102.         i=((currentpoint-8)/8);
  103.         if(currentpoint-8>1 && maze[i][j]==1 && map[i][j]==0)
  104.         {

  105.             currentpoint=currentpoint-8;
  106.             stackpush(p,currentpoint);
  107.             map[i][j]=1; //标记已走
  108.             printf("w ");
  109.             continue;
  110.         }
  111.             //能否右走
  112.              j=((currentpoint+1)%8)-1;
  113.              i=((currentpoint+1)/8);
  114.             if(currentpoint%8!=0 && maze[i][j]==1 && map[i][j]==0){


  115.                 currentpoint=currentpoint+1;
  116.                 stackpush(p,currentpoint);
  117.                 map[i][j]=1;
  118.                 printf("d ");
  119.                 continue;
  120.             }

  121.             //能否下走
  122.             j=((currentpoint+8)%8)-1;
  123.             i=((currentpoint+8)/8);
  124.              if(currentpoint+8<=64 && maze[i][j]==1 && map[i][j]==0){
  125.                 currentpoint=currentpoint+8;
  126.                 stackpush(p,currentpoint);
  127.                 map[i][j]=1;
  128.                 printf("s ");
  129.                 continue;
  130.             }
  131.             //能否左走
  132.             j=((currentpoint-1)%8)-1;
  133.             i=((currentpoint-1)/8);
  134.             if((currentpoint-1)%8!=0 && maze[i][j]==1 && map[i][j]==0){

  135.                 currentpoint=currentpoint-1;
  136.                 stackpush(p,currentpoint);
  137.                 map[i][j]=1;
  138.                 printf("a ");
  139.                 continue;
  140.             }
  141.             //都不能走
  142.             stackpop(p);
  143.             currentpoint=stacktop(p);
  144.             printf("返回位置%d\n",currentpoint);
  145.     }
  146.         printf("well");


  147. }







复制代码



希望能对栈有更深刻的认识
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-5-25 15:17:57 | 显示全部楼层
这段代码有一些错误。首先,`#include`的语句不完整,应该添加`<stdbool.h>`用于定义布尔类型。其次,`assert`函数需要`<assert.h>`头文件的支持。此外,你在声明`maze`和`map`时使用了大括号`{ }`,这是错误的。应该使用方括号`[ ]`来表示数组。

以下是修改后的代码:

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <assert.h>
  4. #include <stdlib.h>

  5. typedef struct stack {
  6.     int capacity;
  7.     int top;
  8.     int *pst;
  9. } stack;

  10. void stackinit(stack *ps);
  11. void stackpush(stack *ps, int data);
  12. void stackpop(stack *ps);
  13. int stacktop(stack *ps);
  14. int stacksize(stack *ps);
  15. bool stackempty(stack *ps);
  16. void stackdestroy(stack *ps);

  17. int maze[] = {1, 1, 0, 1, 1, 1, 0, 1,
  18.               1, 1, 0, 1, 1, 1, 0, 1,
  19.               1, 1, 1, 1, 0, 0, 1, 1,
  20.               1, 0, 0, 0, 1, 1, 1, 1,
  21.               1, 1, 1, 0, 1, 1, 1, 1,
  22.               1, 0, 1, 1, 1, 0, 1, 1,
  23.               1, 0, 0, 0, 1, 0, 0, 1,
  24.               0, 1, 1, 1, 1, 1, 1, 1}; // 1表示通, 0表示不通

  25. int map[] = {0, 0, 0, 0, 0, 0, 0, 0,
  26.              0, 0, 0, 0, 0, 0, 0, 0,
  27.              0, 0, 0, 0, 0, 0, 0, 0,
  28.              0, 0, 0, 0, 0, 0, 0, 0,
  29.              0, 0, 0, 0, 0, 0, 0, 0,
  30.              0, 0, 0, 0, 0, 0, 0, 0,
  31.              0, 0, 0, 0, 0, 0, 0, 0,
  32.              0, 0, 0, 0, 0, 0, 0, 0}; // 地图用于表示迷宫位置是否已走过

  33. void stackinit(stack *ps) {
  34.     ps->pst = NULL;
  35.     ps->top = 0;
  36.     ps->capacity = 0;
  37. }

  38. void stackpush(stack *ps, int data) {
  39.     assert(ps);

  40.     if (ps->top == ps->capacity) {
  41.         int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
  42.         int *tmp = (int *)realloc(ps->pst, newcapacity * sizeof(int));
  43.         if (tmp == NULL) {
  44.             perror("realloc fail");
  45.             return;
  46.         }
  47.         ps->pst = tmp;
  48.         ps->capacity = newcapacity;
  49.     }
  50.     ps->pst[ps->top] = data;
  51.     ps->top++;
  52. }

  53. void stackpop(stack *ps) {
  54.     assert(ps);
  55.     assert(!stackempty(ps));
  56.     ps->top--;
  57. }

  58. int stacktop(stack *ps) {
  59.     assert(ps);
  60.     return ps->pst[ps->top - 1];
  61. }

  62. int stacksize(stack *ps) {
  63.     assert(ps);
  64.     return ps->top;
  65. }

  66. bool stackempty(stack *ps) {
  67.     assert(ps);
  68.     return ps->top == 0;
  69. }

  70. void stackdestroy(stack *ps) {
  71.     assert(ps);
  72.     free(ps->pst);
  73. }

  74. int main() {
  75.     int currentpoint = 1, finalpoint = 64;
  76.    
  77.     stack people;
  78.     stack *p = &people;
  79.     stackinit(p);
  80.     stackpush(p, currentpoint);
  81.     map[currentpoint - 1] = 1;

  82.     while (currentpoint != finalpoint) {
  83.         int i, j;
  84.         
  85.         // 判断能否上走
  86.         j = ((currentpoint - 8) % 8);
  87.         i = ((currentpoint - 8) / 8);
  88.         if (currentpoint - 8 >= 1 && maze[currentpoint - 9] == 1 && map[currentpoint - 9] == 0) {
  89.             currentpoint -= 8;
  90.             stackpush(p, currentpoint);
  91.             map[currentpoint - 1] = 1;
  92.             printf("W ");
  93.             continue;
  94.         }

  95.         // 判断能否右走
  96.         j = ((currentpoint + 1) % 8);
  97.         i = ((currentpoint + 1) / 8);
  98.         if (currentpoint % 8 != 0 && maze[currentpoint] == 1 && map[currentpoint] == 0){
  99.             currentpoint += 1;
  100.             stackpush(p, currentpoint);
  101.             map[currentpoint - 1] = 1;
  102.             printf("D ");
  103.             continue;
  104.         }

  105.         // 判断能否下走
  106.         j = ((currentpoint + 8) % 8);
  107.         i = ((currentpoint + 8) / 8);
  108.         if (currentpoint + 8 <= 64 && maze[currentpoint + 7] == 1 && map[currentpoint + 7] == 0){
  109.             currentpoint += 8;
  110.             stackpush(p, currentpoint);
  111.             map[currentpoint - 1] = 1;
  112.             printf("S ");
  113.             continue;
  114.         }

  115.         // 判断能否左走
  116.         j = ((currentpoint - 1) % 8);
  117.         i = ((currentpoint - 1) / 8);
  118.         if (currentpoint % 8 != 1 && maze[currentpoint - 2] == 1 && map[currentpoint - 2] == 0){
  119.             currentpoint -= 1;
  120.             stackpush(p, currentpoint);
  121.             map[currentpoint - 1] = 1;
  122.             printf("A ");
  123.             continue;
  124.         }

  125.         stackpop(p);
  126.         currentpoint = stacktop(p);
  127.         printf("B ");
  128.     }

  129.     stackdestroy(p);
  130.    
  131.     return 0;
  132. }
复制代码


这里只是简单展示了迷宫的求解过程,没有进行任何输出结果的判断。运行程序后,会输出当前移动的方向(W、D、S、A)以及在退回之后的移动(B)。你可以根据自己的需要进行适当的调整和修改。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-20 00:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表