先使用栈,求处任意一条路径,再使用队列,求出最短路径。代码长了点,而且由于个人水平原因,质量不是很好……但以目前我的水平也就这样了……以后得道成仙以后再写更好的代码…… #include <stdio.h> #include <stdlib.h> #include <conio.h> #define FALSE 0 #define TRUE 1 #define MAXSIZE 1000 //最大的行数 void MGPathest(int,int,int,int,char **); //void Output(struct *,int); void Display(char **,int,int); void MGPath(int,int,int,int,char **); void main(){ int i,j; int x1,y1,x2,y2; int row,low; char *MG[MAXSIZE]; //存行指针 char *tmp; puts("Input the count of the row and the low:"); scanf("%d%d",&row,&low); for(i=0;i<row;i++){ MG[i]=malloc(low*sizeof(char)); tmp=MG[i]; if(i==0||i==row-1){ for(j=0;j<low;j++) tmp[j]='X'; }//if else { tmp[0]='X';tmp[low-1]='X'; for(j=1;j<low-1;j++) tmp[j]=' '; }//else }//for printf("The row of the matrix is %d and the low is %d/n",row,low); puts("So,the matrix is:"); Display(MG,row,low); //显示数组 puts("The /"X/" in the matrix is the wall..."); puts("Now tell me the entrance,input should be like this:/nx,y--x is for row and y is line "); scanf("%d,%d",&x1,&y1); puts("and the exit is:"); scanf("%d,%d",&x2,&y2); puts("Ok,tell me where is the other walls /nand I will tell you how can you get there"); puts("Input should be like this:x,y/nand there should be a space between two input"); puts("when you finish please input -1,-1"); while(scanf("%d,%d",&i,&j)){ if(i==-1&&j==-1) break; else { tmp=MG[i]; tmp[j]='X'; }//else }//while system("cls"); puts("the matrix is:/n"); Display(MG,row,low); puts("And..."); MGPath(x1,y1,x2,y2,MG); puts("/nDo you want to konw the shortest path?(N/Y?)"); if(getchar()=='Y'||getchar()=='y'){ puts("The shortest path is:"); MGPathest(x1,y1,x2,y2,MG); } }//main void Display(char *MG[],int row,int low){ //依据数组和行列号显示该数组 int i,j; char *tmp; printf(" "); for(i=0;i<low;i++) //显示边框 printf("%4d",i); for(i=0;i<row;i++){ //打印矩阵主体 printf("/n%3c",' '); for(j=0;j<low;j++) printf("+---"); printf("+/n%-3d",i); for(j=0,tmp=MG[i];j<low;j++) printf("| %c ",tmp[j]); printf("|"); }//for printf("/n%3c",' '); for(j=0;j<low;j++) printf("+---"); printf("+/n/n"); } void MGPath(int x1,int y1,int x2,int y2,char *MG[]){ //x1,y1为入口坐标…… struct { int i,j; int di; //下一个可以探索的方向 }MNode[MAXSIZE]; int top=0; int m,n; MNode[top].i=x1; MNode[top].j=y1; MNode[top].di=0; while(top>-1){ if(MNode[top].i==x2&&MNode[top].j==y2) break; //已找到路径 else{ //未找到路径 m=n=0; do{ switch(MNode[top].di){ case 0: //下一个方位为0 MNode[top].di++; m=MNode[top].i-1; n=MNode[top].j; break; case 1: MNode[top].di++; m=MNode[top].i; n=MNode[top].j+1; break; case 2: MNode[top].di++; m=MNode[top].i+1; n=MNode[top].j; break; case 3: MNode[top].di++; m=MNode[top].i; n=MNode[top].j-1; break; default: MG[MNode[top].i][MNode[top].j]=' '; //空格表示可以通过.MG可以这样使用……!!! --top; break; }//switch }while(MG[m][n]!=' '&&top>-1); if(top>-1){ //找到 MG[MNode[top].i][MNode[top].j]='X'; //防止走重复的路线 top++; MNode[top].i=m; MNode[top].j=n; MNode[top].di=0; }//if }//else }//while if(top>-1) { //找到 printf("The path from (%d,%d) to (%d,%d) is:/n",x1,y1,x2,y2); for(m=0;m<=top;m++){ printf("(%d,%d) ",MNode[m].i,MNode[m].j); MG[MNode[m].i][MNode[m].j]=' '; if((m+1)%3==0) puts(""); }//for }//if else printf("Oh no,There is no path from (%d,%d) to (%d,%d)",x1,y1,x2,y2); }//MGPath void MGPathest(int x1,int y1,int x2,int y2,char *MG[]){ struct { //队列 int i,j; int pre; }Qu[MAXSIZE]; int front=-1,rear=0; int m,n; int di; int next,tmp; int count; int found=FALSE; Qu[rear].i=x1; Qu[rear].j=y1; Qu[rear].pre=front; MG[x1][y1]='X'; while(!found&&front<rear){ front++; m=Qu[front].i; n=Qu[front].j; if(m==x2&&n==y2) found=TRUE; else { //未找到 for(di=0;di<4;di++){ switch(di){ case 0:m=Qu[front].i-1;n=Qu[front].j;break; case 1:m=Qu[front].i;n=Qu[front].j+1;break; case 2:m=Qu[front].i+1;n=Qu[front].j;break; case 3:m=Qu[front].i;n=Qu[front].j-1;break; }//switch if(MG[m][n]==' '){ rear++; Qu[rear].i=m; Qu[rear].j=n; Qu[rear].pre=front; MG[m][n]='X'; }//if }//for }//else }//while if(found){ //找到,则打印之 for(next=front;front!=-1;next=front,front=tmp){ tmp=Qu[front].pre; Qu[front].pre=next; MG[Qu[front].i][Qu[front].j]=' '; }//for for(front=0,count=1;front!=Qu[front].pre;front=Qu[front].pre,count++){ printf("(%d,%d) ",Qu[front].i,Qu[front].j); if(count%3==0) puts(""); }//for printf("(%d,%d) ",Qu[front].i,Qu[front].j); }//if else printf("Oh no,there is no such path!"); } /*void Output(struct *Qu,int front){ }*/