作者:鬼鬼千年
#include<stdio.h>
int maze[10][10] = {
{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,1,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};/*初始化迷宮*/
int mark[10][10] = {0};/*初始化標誌位,0代表沒走過,1代表走過*/
/*方向*/
typedef struct{
short int vert;
short int horiz;
}offsets;
offsets move[4] = {{0,1},{1,0},{0,-1},{-1,0}};/*北,東,南,西*/
/*迷宮類型*/
typedef struct element{
short int row;
short int col;
short int dir;
}element;
element stack[100];
void path(void);
element del(int* top);
void add(int* top,element item);
element del(int* top)/*出棧,top指向棧頂元素*/
{
if(*top == -1)
{
printf("stack is empty!\n");
}
return stack[(*top)--];
}
void add(int* top,element item)/*入棧*/
{
if(*top >= 100)
{
printf("stack is full!\n");
return;
}
stack[++*top] = item;
}
void path(void)/*迷宮函數*/
{
element position;
int i,row,col,next_row,next_col,dir,top;
int found = 0;
mark[1][8] = 1,top = 0;/*初始化標誌數組元素以及棧*/
stack[0].row = 1,stack[0].col = 8,stack[0].dir = 0;
while(top > -1 && !found)
{
position= del(&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(row == 7 && col == 1)
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",7,1);
}
}
if(found==0)
printf("沒有路徑\n");
}
int main(void)
{
path();
return 0;
}