1.這個程序是動態的,運行後自動找到迷宮的出路。
這個程序對於剛學C語言的人來說意義重大。
3.四周都是墻,坐標(1,1)是入口,右下角是出口。
聲明:本程序不能用VC調試,需要修改。
這個程序的調試工具是TC。.....................
#包含“graphics.h”
#包含“dos.h”
#包含" stdlib.h "
#包含“process.h”
#define MAX_COL 14/*定義迷宮大小*/
#define MAX_ROW 14
typedef結構
{ int vert
int horiz
}抵消;
mapture(int i,int j,int k);/*迷宮式,(I,j) k模式*/
init maze();/*初始化迷宮陣列*/
findmaze(int i,int j);/*找到(I,j)要走,標記為*/
map maze();/*畫原始迷宮*/
int findpath(int row,int col);/*遞歸函數,求迷宮路徑*/
mapbar();/*畫壹個正方形*/
init grap();/*初始化VGA*/
print();/*迷宮結束後,輸出是否成功*/
int startx=50,starty = 50/*圖紙的屏幕坐標*/
int maze[MAX _ ROW][MAX _ COL];
偏移量move[8]={{0,1},{1,1},{-1,1},{1,0},{-1,0},{0,-1},{1,-1},{-1 },1/*朝8個方向看*/
Initmaze()/*初始化迷宮數組*/
{ int i,j;
for(I = 0;我& ltMAX _ ROWI++)/*墻的迷宮設置為1 */
{ maze[I][0]= 1;
迷宮[I][MAX _ COL-1]= 1;
}
for(I = 0;我& ltMAX _ COLi++)
{ maze[0][I]= 1;
迷宮[MAX _ ROW-1][I]= 1;
}
randomize();
for(I = 1;我& ltMAX _ ROW-1;I++)/*隨機生成迷宮圖形1表示不通0表示可行*/
for(j = 1;j & ltMAX _ COL-1;j++)
{
maze[I][j]= random(2);
}
}
Findmaze(int i,int j)/* Find (i,j) to go */
{
mapture(j,I,2);/*圖表上的標記*/
睡眠(1);
}
Returnmaze(int i,int j)/* Find (i,j) to go,但mark */
{
mapture(j,I,3);/*圖表上的標記*/
睡眠(1);
}
Print(int i)/*迷宮後輸出是否成功*/
{ settextstyle(1,0,5);
if(i==1)
outtextxy(340,400,"真實路徑!");
else if(i==2)
outtextxy(340,400,“沒有路徑!”);
}
Int findpath(int row,int col)/*通過遞歸找到迷宮*/
{ int direct,next_row,next _ col
直接= 0;
迷宮[1][1]= 2;
mapture(1,1,2);
睡眠(1);
while(直接& lt8)/*朝8個方向看*/
{ next_row=row+move[direct]。垂直;/*設置下壹個坐標*/
next_col=col+move[direct]。horiz
If(maze[next _ row][next _ col]= = 0)/*如果可以去,標記*/
{ maze[next _ row][next _ col]= 2;
findmaze(next_row,next _ col);
if(next _ ROW = =(MAX _ ROW-2)& amp;& ampNext_col==(MAX_COL-2))/*找到退出程序的出口*/
{ print(1);
getch();
退出(0);
}
其他
findpath(next_row,next _ col);/*沒有繼續遞歸的出口*/
迷宮[下壹行][下壹列]= 3;
returnmaze(next_row,next _ col);
}
direct++;
}
返回(行);
}
TC調試好