#include
#include
#include
#include
#define MAX_WIDTH ? ?30
#define MAX_HEIGHT ? 30
typedef struct _step
{
? ?int x; ? ? ? ? ? ?//行坐標
? ?int y; ? ? ? ? ? ?//列坐標
}STEP;
typedef struct _matrix
{
? ?int data[MAX_WIDTH+2][MAX_WIDTH+2]; //迷宮數據,0:表示有路,1:表示墻
? ?int width; ? ? ? ? ? ? ? ?//矩陣(迷宮)的寬,包括最左和最有2堵墻
? ?int height; ? ? ? ? ? ? ? ?//矩陣(迷宮)的寬,包括頂部和底部2堵墻
? ?STEP entrance; ? ? ? ? ? ? ? ?//迷宮入口
? ?STEP exit; ? ? ? ? ? ? ? ?//迷宮出口
}MATRIX;
MATRIX g_matrix= ? ? ? ? ? ? ? ?//初始化為一個迷宮,程序也能從文件中讀入迷宮數據
{
? ?{
? ? ? ?{1, 1, 1, 1, 1, 1, 1},
? ? ? ?{1, 0, 0, 0, 0, 0, 1},
? ? ? ?{1, 1, 0, 1, 0, 1, 1},
? ? ? ?{1, 0, 0, 1, 1, 1, 1},
? ? ? ?{1, 0, 1, 0, 0, 0, 1},
? ? ? ?{1, 0, 0, 0, 1, 0, 1},
? ? ? ?{1, 1, 1, 1, 1, 1, 1},
? ?},
? ?7,7, ? ? ? ? ? ? ? ? ? ?//7行,7列,包括4堵墻
? ?{1,1}, ? ? ? ? ? ? ? ? ? ?//入口坐標
? ?{5,5} ? ? ? ? ? ? ? ? ? ?//出口坐標
};
static STEP ?s_shift[]=
{
? ?{1,0}, ? ? ? ?//向右走, x++, y 不變
? ?{0,1}, ? ? ? ?//向下走, ?x 不變, y++
? ?{-1,0}, ? ? ? ?//向左走, ?x--, y不變
? ?{0,-1} ? ? ? ?//向上走, ?x 不變, y--
};
void print_paths(STEP path[],int path_len) //打印走迷宮的路徑
{
? ?int i;
? ?for (i=0;i
? ?{
? ? ? ?if (i>0)
? ? ? ? ? ?printf("->");
? ? ? ?printf("(%d,%d)",path[i].x, path[i].y);
? ?}
}
void print_Matrix(MATRIX* pMatrix) ? ? ? ?//打印迷宮數據,迷宮數據包含4堵墻
{
? ?int i,j;
? ?for (i=0;iheight;i++)
? ?{
? ? ? ?for (j=0;jwidth;j++)
? ? ? ?{
? ? ? ? ? ?if (j!=0)
? ? ? ? ? ? ? ?printf(" ");
? ? ? ? ? ?printf("%d",pMatrix->data[i][j]);
? ? ? ?}
? ? ? ?printf("\n");
? ?}
}
int search_path(int matric[MAX_WIDTH+2][MAX_WIDTH+2],
? ? ? ? ? ? ? ?STEP path[],int path_len,STEP exit)
{
? ?while (1)
? ?{
? ? ? ?int i,bFind;
? ? ? ?STEP newPos;
for (bFind=0,i=0;i<4;i++) ?//從右,下,左,上,查找新的可以走的位置
{
newPos.x = path[path_len-1].x + s_shift[i].x;
newPos.y = path[path_len-1].y + s_shift[i].y;
if ( path_len==1 )
{
if ( g_matrix.data[newPos.x][newPos.y]==0)
{
bFind=1; break; //找到一個位置
}
? ? ? ? ? ?}
? ? ? ? ? ?// path[path_len-1]表示當前位置,path[path_len-2]表示上一步的位置,
? ? ? ? ? ?// 如果新的位置等于上一個位置,將陷入循環,故要求新的位置不能是上一步的位置
? ? ? ? ? ?else if ( (newPos.x != path[path_len-2].x || newPos.y!=path[path_len-2].y) && g_matrix.data[newPos.x][newPos.y]==0)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?bFind=1; break; ? ? ? ?//找到一個位置
? ? ? ? ? ?}
? ? ? ?}
if (bFind)
{
path[path_len++]=newPos; //將新的位置追加到path數組,路徑長度+1
if ( newPos.x==exit.x && newPos.y==exit.y)
return path_len;
}
else
{
matric[path[path_len-1].x][path[path_len-1].y]=1; //從當前位置,無路可走,將當前位置標記為墻
path_len--; ? ? ? ?//回退一步
if ( path_len==0)
return path_len;
}
}
}
int readMatrix(char *file)
{
? ?char line[1024];
? ?FILE *fp=NULL;
? ?int i,j,x,y;
? ?fp=fopen(file,"rt");
? ?if (fp==NULL)
? ?{
? ? ? ?printf("Can not open file %s\n",file);
? ? ? ?return 0;
? ?}
? ?memset(&(g_matrix),0,sizeof(g_matrix));
? ?fgets(line,sizeof(line)-1,fp);
sscanf(line,"%d %d",&x,&y); ? ? ? ? ? ? ? ? ? ?//讀入迷宮的行數和列數
if ( x>MAX_WIDTH || y>MAX_HEIGHT)
{
printf("The Matrix is too large\n");
fclose(fp);
return 0;
}
? ?g_matrix.width=x+2; ? ? ? ? ? ? ? ? ? ? ? ? ? ?//在4條邊增加4堵墻,故寬和高增加2
? ?g_matrix.height=y+2;
for (j=0;j
{
g_matrix.data[0][j]=1; ? ? ? ? ? ? ? ? ? ?//第一行為墻
g_matrix.data[g_matrix.height-1][j]=1; ? ?//最后一行為墻
}
? ?for (i=0;i
? ?{
? ? ? ?g_matrix.data[i][0]=1; ? ? ? ? ? ? ? ? ? ?//最左邊的列為墻
? ? ? ?g_matrix.data[i][g_matrix.width-1]=1; ? ?//最右邊的列為墻
? ?}
for (i=1;i
{
char *p;
fgets(line,sizeof(line)-1,fp);
j=1;
p=line;
while (1)
{
while ( *p==' ' ||*p== 9)//跳過空格符號
p++;
? ? ? ? ? ?if ( *p>='0' && *p<='9')
? ? ? ? ? ?{
? ? ? ? ? ? ? ?sscanf(p,"%d",&(g_matrix.data[i][j])); ?//讀入地i行j列的數據
? ? ? ? ? ? ? ?while ( *p>='0' && *p<='9')
? ? ? ? ? ? ? ? ? ?p++; ? ? ? ? ? ?//數據已經讀入,跳過當前的數字
? ? ? ? ? ? ? ?j++;
? ? ? ? ? ?}
? ? ? ? ? ?if (j>=g_matrix.width-2)
? ? ? ? ? ? ? ?break;
? ? ? ?}
? ?}
fgets(line,sizeof(line)-1,fp);
//讀入入口的行坐標和列坐標,和出口的行坐標,列坐標
sscanf(line,"%d %d %d %d",&(g_matrix.entrance.x),&(g_matrix.exit.y),&(g_matrix.exit.x),&(g_matrix.exit.y));
fclose(fp); fp=NULL;
g_matrix.entrance.x++; ?//增加了一列是墻,故入口橫坐標+1
g_matrix.entrance.y++; ?//增加了一行是墻,故入口縱坐標+1
g_matrix.exit.x++; ? ? ?//增加了一列是墻,故入口橫坐標+1
g_matrix.exit.y++; ? ? ? ?//增加了一列是墻,故入口縱坐標+1
? ?return 1;
}
int main()
{
? ?STEP path[MAX_WIDTH*MAX_HEIGHT];
? ?int step_count;
? ?if ( !readMatrix("matrix.txt") ) ?//不使用初始化的數據,從文件中讀入迷宮數據
? ?{
? ? ? ?return 0; ? ? ? ? ? ? ? ? ? ? ?//讀取失敗,直接跳出
}
? ?printf("The matrix is\n");
? ?print_Matrix(&g_matrix);
? ?path[0]=g_matrix.entrance; //將入口位置放入path數組
? ?step_count = search_path(g_matrix.data,path,1,g_matrix.exit); //查找一條路徑,路徑的每一步的坐標放入path數組
? ?if (step_count>0) ? ? ? ? ? ? ? ? ? ? ? ? ?//找到一條出路,步數>0
? ?{
? ? ? ?printf("\nThe path is\n");
? ? ? ?print_paths(path,step_count);
? ?}
? ?else ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //步數<=0, 沒有找到出路
printf("No solution\n");
return 0;
} ? ?
? ?