鱼C论坛

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

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

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

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

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

x
#include<stdio.h>
#include<stdlib.h>
#include <stdbool.h>
#include <assert.h>




typedef struct stack{
    int capacity;//最大容量
    int top;//栈顶
    int *pst;
}stack;


// 初始化栈
void stackinit(stack* ps);
// 入栈
void stackpush(stack* ps, int data);
// 出栈
void stackpop(stack* ps);
// 获取栈顶元素
int stacktop(stack* ps);
// 获取栈中有效元素个数
int stacksize(stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool stackempty(stack* ps);
// 销毁栈
void stackdestroy(stack *ps);

int maze[8][8]={1,1,0,1,1,1,0,1,
                1,1,0,1,1,1,0,1,
                1,1,1,1,0,0,1,1,
                1,0,0,0,1,1,1,1,
                1,1,1,0,1,1,1,1,
                1,0,1,1,1,0,1,1,
                1,0,0,0,1,0,0,1,
                0,1,1,1,1,1,1,1,};//1表示通,0表示不通,表示迷宫

int map[8][8]={0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,};//地图用于表示迷宫位置是否已走过


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

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

    if(ps->top == ps->capacity)
    {
        int newcapacity=ps->capacity==0?4 : ps->capacity*2;
        int *tmp=(int *)realloc(ps->pst,newcapacity*sizeof(int));
        if(tmp==NULL)
        {
            perror("realloc fail");
            return;
        }
        ps->pst=tmp;
        ps->capacity=newcapacity;
    }
    ps->pst[ps->top]=data;
    ps->top++;
}

void stackpop(stack *ps){
    assert(ps);
    assert(!stackempty(ps));
    ps->top--;
}

int stacktop(stack *ps)
{
    assert(ps);
    int top=ps->top;
    return (ps->pst)[top-1];
}

int stacksize(stack *ps){
    assert(ps);
    return ps->top;
}

bool stackempty(stack *ps){
    assert(ps);
    if(ps->top==0)
    {
        return true;
    }
    return false;
}

void stackdestroy(stack *ps){
    assert(ps);
    free(ps->pst);
}

int main()
{
    int currentpoint=1,finalpoint=64;


    stack people;
    stack * p=&people;
    stackinit(p);
    stackpush(p,currentpoint);
    map[0][0]=1;//

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

            currentpoint=currentpoint-8;
            stackpush(p,currentpoint);
            map[i][j]=1; //标记已走
            printf("w ");
            continue;
        }
            //能否右走
             j=((currentpoint+1)%8)-1;
             i=((currentpoint+1)/8);
            if(currentpoint%8!=0 && maze[i][j]==1 && map[i][j]==0){


                currentpoint=currentpoint+1;
                stackpush(p,currentpoint);
                map[i][j]=1;
                printf("d ");
                continue;
            }

            //能否下走
            j=((currentpoint+8)%8)-1;
            i=((currentpoint+8)/8);
             if(currentpoint+8<=64 && maze[i][j]==1 && map[i][j]==0){
                currentpoint=currentpoint+8;
                stackpush(p,currentpoint);
                map[i][j]=1;
                printf("s ");
                continue;
            }
            //能否左走
            j=((currentpoint-1)%8)-1;
            i=((currentpoint-1)/8);
            if((currentpoint-1)%8!=0 && maze[i][j]==1 && map[i][j]==0){

                currentpoint=currentpoint-1;
                stackpush(p,currentpoint);
                map[i][j]=1;
                printf("a ");
                continue;
            }
            //都不能走
            stackpop(p);
            currentpoint=stacktop(p);
            printf("返回位置%d\n",currentpoint);
    }
        printf("well");


}


希望能对栈有更深刻的认识
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

以下是修改后的代码:
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>

typedef struct stack {
    int capacity;
    int top;
    int *pst;
} stack;

void stackinit(stack *ps);
void stackpush(stack *ps, int data);
void stackpop(stack *ps);
int stacktop(stack *ps);
int stacksize(stack *ps);
bool stackempty(stack *ps);
void stackdestroy(stack *ps);

int maze[] = {1, 1, 0, 1, 1, 1, 0, 1,
              1, 1, 0, 1, 1, 1, 0, 1,
              1, 1, 1, 1, 0, 0, 1, 1,
              1, 0, 0, 0, 1, 1, 1, 1,
              1, 1, 1, 0, 1, 1, 1, 1,
              1, 0, 1, 1, 1, 0, 1, 1,
              1, 0, 0, 0, 1, 0, 0, 1,
              0, 1, 1, 1, 1, 1, 1, 1}; // 1表示通, 0表示不通

int map[] = {0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 0, 0, 0}; // 地图用于表示迷宫位置是否已走过

void stackinit(stack *ps) {
    ps->pst = NULL;
    ps->top = 0;
    ps->capacity = 0;
}

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

    if (ps->top == ps->capacity) {
        int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
        int *tmp = (int *)realloc(ps->pst, newcapacity * sizeof(int));
        if (tmp == NULL) {
            perror("realloc fail");
            return;
        }
        ps->pst = tmp;
        ps->capacity = newcapacity;
    }
    ps->pst[ps->top] = data;
    ps->top++;
}

void stackpop(stack *ps) {
    assert(ps);
    assert(!stackempty(ps));
    ps->top--;
}

int stacktop(stack *ps) {
    assert(ps);
    return ps->pst[ps->top - 1];
}

int stacksize(stack *ps) {
    assert(ps);
    return ps->top;
}

bool stackempty(stack *ps) {
    assert(ps);
    return ps->top == 0;
}

void stackdestroy(stack *ps) {
    assert(ps);
    free(ps->pst);
}

int main() {
    int currentpoint = 1, finalpoint = 64;
    
    stack people;
    stack *p = &people;
    stackinit(p);
    stackpush(p, currentpoint);
    map[currentpoint - 1] = 1;

    while (currentpoint != finalpoint) {
        int i, j;
        
        // 判断能否上走
        j = ((currentpoint - 8) % 8);
        i = ((currentpoint - 8) / 8);
        if (currentpoint - 8 >= 1 && maze[currentpoint - 9] == 1 && map[currentpoint - 9] == 0) {
            currentpoint -= 8;
            stackpush(p, currentpoint);
            map[currentpoint - 1] = 1;
            printf("W ");
            continue;
        }

        // 判断能否右走
        j = ((currentpoint + 1) % 8);
        i = ((currentpoint + 1) / 8);
        if (currentpoint % 8 != 0 && maze[currentpoint] == 1 && map[currentpoint] == 0){
            currentpoint += 1;
            stackpush(p, currentpoint);
            map[currentpoint - 1] = 1;
            printf("D ");
            continue;
        }

        // 判断能否下走
        j = ((currentpoint + 8) % 8);
        i = ((currentpoint + 8) / 8);
        if (currentpoint + 8 <= 64 && maze[currentpoint + 7] == 1 && map[currentpoint + 7] == 0){
            currentpoint += 8;
            stackpush(p, currentpoint);
            map[currentpoint - 1] = 1;
            printf("S ");
            continue;
        }

        // 判断能否左走
        j = ((currentpoint - 1) % 8);
        i = ((currentpoint - 1) / 8);
        if (currentpoint % 8 != 1 && maze[currentpoint - 2] == 1 && map[currentpoint - 2] == 0){
            currentpoint -= 1;
            stackpush(p, currentpoint);
            map[currentpoint - 1] = 1;
            printf("A ");
            continue;
        }

        stackpop(p);
        currentpoint = stacktop(p);
        printf("B ");
    }

    stackdestroy(p);
    
    return 0;
}

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-17 07:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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