c语言板跑迷宫
#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={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); //断言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=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);
}
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=1;//
while(currentpoint!=finalpoint)//直到终点停止
{
int i,j;//标记地图
//判断能否上
j=((currentpoint-8)%8)-1;
i=((currentpoint-8)/8);
if(currentpoint-8>1 && maze==1 && map==0)
{
currentpoint=currentpoint-8;
stackpush(p,currentpoint);
map=1; //标记已走
printf("w ");
continue;
}
//能否右走
j=((currentpoint+1)%8)-1;
i=((currentpoint+1)/8);
if(currentpoint%8!=0 && maze==1 && map==0){
currentpoint=currentpoint+1;
stackpush(p,currentpoint);
map=1;
printf("d ");
continue;
}
//能否下走
j=((currentpoint+8)%8)-1;
i=((currentpoint+8)/8);
if(currentpoint+8<=64 && maze==1 && map==0){
currentpoint=currentpoint+8;
stackpush(p,currentpoint);
map=1;
printf("s ");
continue;
}
//能否左走
j=((currentpoint-1)%8)-1;
i=((currentpoint-1)/8);
if((currentpoint-1)%8!=0 && maze==1 && map==0){
currentpoint=currentpoint-1;
stackpush(p,currentpoint);
map=1;
printf("a ");
continue;
}
//都不能走
stackpop(p);
currentpoint=stacktop(p);
printf("返回位置%d\n",currentpoint);
}
printf("well");
}
希望能对栈有更深刻的认识 这段代码有一些错误。首先,`#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 = data;
ps->top++;
}
void stackpop(stack *ps) {
assert(ps);
assert(!stackempty(ps));
ps->top--;
}
int stacktop(stack *ps) {
assert(ps);
return ps->pst;
}
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 = 1;
while (currentpoint != finalpoint) {
int i, j;
// 判断能否上走
j = ((currentpoint - 8) % 8);
i = ((currentpoint - 8) / 8);
if (currentpoint - 8 >= 1 && maze == 1 && map == 0) {
currentpoint -= 8;
stackpush(p, currentpoint);
map = 1;
printf("W ");
continue;
}
// 判断能否右走
j = ((currentpoint + 1) % 8);
i = ((currentpoint + 1) / 8);
if (currentpoint % 8 != 0 && maze == 1 && map == 0){
currentpoint += 1;
stackpush(p, currentpoint);
map = 1;
printf("D ");
continue;
}
// 判断能否下走
j = ((currentpoint + 8) % 8);
i = ((currentpoint + 8) / 8);
if (currentpoint + 8 <= 64 && maze == 1 && map == 0){
currentpoint += 8;
stackpush(p, currentpoint);
map = 1;
printf("S ");
continue;
}
// 判断能否左走
j = ((currentpoint - 1) % 8);
i = ((currentpoint - 1) / 8);
if (currentpoint % 8 != 1 && maze == 1 && map == 0){
currentpoint -= 1;
stackpush(p, currentpoint);
map = 1;
printf("A ");
continue;
}
stackpop(p);
currentpoint = stacktop(p);
printf("B ");
}
stackdestroy(p);
return 0;
}
这里只是简单展示了迷宫的求解过程,没有进行任何输出结果的判断。运行程序后,会输出当前移动的方向(W、D、S、A)以及在退回之后的移动(B)。你可以根据自己的需要进行适当的调整和修改。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]