#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define NULL 0
#define TRUE 1
#define FALSE 0
#define RIGHT 1
#define DOWN 2
#define LEFT 3
#define UP 4
const int width = 10;
const int height = 10;
int maze[height][width]={
//0,1,2,3,4,5,6,7,8,9
{1,1,1,1,1,1,1,1,1,1},//0
{1,0,0,1,0,0,0,1,0,1},//1
{1,0,0,1,0,0,0,1,0,1},//2
{1,0,0,0,0,1,1,0,0,1},//3
{1,0,1,1,1,0,0,0,0,1},//4
{1,0,0,0,1,0,0,0,0,1},//5
{1,0,1,0,0,0,1,0,0,1},//6
{1,0,1,1,1,0,1,1,0,1},//7
{1,1,0,0,0,0,0,0,0,1},//8
{1,1,1,1,1,1,1,1,1,1},//9
};
int foot[height][width]={0};
typedef struct PosType
{
int x;
int y;
}PosType;
typedef struct {
PosType pos;
int di;
}ElemType;
typedef struct
{
int **path;
int width;
int height;
}MazePath;
//地图
MazePath mazepath = {(int **)maze,width,height};
typedef struct _stack
{
ElemType data;
struct _stack* next;
}Stack , *PStack;
const int nLEN = sizeof(Stack);
void InitStack(PStack* head)
{
(*head) = (PStack)malloc(nLEN);
// (*head)->data = NULL;
(*head)->next = 0;
}
void DestroyStack(PStack *head)
{
PStack p = *head;
PStack p1 = p;
while(p)
{
p = p->next;
free(p1);
p1 = p;
}
}
void CleadStack(PStack *head)
{
PStack p = (*head)->next;
PStack p1 = p;
while(p)
{
p = p->next;
free(p1);
p1 = p;
}
}
int StackEmpty(PStack s)
{
s = s->next;
if(s!=NULL)
return FALSE;
else return TRUE;
}
int StackLength(PStack s)
{
s = s->next;
int nSize = 0;
while(s!=NULL)
{
nSize++;
s = s->next;
}
return nSize;
}
int GetTop(PStack s,ElemType *data)
{
if(!StackEmpty(s))
{
s = s->next;
*data = s->data;
return TRUE;
}
return FALSE;
}
int Push(PStack s,ElemType data)
{
PStack temp = (PStack)malloc(nLEN);
temp->data = data;
temp->next = NULL;
//
temp->next = s->next;
s->next = temp;
return TRUE;
}
int Pop(PStack s,ElemType *data)
{
if(StackEmpty(s))
return FALSE;
PStack temp = s->next;
*data = temp->data;
s->next = s->next->next;
free(temp);
return TRUE;
}
//能否通过
int Canpass(PosType pos)
{
int x = pos.x;
int y = pos.y;
//已经走过
if(foot[y][x]==1)
{
return FALSE;
}
//有障碍
int (*p)[width] = (int(*)[width])(mazepath.path);
if(p[y][x]==1)
{
return FALSE;
}
return 1;
}
//留下足迹
void MarkFoot(PosType pos)
{
int x = pos.x;
int y = pos.y;
foot[y][x] = 1;
}
PosType NextPos(PosType pos,int di)
{
PosType p;
if(di==RIGHT)
{
p.x = pos.x +1;
p.y = pos.y;
}
else if(di == LEFT)
{
p.x = pos.x-1;
p.y = pos.y;
}
else if(di==DOWN)
{
p.x = pos.x;
p.y = pos.y+1;
}
else if(di == UP)
{
p.x = pos.x;
p.y = pos.y-1;
}
return p;
}
void _printffoot(PStack s)
{
ElemType e;
int nlen = StackLength(s);
while(!StackEmpty(s))
{
Pop(s,&e);
printf("%-3d : (%d,%d)\n",nlen--,e.pos.x,e.pos.y);
}
}
int TestMazePath(PosType start , PosType end)
{
PStack s;
InitStack(&s);
PosType curpos;
curpos.x = start.x;
curpos.y = start.y;
ElemType e={0};
do
{
//可以通过
if(Canpass(curpos))
{
//留下标记
MarkFoot(curpos);
e.pos = curpos;
e.di = RIGHT;
//入栈
Push(s,e);
//找到出口了
if(curpos.x==end.x && curpos.y==end.y)
{
_printffoot(s);
return TRUE;
}
//取下一个pos
curpos = NextPos(curpos,RIGHT);
}
//不能通过
else
{
if(!StackEmpty(s))
{
//出栈
Pop(s,&e);
//这个点已搜索完了
if(e.di==UP && !StackEmpty(s))
{
MarkFoot(e.pos);
Pop(s,&e);
}
//还没搜完
if(e.di < 4)
{
//继续搜
e.di++;
Push(s,e);
curpos = NextPos(e.pos , e.di);
}
}
}
} while (!StackEmpty(s));
return FALSE;
}
int main()
{
PosType start={1,1};
PosType end={8,8};
int iret = TestMazePath(start , end);
// printf("%d",iret);
return 0;
}
/*
void test1()
{
PStack s;
InitStack(&s);
int n = 0;
scanf("%d",&n);
while(n)
{
Push(s,n%8);
n = n/8;
}
int data = 0;
while(!StackEmpty(s))
{
Pop(s,&data);
printf("%d",data);
}
CleadStack(&s);
Push(s,10);
Push(s,20);
Push(s,100);
int nn = StackLength(s);
printf("%d\n",nn);
DestroyStack(&s);
return ;
}
int IsLeft(char ch)
{
if(ch=='[')
return TRUE;
if(ch=='(')
return TRUE;
return FALSE;
}
int test2(char *psz,int nlen)
{
PStack s;
InitStack(&s);
for(int i=0;i<nlen;i++)
{
int ch = psz[i];
if(ch=='[' || ch=='(')
{
Push(s , ch);
}
else if(ch==']' || ch==')')
{
int temp = 0;
Pop(s,&temp);
if(ch==']' && temp!='[')
return FALSE;
if(ch==')' && temp!='(')
return FALSE;
}
}
if(StackEmpty(s))
return TRUE;
return FALSE;
}*/
栈应用-走迷宫
最新推荐文章于 2023-10-02 22:34:23 发布