#include"stdio.h" #include"stdlib.h" #define M 4 #define N 4 #define MAXSIZE 100 typedef struct { int inc; int iny; }SQ; typedef struct{ SQ data[MAXSIZE]; int top; }sqstack; void initstack(sqstack *s){ s=(sqstack *)malloc(sizeof(sqstack)); s->top =-1; } typedef struct{ int x; int y; int di; }box; struct{ int x; int y; }node[4]={{1,0},{0,1},{-1,0},{0,1}}; bool stackempty(sqstack *s){ if(s->top =-1)return true; else return false; } bool push(sqstack *&s,box temp){ if(s->top<MAXSIZE-1){ s->top ++; s->data[s->top].inc=temp.x ; s->data[s->top].iny=temp.y ; } else return false; } bool pop(sqstack *&s,box temp){ if(s->top !=-1){ temp.x =s->data [s->top ].inc; temp.y =s->data [s->top ].iny; s->top --; } else return false; } int mg[M+2][N+2]={ {1,1,1,1,1,1},{1,0,0,0,1,1},{1,0,1,0,0,1}, {1,0,0,0,1,1},{1,1,0,0,0,1},{1,1,1,1,1,1} }; bool path(sqstack *&s,int mg[M+2][N+2]){ int x,y,di; int line,col; box temp; mg[1][1]=-1; temp={1,1,-1}; push(s,temp); while(!stackempty(s)){ pop(s,temp); x=temp.x; y=temp.y; di=temp.di+1; while(di<4){ line=x+node[di].x; col=y+node[di].y; if(mg[line][col]==0){ temp={x,y,di}; push(s,temp); x=line,y=col; mg[line][col]=-1; if(x==M && y==N)return true; else di=0; } else di++; } } return false; } int main(){ sqstack *s; initstack(s); path(s,mg); while(!stackempty(s)){ printf("(%d,%d)",s->data[s->top].inc ,s->data[s->top].iny ); s->top--; } }
标签:懂哥,iny,temp,sqstack,int,top,迷宫,数据结构,data From: https://www.cnblogs.com/yylearn/p/16826838.html