如果對妳有幫助,歡迎采納~~~
void path(void)/*迷宮函數*/
{
element position;
int i,row,col,next_row,next_col,dir,top;
_Bool found = 0;
mark[1][1] = 1,top = 0;/*初始化標誌數組元素以及棧*/
stack[0].row = 1,stack[0].col = 1,stack[0].dir = 0;
while(top > -1 && !found)
{
position = delete(&top); /*將棧頂元素取出,*/
row = position.row; /*利用中間變量row,col,dir等候判斷*/
col = position.col;
dir = position.dir;
while(dir < 4 && !found)
{
next_row = row + move[dir].vert;
next_col = col + move[dir].horiz;
if(next_row == EXIT_ROW && next_col == EXIT_COL)
found = 1;
else if(!maze[next_row][next_col] && !mark[next_row][next_col])/*判斷下壹步可走並且沒走過,則入棧*/
{
mark[next_row][next_col] = 1;
position.row = row;
position.col = col;
position.dir = ++dir;
add(&top,position);/*合理則入棧*/
row = next_row;/*繼續向下走*/
col = next_col;dir = 0;
}
else
dir++;/*dir<4時,改變方向*/
}
if(found)/*判斷是否有出口*/
{
printf("the path is:\n");
printf("row col\n");
for(i = 0;i <= top;++i)
printf("%2d%5d",stack[i].row,stack[i].col);
printf("%2d%5d\n",row,col);
printf("%2d%5d\n",EXIT_ROW,EXIT_COL);
}
}
}