本文实例为大家分享了C语言通过栈实现小人走迷宫的具体代码,供大家参考,具体内容如下 新建stack.h #include "Data.h" #ifndef _STACK_H #de
本文实例为大家分享了C语言通过栈实现小人走迷宫的具体代码,供大家参考,具体内容如下
新建stack.h
#include "Data.h"
#ifndef _STACK_H
#define _STACK_H
#define INIT_SIZE 10
#define INIT_INCREM 10
typedef struct _STACK{
ElemType *Base;
ElemType *Top;
int size;
} STACK;
STACK* InitStack();
void DestroyStack(STACK* s);
//压栈
int Push(STACK* s, ElemType *e);
//弹栈
int Pop(STACK* s, ElemType* e);
//站是否为空
int IsEmpty(STACK* s);
#endif;
新建stack.c
#include "stack.h"
#include<stdlib.h>
STACK* InitStack(){
STACK* s = (STACK*)malloc(sizeof(STACK));
if (s == NULL){
exit(0);
}
s->Base = (ElemType*)malloc(INIT_SIZE*sizeof(ElemType));
if (s->Base == NULL){
free(s->Base);
free(s);
exit(0);
}
s->Top = s->Base;
s->size = INIT_SIZE;
return s;
}
void DestroyStack(STACK* s){
free(s->Base);
free(s);
}
int Push(STACK* s, ElemType *e){
if (s == NULL || e==NULL){
return 0;
}
if (s->Top - s->Base >= s->size){
s->Base = (ElemType*)realloc(s->Base, (s->size + INIT_INCREM)*sizeof(ElemType));
if (s->Base == NULL){
return 0;
}
s->Top = s->Base + s->size;
s->size = s->size + INIT_INCREM;
}
*s->Top = *e;
s->Top++;
return 1;
}
int Pop(STACK* s, ElemType* e){
if (s == NULL || e==NULL){
return 0;
}
if (s->Base == s->Top){
return 0;
}
s->Top--;
*e = *s->Top;
return 1;
}
int IsEmpty(STACK* s){
return s->Base == s->Top ? 1 : 0;
}
新建Data.h
#ifndef _DATA_H
#define _DATA_H
typedef struct
{
int y;
int x;
}POS;
typedef struct{
int ord;
POS seat;
int di;
}ElemType;
#endif
新建main.c
#include "Data.h"
#include "stack.h"
#include <stdio.h>
#include <coNIO.h>
#include <stdlib.h>
int item[10][10]={
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
static const POS inPos={1,1},outPos={8,8};
int IsPass(POS CurP){
return item[CurP.y][CurP.x]==0?1:0;
}
POS NextPos(POS CurP,int di){
POS p=CurP;
switch(di){
case 0:
p.x--;//向左
break;
case 1:
p.y++;//向下
break;
case 2:
p.x++;//向右
break;
case 3:
p.y--;//向上
break;
}
return p;
}
void PrintItem(POS CurP){
int i,j;
system("cls");
for(i=0;i<10;i++){
for(j=0;j<10;j++){
if(i==CurP.y && j==CurP.x){
printf("@");
continue;
}
if(item[i][j]==1){
printf("*");
}else{
printf(" ");
}
}
printf("\n");
}
}
void main(){
STACK* s=InitStack();
ElemType e;
int setp=1;
POS CurPos=inPos;
PrintItem(inPos);
do{
if(IsPass(CurPos)){
e.ord=setp;
e.di=0;
e.seat=CurPos;
Push(s,&e);//只有能通过才压栈
item[CurPos.y][CurPos.x]=2;
if(CurPos.y==outPos.y && CurPos.x==outPos.x){
PrintItem(CurPos);
printf("ok!\n");
break;
}
PrintItem(CurPos);
CurPos=NextPos(e.seat,0);
setp++;
getch();
}else{
Pop(s,&e);//如果不能通过就弹栈
if(e.di==4 && !IsEmpty(s)){
item[CurPos.y][CurPos.x]=8;
Pop(s,&e);
}
if(e.di<3){
e.di++;
Push(s,&e);
CurPos=NextPos(e.seat,e.di);
}
}
}while(!IsEmpty(s));
}
--结束END--
本文标题: C语言通过栈实现小人走迷宫
本文链接: https://lsjlt.com/news/140993.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0