#include<stdio.h>
#include<stdlib.h>
#define MAX 3
typedef struct step
{
int x,y,d;
step *next;
}Link,*Stack;
int map[MAX+2][MAX+2];
int D[4][2]={0,1,1,0,0,-1,-1,0};
int d=-1;
int x=1,y=1;
int YESorNO=0;
void InitMap()
{
int i,j;
for(i=0;i<MAX+2;i++)
for(j=0;j<MAX+2;j++)
scanf("%d",&map[i][j]);
}
Stack InStack(Stack top,int x,int y,int d)
{
Stack p=(Stack)malloc(sizeof(Link));
p->x=x;
p->y=y;
p->d=d;
p->next=top;
top=p;
return top;
}
Stack OutStack(Stack top)
{
if(top==NULL)
return NULL;
else{
top=top->next;
}
return top;
}
int i,j;
int main()
{
InitMap();
Stack top=NULL;
top=InStack(top,1,1,-1);
map[1][1]=-1;
while(top!=NULL){
x=top->x;
y=top->y;
d=(top->d)+1;
top=OutStack(top);
while(d<4){
i=x+D[d][0];
j=y+D[d][1];
if(map[i][j]==0){
top=InStack(top,x,y,d);
if(i==3&&j==3){
top=InStack(top,i,j,d);
YESorNO=1;
break;
}else{
x=i;
y=j;
map[x][y]=-1;
d=0;
}
}else{
d++;
}
}
if(YESorNO==1)
break;
}
while(top!=NULL){
printf("(%d,%d)",top->x,top->y);
top=top->next;
}
return 0;
}
运行结果如下: