可以使用广搜、DIJKSTRA等算法。 这里使用的是DIJKSTRA算法。 源代码: /*迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1392 Accepted: 771 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。 Input 一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。 Output 左上角到右下角的最短路径,格式如样例所示。 Sample Input 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 Sample Output (0, 0) (1, 0) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4) */ #include<stdio.h> #include<string.h> #define MAX_MAZE_SIZE 5 #define INFINATE_NUMBER 0X7F int gaiMaze[MAX_MAZE_SIZE][MAX_MAZE_SIZE]; typedef struct _MAZE_DISTANCE_ST_ { int iDistance; int iPreVertex_X; int iPreVertex_Y; }MAZE_DISTANCE_ST; MAZE_DISTANCE_ST gaiMazeDistance[MAX_MAZE_SIZE][MAX_MAZE_SIZE]; void DIJKSTRA() { int iLoopX; int iLoopY; memset(gaiMazeDistance,INFINATE_NUMBER,MAX_MAZE_SIZE*MAX_MAZE_SIZE*sizeof(MAZE_DISTANCE_ST)); gaiMazeDistance[0][0].iDistance = 0; for(iLoopY = 0; iLoopY < MAX_MAZE_SIZE; iLoopY++) { for(iLoopX = 0; iLoopX < MAX_MAZE_SIZE; iLoopX++) { if (0 == gaiMaze[iLoopY][iLoopX]) { /*right*/ if (MAX_MAZE_SIZE-1 != iLoopX &&0 == gaiMaze[iLoopY][iLoopX+1]) { if (gaiMazeDistance[iLoopY][iLoopX+1].iDistance > 1 + gaiMazeDistance[iLoopY][iLoopX].iDistance) { gaiMazeDistance[iLoopY][iLoopX+1].iDistance = 1 + gaiMazeDistance[iLoopY][iLoopX].iDistance; gaiMazeDistance[iLoopY][iLoopX+1].iPreVertex_Y = iLoopY; gaiMazeDistance[iLoopY][iLoopX+1].iPreVertex_X = iLoopX; } } /*down*/ if (MAX_MAZE_SIZE-1 != iLoopY &&0 == gaiMaze[iLoopY+1][iLoopX]) { if (gaiMazeDistance[iLoopY+1][iLoopX].iDistance > 1 + gaiMazeDistance[iLoopY][iLoopX].iDistance) { gaiMazeDistance[iLoopY+1][iLoopX].iDistance = 1 + gaiMazeDistance[iLoopY][iLoopX].iDistance; gaiMazeDistance[iLoopY+1][iLoopX].iPreVertex_Y = iLoopY; gaiMazeDistance[iLoopY+1][iLoopX].iPreVertex_X = iLoopX; } } /*left*/ if (0 != iLoopX &&0 == gaiMaze[iLoopY][iLoopX-1]) { if (gaiMazeDistance[iLoopY][iLoopX-1].iDistance > 1 + gaiMazeDistance[iLoopY][iLoopX].iDistance) { gaiMazeDistance[iLoopY][iLoopX-1].iDistance = 1 + gaiMazeDistance[iLoopY][iLoopX].iDistance; gaiMazeDistance[iLoopY][iLoopX-1].iPreVertex_Y = iLoopY; gaiMazeDistance[iLoopY][iLoopX-1].iPreVertex_X = iLoopX; } } /*up*/ if (0 != iLoopY &&0 == gaiMaze[iLoopY-1][iLoopX]) { if (gaiMazeDistance[iLoopY-1][iLoopX].iDistance > 1 + gaiMazeDistance[iLoopY][iLoopX].iDistance) { gaiMazeDistance[iLoopY-1][iLoopX].iDistance = 1 + gaiMazeDistance[iLoopY][iLoopX].iDistance; gaiMazeDistance[iLoopY-1][iLoopX].iPreVertex_Y = iLoopY; gaiMazeDistance[iLoopY-1][iLoopX].iPreVertex_X = iLoopX; } } } } } return; } void PrintPath( int iVertex_X,int iVertex_Y) { if ( 0 == iVertex_X && 0 == iVertex_Y) { printf("(%d, %d)/n",iVertex_Y,iVertex_X); return; } PrintPath(gaiMazeDistance[iVertex_Y][iVertex_X].iPreVertex_X,gaiMazeDistance[iVertex_Y][iVertex_X].iPreVertex_Y); printf("(%d, %d)/n",iVertex_Y,iVertex_X); return; } int main(void)//MazeMain { int iLoopY; for(iLoopY = 0; iLoopY < MAX_MAZE_SIZE; iLoopY++) { scanf("%d %d %d %d %d",&gaiMaze[iLoopY][0],&gaiMaze[iLoopY][1],&gaiMaze[iLoopY][2],&gaiMaze[iLoopY][3],&gaiMaze[iLoopY][4]); } DIJKSTRA(); PrintPath(MAX_MAZE_SIZE-1,MAX_MAZE_SIZE-1); return 0; }