栈是限定仅在表尾进行插入和删除操作的线性表。“栈”者,存储货物或供旅客住宿的地方,可引申为仓库、中转站,引入到计算机领域里,就是指数据暂时存储的地方,所以才有进栈、出栈的说法。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CwDoVSpo-1572180598700)(https://pic.baike.soso.com/ugc/baikepic2/11312/20171225141931-185064715_png_286_300_64293.jpg/0)]
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define OVERFALSE -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType sleep;
ElemType wolf;
ElemType human;
ElemType cabbage;
}SElemType;
typedef struct{
SElemType * base;
SElemType * top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S){
S.base=(SElemType )malloc(STACK_INIT_SIZEsizeof(SElemType));
if(!S.base) exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}//InitStack
Status GetTop(SqStack S,SElemType &e){
if(S.topS.base) return ERROR;
e=*(S.top-1);
return OK;
}//GetTop
Status Push(SqStack &S,SElemType e){
if(S.top-S.base>=S.stacksize){
S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base) exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}//Push
Status Pop(SqStack &S,SElemType &e){
if(S.topS.base) return ERROR;
e=*–S.top;
return OK;
}//Pop
Status Empty(SqStack S){
if(S.top==S.base)
return ERROR;
else
return OK;
}//Empty
Status Judge(SElemType e,SElemType q[],int i){
for(int j=0;j<=i-1;j++){
if(q[j].humane.human&&q[j].cabbagee.cabbage&&q[j].sleepe.sleep&&q[j].wolfe.wolf)
return ERROR;
}
//printf(“Judge:”);
//printf("%d %d %d %d\n",e.cabbage,e.human,e.sleep,e.wolf);
if( (e.human0&&e.sleep0&&e.cabbage0&&e.wolf0)||(e.human1&&e.sleep1&&e.cabbage0&&e.wolf0)||(e.human0&&e.sleep1&&e.cabbage0&&e.wolf0)
||(e.human1&&e.sleep1&&e.cabbage1&&e.wolf0)||(e.human0&&e.sleep0&&e.cabbage1&&e.wolf0)||(e.human1&&e.sleep0&&e.cabbage1&&e.wolf1)
||(e.human0&&e.sleep0&&e.cabbage1&&e.wolf1)||(e.human1&&e.sleep1&&e.cabbage1&&e.wolf1)||(e.human1&&e.sleep1&&e.cabbage0&&e.wolf1)
||(e.human0&&e.sleep0&&e.cabbage0&&e.wolf1))
return OK;
else
return ERROR;
}
void MazePath(){
SqStack S;
InitStack(S);
SElemType e1,q[1000],e2;
int i=0;
e1.cabbage=0,e1.human=0,e1.sleep=0,e1.wolf=0;
Push(S,e1);
while(Empty(S)){
Pop(S,e1);
q[i].cabbage=e1.cabbage;
q[i].human=e1.human;
q[i].sleep=e1.sleep;
q[i].wolf=e1.wolf;
i++;
if(e1.cabbage1&&e1.human1&&e1.sleep1&&e1.wolf1){
printf(“The path have found!\n”);
break;
}
if(e1.human==0){
e1.human=1;
if(Judge(e1,q,i)){
Push(S,e1);
}
if(e1.cabbage==0){
e1.cabbage=1;
if(Judge(e1,q,i)){
Push(S,e1);
}
e1.cabbage=0;
}//cabbage
if(e1.sleep==0){
e1.sleep=1;
if(Judge(e1,q,i)){
Push(S,e1);
}
e1.sleep=0;
}//sleep
if(e1.wolf==0){
e1.wolf=1;
if(Judge(e1,q,i)){
Push(S,e1);
}
e1.wolf=0;
}//wolf
}
if(e1.human==1){
e1.human=0;
if(Judge(e1,q,i)){
Push(S,e1);
}
if(e1.cabbage==1){
e1.cabbage=0;
if(Judge(e1,q,i)){
Push(S,e1);
}
e1.cabbage=1;
}//cabbage
if(e1.sleep==1){
e1.sleep=0;
if(Judge(e1,q,i)){
Push(S,e1);
}
e1.sleep=1;
}//sleep
if(e1.wolf==1){
e1.wolf=0;
if(Judge(e1,q,i)){
Push(S,e1);
}
e1.wolf=1;
}//wolf
}
}
for(int y=0;y<i;y++){
printf("白菜 人 羊 狼\n");
printf("%d %d %d %d\n",q[y].cabbage,q[y].human,q[y].sleep,q[y].wolf);
}
}
main(){
MazePath();
/SqStack S;
SElemType e,r;
InitStack(S);
e.cabbage=0;
e.human=0;
e.sleep=0;
e.wolf=0;
Push(S,e);
if(Empty(S)){
Pop(S,r);
printf("%d %d %d %d",r.cabbage,r.human,r.sleep,r.wolf);
}/
return 0;
}