#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAX 200
#define STACKINCREMENT 100
#define TRUE 1
#define FALSE 0
typedef struct _pos
{
int x;
int y;
}pos;
typedef struct _record
{
pos p;
int dic;
}rcd;
typedef rcd elemtype;
typedef int state;
typedef struct _stack
{
elemtype *base;
elemtype *top;
int stacksize;
}stack;
state initStack(stack *s)
{
s->base = (elemtype *)malloc(sizeof(elemtype) * MAX);
memset(s->base, 0, sizeof(elemtype) * MAX);
s->top = s->base;
s->stacksize = MAX;
return TRUE;
}
state destroyStack(stack *s)
{
free(s->base);
return TRUE;
}
state clearStack(stack *s)
{
s->top = s->base;
return TRUE;
}
state StackEmpty(stack *s)
{
return (s->top == s->base) ? TRUE : FALSE;
}
state Push(stack *s, elemtype x)
{
if((s->top - s->base) >= s->stacksize)
{
s->base = (elemtype *)realloc(s->base, s->stacksize + STACKINCREMENT);
s->top = s->base + s->stacksize;
s->stacksize += STACKINCREMENT;
return FALSE;
}
*s->top++ = x;
return TRUE;
}
state Pop(stack *s, elemtype *x)
{
if(s->top == s->base)
{
printf("栈空\n");
return FALSE;
}
*x = *--s->top;
return TRUE;
}
state GetTop(stack *s, elemtype *x)
{
if(s->top == s->base)
{
printf("栈空\n");
return FALSE;
}
*x = *(s->top - 1);
return TRUE;
}
#define N 10
int puzzle[N][N] = {{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}};
void printPuzzle()
{
int i;
int j;
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
if(puzzle[i][j] == 1)
{
printf("#");
}
else if(puzzle[i][j] == 2)
{
printf("*");
}
else if(puzzle[i][j] == 3)
{
printf("@");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
state isEnd(pos *a, pos *b)
{
if(a->x == b->x && a->y == b->y)
{
return TRUE;
}
else
{
return FALSE;
}
}
state printFoot(pos *a)
{
puzzle[a->x][a->y] = 3;
return TRUE;
}
state accessPos(pos *a)
{
return (puzzle[a->x][a->y] == 0) ? TRUE : FALSE;
}
void changePos(rcd *p, rcd *dst)
{
p->dic++;
dst->dic = 0;
switch(p->dic)
{
case 0://右
dst->p.x = p->p.x;
dst->p.y = p->p.y + 1;
break;
case 1://下
dst->p.x = p->p.x + 1;
dst->p.y = p->p.y;
break;
case 2://左
dst->p.x = p->p.x;
dst->p.y = p->p.y - 1;
break;
case 3://上
dst->p.x = p->p.x - 1;
dst->p.y = p->p.y;
break;
default:
printf("pos error\n");
}
}
int main()
{
int i;
int j;
stack s;
rcd cur;
pos dst;
rcd top;
//终点
dst.x = 8;
dst.y = 8;
//起点
cur.p.x = 1;
cur.p.y = 1;
cur.dic = 0;
initStack(&s);
printPuzzle();
while(!isEnd(&cur.p, &dst))//判断是否是终点
{
if(accessPos(&cur.p))//可以进入
{
printFoot(&cur.p);//留下足迹
Push(&s, cur);
cur.dic = 0;//走右侧位置
cur.p.y++;
}
else
{
if(!StackEmpty(&s))
{
Pop(&s, &top);
if(top.dic == 3)//四周都走过了
{
Pop(&s, &cur);
}
else
{
changePos(&top, &cur);//换一个方向
Push(&s, top);
}
}
else
{
break;
}
}
}
Push(&s, cur);
while(!StackEmpty(&s))
{
Pop(&s, &cur);
puzzle[cur.p.x][cur.p.y] = 2;
}
printPuzzle();
destroyStack(&s);
return 0;
}