當前位置:首頁 » 編程語言 » 啟發式迷宮搜索c語言
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

啟發式迷宮搜索c語言

發布時間: 2023-05-24 13:57:23

Ⅰ 迷宮問題,c語言

#include<stdio.h>
int main(void)
{
int maze[100][100];
int MAZE[100][100];
int m,n;
int p,q;

printf("輸入迷宮的行數m,列隱氏數n:\n");
scanf("%d%d",&m,&n);
for(p=0;p<=n+1;p++){
maze[0][p]=1;
maze[m+1][p]=1;
}
for(p=1;p<=m;p++){
maze[p][0]=1;
maze[p][n+1]=1;
printf("寬做輸入第%d行迷宮:\n",p);
for(q=1;q<=n;q++){
scanf("%d",&maze[p][q]);
MAZE[p][q]=maze[p][q];

}
}

struct location{
int row;
int col;
}way[100];

int movehoriz[8]={-1,0,1,1,1,0,-1,-1};
int movevert[8]={1,1,1,0,-1,-1,-1,0};

int endrow=m;
int endcol=n;

way[0].row=1;
way[0].col=1;
int start=3;

int i=0;
int k;
int j;
int found=0;

while(!found){
for(k=start;k<start+8;k++){
if((maze[way[i].row+movevert[k%8]][way[i].col+movehoriz[k%8]]==0)&&((i==0)||((way[i].row!=way[i-1].row)||(way[i].col!=way[i-1].col)))){
way[i+1].row=way[i].row+movevert[k%8];
way[i+1].col=way[i].col+movehoriz[k%8];
i++;
start=(k+5)%8;
break;
}
if((way[i].row==endrow)&&(way[i].col==endcol)){
break;
}
if((maze[way[i].row+movevert[k]][way[i].col+movehoriz[k]]==0)&&(way[i].row==way[i-1].row)&&(way[i].col==way[i-1].col)){
way[i].row=0;
way[i].col=0;
maze[way[i].row][way[i].col]=1;
i--;
start=(start+4)%8;
}

}
if(k>=start+8){
break;
}
if((way[i].row==endrow)||(way[i].col==endcol)){
found=1;
break;
}

}

if(found){
for(j=0;j<=i;j++){
printf("maze[%d][%d]\n",way[j].row,way[j].col);
}
}
else{
printf("The maze does not have a path\n");
}

}

QQ:366597114 不一定完全對。也許有灶巧散小錯誤。有問題可以來問我哈

Ⅱ 用C語言編寫一個迷宮程序,知道出處也行 ~~!

#include<stdio.h>
#include<stdlib.h>
#define M 15
#define N 15
struct mark //定義迷宮內點的坐標類型
{
int x;
int y;
};

struct Element //"戀"棧元素,嘿嘿。。
{
int x,y; //x行迅枯核,y列
int d; //d下一步的方向
};

typedef struct LStack //鏈棧
{
Element elem;
struct LStack *next;
}*PLStack;

/*************棧函數****************/

int InitStack(PLStack &S)//構造空棧
{
S=NULL;
return 1;
}

int StackEmpty(PLStack S)//判斷棧是否為空
{
if(S==NULL)
return 1;
else
return 0;
}

int Push(PLStack &S, Element e)//壓入新數據元素
{
PLStack p;
p=(PLStack)malloc(sizeof(LStack));
p->elem=e;
p->next=S;
S=p;
return 1;
}

int Pop(PLStack &S,Element &e) //棧頂元素出棧
{
PLStack p;
if(!StackEmpty(S))
{
e=S->elem;
p=S;
S=S->next;
free(p);
return 1;
}
else
return 0;
}

/***************求迷宮路徑函數***********************/
void MazePath(struct mark start,struct mark end,int maze[M][N],int diradd[4][2])
{
int i,j,d;int a,b;
Element elem,e;
PLStack S1, S2;
InitStack(S1);
InitStack(S2);
maze[start.x][start.y]=2; //入口點作上標記
elem.x=start.x;
elem.y=start.y;
elem.d=-1; //開始為-1
Push(S1,elem);
while(!StackEmpty(S1)) //棧不為空 有路徑可走
{
Pop(S1,elem);
i=elem.x;
j=elem.y;
d=elem.d+1; //下一敗陪個方向
while(d<4) //試探東南西北各個方向
{
a=i+diradd[d][0];
b=j+diradd[d][1];
if(a==end.x && b==end.y && maze[a][b]==0) //如果到了出口
{
elem.x=i;
elem.y=j;
elem.d=d;
Push(S1,elem);
elem.x=a;
elem.y=b;
elem.d=886; //方向輸出為-1 判斷是否到了出口
Push(S1,elem);
printf("\n0=東 1=南 2=西 3=北 886為則走出迷宮\n\n通路為:(行坐標,列坐標,方向)\n");
while(S1) //逆置序列 並輸出迷宮路徑序列
{
Pop(S1,e);
Push(S2,e);
}
while(S2)
{
Pop(S2,e);
printf("-->(%d,%d,%d)",e.x,e.y,e.d);
}
return; //跳出兩層循環,本來用break,但發現出錯,exit又會結束程序,選用return還是不錯滴
}
if(maze[a][b]==0) //找到可以前進的非出口的點
{
maze[a][b]=2; //標記走過此點
elem.x=i;
elem.y=j;
elem.d=d;
Push(S1,elem); //當前位置畝掘入棧
i=a; //下一點轉化為當前點
j=b;
d=-1;
}
d++;
}
}
printf("沒有找到可以走出此迷宮的路徑\n");
}

/*************建立迷宮*******************/
void initmaze(int maze[M][N])
{
int i,j;
int m,n; //迷宮行,列 [/M]

printf("請輸入迷宮的行數 m=");
scanf("%d",&m);
printf("請輸入迷宮的列數 n=");
scanf("%d",&n);
printf("\n請輸入迷宮的各行各列:\n用空格隔開,0代表路,1代表牆\n",m,n);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%d",&maze[i][j]);
printf("你建立的迷宮為(最外圈為強)...\n");
for(i=0;i<=m+1;i++) //加一圈圍牆
{
maze[i][0]=1;
maze[i][n+1]=1;
}
for(j=0;j<=n+1;j++)
{
maze[0][j]=1;
maze[m+1][j]=1;
}
for(i=0;i<=m+1;i++) //輸出迷宮
{
for(j=0;j<=n+1;j++)
printf("%d ",maze[i][j]);
printf("\n");
}
}

void main()
{
int sto[M][N];
struct mark start,end; //start,end入口和出口的坐標
int add[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//行增量和列增量 方向依次為東西南北 [/M]

initmaze(sto);//建立迷宮

printf("輸入入口的橫坐標,縱坐標[逗號隔開]\n");
scanf("%d,%d",&start.x,&start.y);
printf("輸入出口的橫坐標,縱坐標[逗號隔開]\n");
scanf("%d,%d",&end.x,&end.y);

MazePath(start,end,sto,add); //find path
system("PAUSE");
}

Ⅲ c語言的迷宮問題

//尋路_帶權重_帶障礙_最短_文件地圖_不閃------wlfryq------//
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>

typedefstruct
{
intx;
inty;
}_PT;

_PTpt;
introw=0,col=0;

//設置CMD窗口游標位置
voidsetxy(intx,inty)
{
COORDcoord={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
//獲取當前CMD當前游標所在位置
voidgetxy()
{
HANDLEhConsole=GetStdHandle(STD_OUTPUT_HANDLE);
COORDcoordScreen={0,0};//游標位置
CONSOLE_SCREEN_BUFFER_INFOcsbi;
if(GetConsoleScreenBufferInfo(hConsole,&csbi))
{
pt.x=csbi.dwCursorPosition.X;
pt.y=csbi.dwCursorPosition.Y;
}
}

typedefstruct
{
intx;
inty;
inttype;
intv;
}PT;

PT**s=NULL,stack[50],start,end,c;
intpos=0;

voidprt()
{
inti,j;
system("cls");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(s[i][j].type==1)
{
printf("■");
}
elseif(i==end.x&&j==end.y)
{
printf("★");
}
elseif(i==c.x&&j==c.y)
{
printf("◎");
}
else
{
printf("");
}
}
printf(" ");
}
Sleep(500);
}

voidstack_in(PTa)
{
stack[pos++]=a;
}

PTstack_out()
{
inti;
PTt;
t=stack[0];
for(i=0;i<pos-1;i++)
{
stack[i]=stack[i+1];
}
pos--;
returnt;
}

voidfun()
{
PTa;
intx,y,v;
while(1)
{
if(pos==0)
{
break;
}
a=stack_out();
x=a.x;
y=a.y;
if(x==start.x&&y==start.y)
{
break;
}
v=s[x][y].v;
if(x-1>=0&&s[x-1][y].type==0&&(s[x-1][y].v==-1||s[x-1][y].v>v+1))
{
s[x-1][y].v=v+1;
stack_in(s[x-1][y]);
}
if(x+1<=row-1&&s[x+1][y].type==0&&(s[x+1][y].v==-1||s[x-1][y].v>v+1))
{
s[x+1][y].v=v+1;
stack_in(s[x+1][y]);
}
if(y-1>=0&&s[x][y-1].type==0&&(s[x][y-1].v==-1||s[x-1][y].v>v+1))
{
s[x][y-1].v=v+1;
stack_in(s[x][y-1]);
}
if(y+1<=col-1&&s[x][y+1].type==0&&(s[x][y+1].v==-1||s[x-1][y].v>v+1))
{
s[x][y+1].v=v+1;
stack_in(s[x][y+1]);
}
}
}

voidgo(intx,inty)
{
printf(" 按任意鍵開始 ");
getchar();
intv;
while(1)
{
if(x==end.x&&y==end.y)
{
setxy(0,y+2);
printf("end....");
return;
}
v=s[x][y].v;
if(v==0)
{
return;
}
if(x-1>=0&&s[x-1][y].v==v-1)
{
c=s[x-1][y];
setxy(y*2,x);
x=x-1;
printf("");
setxy(y*2,x);
printf("◎");
Sleep(500);
continue;
}
elseif(x+1<=row-1&&s[x+1][y].v==v-1)
{
c=s[x+1][y];
setxy(y*2,x);
x++;
printf("");
setxy(y*2,x);
printf("◎");
Sleep(500);
continue;
}
elseif(y-1>=0&&s[x][y-1].v==v-1)
{
c=s[x][y-1];
setxy(y*2,x);
y--;
printf("");
setxy(y*2,x);
printf("◎");
Sleep(500);
continue;
}
elseif(y+1<=col-1&&s[x][y+1].v==v-1)
{
c=s[x][y+1];
setxy(y*2,x);
y++;
printf("");
setxy(y*2,x);
printf("◎");
Sleep(500);
continue;
}
}
printf(" returngo ");
system("pause");
}

voidGetMapFromFile()
{
inti,j,x,y,len;
charch[50]={''};
FILE*fp=fopen("e:\map1.txt","r");
if(fp==NULL)
{
printf("openfilefailed. ");
return;
}
x=0;

while(!feof(fp))
{
fgets(ch,50,fp);
row++;
}
col=strlen(ch);
fseek(fp,0L,SEEK_SET);
while(!feof(fp))
{
fgets(ch,50,fp);
if(s==NULL)
{
len=strlen(ch);
for(i=len-1;i>0;i--)
{
if(ch[i]!='0'&&ch[i]!='1')
{
ch[i]='';
}
else
{
break;
}
}
len=strlen(ch);
s=(PT**)malloc(sizeof(PT*)*row);
for(j=0;j<len;j++)
{
*(s+j)=(PT*)malloc(sizeof(PT)*len);
}
}
y=0;
for(i=0;i<len;i++)
{
s[x][y].type=ch[i]-48;
s[x][y].x=x;
s[x][y].y=y;
s[x][y].v=-1;
y++;
}
x++;
}
start=s[row-2][1];
end=s[row-2][len-2];
fclose(fp);
}

intmain()
{
GetMapFromFile();
inti,j;
intx,y;
x=end.x;
y=end.y;
s[x][y].v=0;
stack_in(end);
fun();
c=start;
prt();
go(start.x,start.y);
return0;
}

Ⅳ 如何用C語言編寫一個迷宮程序

#include x0dx0a#include x0dx0a#define M 15 x0dx0a#define N 15 x0dx0astruct mark //定義迷宮內點的坐標類型 x0dx0a{ x0dx0aint x; x0dx0aint y; x0dx0a}; x0dx0ax0dx0astruct Element //"戀"棧元素,嘿嘿。。 x0dx0a{ x0dx0aint x,y; //x行,y列 x0dx0aint d; //d下一步的方向 x0dx0a}; x0dx0ax0dx0atypedef struct LStack //鏈棧 x0dx0a{ x0dx0aElement elem; x0dx0astruct LStack *next; x0dx0a}*PLStack; x0dx0ax0dx0a/*************棧函數****************/ x0dx0ax0dx0aint InitStack(PLStack &S)//構造空棧 x0dx0a{ x0dx0aS=NULL; x0dx0areturn 1; x0dx0a} x0dx0ax0dx0aint StackEmpty(PLStack S)//判斷棧是否為空 x0dx0a{ x0dx0aif(S==NULL) x0dx0areturn 1; x0dx0aelse x0dx0areturn 0; x0dx0a} x0dx0ax0dx0aint Push(PLStack &S, Element e)//壓入新數據元素 x0dx0a{ x0dx0aPLStack p; x0dx0ap=(PLStack)malloc(sizeof(LStack)); x0dx0ap->elem=e; x0dx0ap->next=S; x0dx0aS=p; x0dx0areturn 1; x0dx0a} x0dx0ax0dx0aint Pop(PLStack &S,Element &e) //棧頂元素出棧 x0dx0a{ x0dx0aPLStack p; x0dx0aif(!StackEmpty(S)) x0dx0a{ x0dx0ae=S->elem; x0dx0ap=S; x0dx0aS=S->next; x0dx0afree(p); x0dx0areturn 1; x0dx0a} x0dx0aelse x0dx0areturn 0; x0dx0a} x0dx0ax0dx0a/***************求迷宮路徑鋒雹函數***********************/ x0dx0avoid MazePath(struct mark start,struct mark end,int maze[M][N],int diradd[4][2]) x0dx0a{ x0dx0aint i,j,d;int a,b; x0dx0aElement elem,e; x0dx0aPLStack S1, S2; x0dx0aInitStack(S1); x0dx0aInitStack(S2); x0dx0amaze[start.x][start.y]=2; //入口點作上標記 x0dx0aelem.x=start.x; x0dx0aelem.y=start.y; x0dx0aelem.d=-1; //開始為-1 x0dx0aPush(S1,elem); x0dx0awhile(!StackEmpty(S1)) //棧不為空 有路徑可走 x0dx0a{ x0dx0aPop(S1,elem); x0dx0ai=elem.x; x0dx0aj=elem.y; x0dx0ad=elem.d+1; //下一個方向 x0dx0awhile(d<4) //試探東南西北各個方向 x0dx0a{ x0dx0aa=i+diradd[d][0]; x0dx0ab=j+diradd[d][1]; x0dx0aif(a==end.x && b==end.y && maze[a][b]==0) //如果到了出口 x0dx0a{ x0dx0aelem.x=i; x0dx0aelem.y=j; x0dx0aelem.d=d; x0dx0aPush(S1,elem); x0dx0aelem.x=a; x0dx0aelem.y=b; x0dx0aelem.d=886; //方向輸出為-1 判斷是否到了出口 x0dx0aPush(S1,elem); x0dx0aprintf("\n0=東 1=南 2=西 3=北 886為則走出迷宮\n\n通路為:(行坐標,列坐標,方向)\n"); x0dx0awhile(S1) //逆置序列 並輸出迷宮路徑序列 x0dx0a{ x0dx0aPop(S1,e); x0dx0aPush(S2,e); x0dx0a} x0dx0awhile(S2) x0dx0a{ x0dx0aPop(S2,e); x0dx0aprintf("-->(%d,%d,%d)",e.x,e.y,e.d); x0dx0a} x0dx0areturn; //跳出兩層循環,本來乎基衫用break,但發現出錯,exit又會歲腔結束程序,選用return還是不錯滴x0dx0a} x0dx0aif(maze[a][b]==0) //找到可以前進的非出口的點 x0dx0a{ x0dx0amaze[a][b]=2; //標記走過此點 x0dx0aelem.x=i; x0dx0aelem.y=j; x0dx0aelem.d=d; x0dx0aPush(S1,elem); //當前位置入棧 x0dx0ai=a; //下一點轉化為當前點 x0dx0aj=b; x0dx0ad=-1; x0dx0a} x0dx0ad++; x0dx0a} x0dx0a} x0dx0aprintf("沒有找到可以走出此迷宮的路徑\n"); x0dx0a} x0dx0ax0dx0a/*************建立迷宮*******************/ x0dx0avoid initmaze(int maze[M][N]) x0dx0a{ x0dx0aint i,j; x0dx0aint m,n; //迷宮行,列 [/M] x0dx0ax0dx0aprintf("請輸入迷宮的行數 m="); x0dx0ascanf("%d",&m); x0dx0aprintf("請輸入迷宮的列數 n="); x0dx0ascanf("%d",&n); x0dx0aprintf("\n請輸入迷宮的各行各列:\n用空格隔開,0代表路,1代表牆\n",m,n); x0dx0afor(i=1;i<=m;i++) x0dx0afor(j=1;j<=n;j++) x0dx0ascanf("%d",&maze[i][j]); x0dx0aprintf("你建立的迷宮為(最外圈為強)...\n"); x0dx0afor(i=0;i<=m+1;i++) //加一圈圍牆 x0dx0a{ x0dx0amaze[i][0]=1; x0dx0amaze[i][n+1]=1; x0dx0a} x0dx0afor(j=0;j<=n+1;j++) x0dx0a{ x0dx0amaze[0][j]=1; x0dx0amaze[m+1][j]=1; x0dx0a} x0dx0afor(i=0;i<=m+1;i++) //輸出迷宮 x0dx0a{ x0dx0afor(j=0;j<=n+1;j++) x0dx0aprintf("%d ",maze[i][j]); x0dx0aprintf("\n"); x0dx0a} x0dx0a} x0dx0ax0dx0avoid main() x0dx0a{ x0dx0aint sto[M][N]; x0dx0astruct mark start,end; //start,end入口和出口的坐標 x0dx0aint add[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//行增量和列增量 方向依次為東西南北 [/M] x0dx0ax0dx0ainitmaze(sto);//建立迷宮 x0dx0ax0dx0aprintf("輸入入口的橫坐標,縱坐標[逗號隔開]\n"); x0dx0ascanf("%d,%d",&start.x,&start.y); x0dx0aprintf("輸入出口的橫坐標,縱坐標[逗號隔開]\n"); x0dx0ascanf("%d,%d",&end.x,&end.y); x0dx0ax0dx0aMazePath(start,end,sto,add); //find path x0dx0asystem("PAUSE"); x0dx0a}

Ⅳ 如何用C語言實現求迷宮的最短路徑

#include<stdio.h>
#include<stdlib.h>
#define M 8
#define N 8
#define Max 100
int mg[M+2][N+2]= //定義迷宮,0表示能走的塊,1表示不能走,在外圍加上一圈不能走的塊
{
{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,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
struct
{
int i,j; //塊的位置
int pre; //本路徑中上一塊在隊列中的下標
}Qu[Max];
int front=-1,rear=-1;
void print(int n);
int mgpath(int xi,int yi,int xe,int ye) //搜索演算法
{
int i,j,find=0,di;
rear++;
Qu[rear].i=xi;
Qu[rear].j=yi;
Qu[rear].pre=-1;
mg[1][1]=-1;
while(front<=rear&&!find)
{
front++;
i=Qu[front].i;
j=Qu[front].j;
if(i==xe&&j==ye)
{
find=1;
print(front);
return(1);
}
for(di=0;di<4;di++)
{
switch(di) //四個方向
{
case 0:i=Qu[front].i-1;j=Qu[front].j;break;
case 1:i=Qu[front].i;j=Qu[front].j+1;break;
case 2:i=Qu[front].i+1;j=Qu[front].j;break;
case 3:i=Qu[front].i;j=Qu[front].j-1;break;
}
if(mg[i][j]==0)
{
rear++;
Qu[rear].i=i;
Qu[rear].j=j;
Qu[rear].pre=front;
mg[i][j]=-1; //避免死循環
}
}
}
return 0;
}

void print(int n) //輸出 路徑演算法
{
int k=n,j,m=1;
printf("\n");
do //將輸出的路徑上的所有pre改為-1
{
j=k;
k=Qu[k].pre;
Qu[j].pre=-1;
}while(k!=0);
printf("迷宮最短路徑如下:\n");
k=0;
while(k<Max)
{
if(Qu[k].pre==-1)
{
printf("\t(%d,%d)",Qu[k].i,Qu[k].j);
if(m%5==0)
printf("\n");
m++;
}
k++;
}
printf("\n");
}
int main()
{
mgpath(1,1,M,N);
system("pause");
return 0;
}

Ⅵ C語言迷宮,要完整代碼的

#include<stdio.h>
#include<conio.h>
intmigong[10][10]=//設置迷宮,最外圍1為牆里邊0為可走路徑1為障礙
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,1,1,1},
{1,0,1,1,1,1,1,0,0,1},
{1,0,1,0,0,0,0,0,0,1},
{1,0,0,0,1,0,1,1,1,1},
{1,1,1,1,0,0,1,1,1,1},
{1,0,0,0,0,1,1,1,1,1},
{1,0,1,1,0,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
intnum;
struct
{
intx,y,d;
}lj[100];//x,y分別為垂直和水平方向

voidstart()
{
inttop=0,x,y,d,find;//d為設置方向,上下左右。find為設置找不找得到路
lj[top].x=1;
lj[top].y=1;
migong[1][1]=-1;
find=0;d=-1;

while(top>-1){
if(lj[top].x==8&&lj[top].y==8)
{
printf("迷宮路徑如下: ");
printf("start->");
for(x=0;x<=top;x++)
{
printf("(%d,%d)->",lj[x].x,lj[x].y);//把找到的路徑輸出
num++;
if(num%8==0)
printf(" ");
}
printf("->end! ");
}

while(d<4&&find==0){
d++;
switch(d){
case0:x=lj[top].x-1;y=lj[top].y;break;//方向為上
case1:x=lj[top].x;y=lj[top].y+1;break;//方向為右
case2:x=lj[top].x+1;y=lj[top].y;break;//方向為下
case3:x=lj[top].x;y=lj[top].y-1;}//方向為左
if(migong[x][y]==0)
find=1;
}

if(find==1){//判斷是否找得到
lj[top].d=d;
top++;
lj[top].x=x;
lj[top].y=y;
d=-1;find=0;//重新調整方向
migong[x][y]=-1;}
else{
migong[lj[top].x][lj[top].y]=0;
top--;d=lj[top].d;//找不到的話退棧
}
}
}

voidmain()
{
start();
getch();
}

Ⅶ 用C語言編個走迷宮程序,要求:1:迷宮的規模和地圖由程序隨機自動生成。入口和出口由用戶指定。

程序目的:
輸入一個任意大小的迷宮,用棧求出一條走出迷宮的路徑,並
顯示在屏幕上。
程序實現:
可以實現載入迷宮和保存迷宮,附帶文件中有4個測試迷宮路徑的
文件test1~4.dd。請將這些文件拷貝到TC當前目錄下,或者在載
入時寫明完全路徑。由於屏幕大小的限制,當用戶自己輸入迷宮
時一定要注意:迷宮大小是有限制的,不小於4*3,不大於30*20。
否則會出現錯誤信息。輸入開始時全是牆,用上下左右鍵移動,
用Del鍵刪除牆,形成通路,用Enter鍵添加牆。輸入結束時可以
將迷宮保存下來,以dd為擴展名。輸入完畢時用F9鍵來得到結果,
找到路徑時,屏幕下方會出現Path found,否則出現Path not found。
程序經Turbo C 2.0編譯調試成功。運行時不用添加任何運行庫。
不可以在VC上編譯。
下載DOS版和windows版的迷宮游戲全部代碼
用戶名:migong
----------------------------------------------------------------------------------
/**//*
MazePath Demo BY Turbo C 2.0
Copyright(c) RoverUnion. All right reserved.
Filename: Maze.c
Author Dongchengyu.
Ver 1.10
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
#include <dos.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define F9 0x43
#define Esc 0x1b
#define Del 0x53
#define Home 0x47
#define End 0x4f
#define Space 0x20
#define Up 0x48
#define Down 0x50
#define Left 0x4b
#define Right 0x4d
#define Enter 0x0d
#define F2 0x3c
#define F3 0x3d
#define STACK_INIT_SIZE 200
#define STACKINCREMENT 10
typedef int Boolean;
typedef int Status;
typedef struct {
int x;
int y;
} PosType;
typedef struct {
int ord;
PosType seat;
int di;
} SElemType;
typedef struct {
int td;
int foot;
int mark;
} MazeType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
} Stack;
int Maze[20][30];
MazeType maze[20][30];
PosType StartPlace;
PosType EndPlace;
int count;
int m,n;
Boolean b_start=FALSE,b_end=FALSE;
void CreatMaze(void);
Status SaveMaze(char *filename);
Status LoadMaze(char *filename);
void Error(char *message);
Status InitStack(Stack *s);
Status DestroyStack(Stack *s);
Status ClearStack(Stack *s);
Boolean StackEmpty(Stack *s);
int StackLength(Stack *s);
Status Push(Stack *s,SElemType e);
SElemType Pop(Stack *s,SElemType e);
Status GetTop(Stack *s,SElemType *e);
Status StackTraverse(Stack *s,Status (* visit)(SElemType *se));
Boolean Pass(PosType curpos);
void MarkPrint(PosType seat);
void FootPrint(PosType curpos);
PosType NextPos(PosType seat,int di);
Status MazePath(PosType start,PosType end);
void CreatMaze(void)
/**//* Form the maze. */
{
void Error(char *message);
Status SaveMaze(char *filename);
Status LoadMaze(char *filename);
int i,j;
int x,y;
char c;
char savename[12],loadname[12];
Boolean flag=FALSE,load=FALSE;
clrscr();
printf("Menu:\n\n");
printf("1.Load Mazefile:(*.dd)\n\n");
printf("2.Input Maze:\n\n");
printf("Input your choice: ");
do
{
c=getch();
switch(c)
{
case ''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''': putch(''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''); break;
case ''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''': putch(''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''); break;
case Esc: sleep(1); exit(1);
default: break;
}
}
while(c!=''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''&&c!=''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''') ;
if(c==''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''')
{
printf("\n\nLoadName: ");
scanf("%s",loadname);
if(LoadMaze(loadname))
{
sleep(1); load=TRUE;
}
else { gotoxy(1,9); printf("Load fail! "); }
}
if(!load)
{
printf("\nInput the maze''''''''''''''''''''''''''''''''s size:\n");
printf("\nInput Length :\n");
scanf("%d",&m);
printf("\nInput Width :\n");
scanf("%d",&n);
if(m<4||n<4) Error("Input");
if(m>30||n>20) Error("Maze too large");
for(i=0;i<30;i++)
for(j=0;j<20;j++)
Maze[j][i]=2;
StartPlace.x=0;
StartPlace.y=0;
EndPlace.x=0;
EndPlace.y=0;
clrscr();
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
printf(" #");
Maze[i-1][j-1]=0;
}
printf("\n");
}
}
gotoxy(65,5);
printf("''''''''''''''''''''''''''''''''#'''''''''''''''''''''''''''''''':Wall");
gotoxy(65,7);
printf("Start:Home");
gotoxy(65,9);
printf("End:End");
gotoxy(65,11);
printf("Delete Wall:Del");
gotoxy(65,13);
printf("Enter Wall:Enter");
gotoxy(65,15);
printf("Save Maze:F2");
gotoxy(65,17);
printf("Complete:F9");
gotoxy(65,19);
printf("Exit:Esc");
gotoxy(4,3);
x=4;y=3;
do
{
c=getch();
switch(c)
{
case Up: if(y>3) { y--; gotoxy(x,y); }
break;
case Down: if(y<n) { y++; gotoxy(x,y); }
break;
case Left: if(x>4) { x-=2; gotoxy(x,y); }
break;
case Right: if(x<2*m-2) { x+=2; gotoxy(x,y); }
break;
case Del: if(y-2==StartPlace.y&&x/2-1==StartPlace.x) b_start=FALSE;
if(y-2==EndPlace.y&&x/2-1==EndPlace.x) b_end=FALSE;
putch('''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''); Maze[y-2][x/2-1]=1; gotoxy(x,y);
break;
case Enter: if(y-2==StartPlace.y&&x/2-1==StartPlace.x) break;
if(y-2==EndPlace.y&&x/2-1==EndPlace.x) break;
putch(''''''''''''''''''''''''''''''''#''''''''''''''''''''''''''''''''); Maze[y-2][x/2-1]=0; gotoxy(x,y);
break;
case Home: if(Maze[y-2][x/2-1]&&!b_start)
{
StartPlace.x=x/2-1;
StartPlace.y=y-2;
putch(''''''''''''''''''''''''''''''''S'''''''''''''''''''''''''''''''');
gotoxy(x,y);
b_start=TRUE;
}
break;
case End: if(Maze[y-2][x/2-1]&&!b_end)
{
EndPlace.x=x/2-1;
EndPlace.y=y-2;
putch(''''''''''''''''''''''''''''''''E'''''''''''''''''''''''''''''''');
gotoxy(x,y);
b_end=TRUE;
}
break;
case Esc: gotoxy(2,22); printf("exit"); sleep(1); exit(1);
case F9: if(b_start&&b_end) flag=TRUE; break;
case F2: gotoxy(2,22);
printf("Savename:");
scanf("%s",savename);
gotoxy(2,22);
if(SaveMaze(savename)) printf("Save OK! ");
else printf("Save fail! ");
sleep(1);
gotoxy(2,22);
printf(" ");
gotoxy(x,y);
break;
default: break;
}
}
while(!flag);
for(i=0;i<30;i++)
for(j=0;j<20;j++)
{
maze[j][i].td=Maze[j][i];
maze[j][i].mark=0;
maze[j][i].foot=0;
}
}
Status LoadMaze(char *file)
/**//* The maze has been loaded. */
{
FILE *fp;
char *buffer;
char ch;
int i=0,j,k;
Boolean len=FALSE,wid=FALSE;
if((fp=fopen(file,"r"))==NULL)
return ERROR;
buffer=(char *)malloc(600*sizeof(char));
ch=fgetc(fp);
while(ch!=EOF)
{
buffer[i]=ch;
i++;
ch=fgetc(fp);
}
m=30;n=20;
for(i=0;i<600;i++)
{
j=i/30; k=i%30;
if(buffer[i]==''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''&&!len){ m=i; len=TRUE; }
if(k==0&&buffer[i]==''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''&&!wid){ n=j; wid=TRUE; }
switch(buffer[i])
{
case ''''''''''''''''''''''''''''''''0'''''''''''''''''''''''''''''''': Maze[j][k]=0; break;
case ''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''': Maze[j][k]=1; break;
case ''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''': Maze[j][k]=2; break;
case ''''''''''''''''''''''''''''''''3'''''''''''''''''''''''''''''''': Maze[j][k]=1;
StartPlace.x=k;
StartPlace.y=j;
b_start=TRUE;
break;
case ''''''''''''''''''''''''''''''''4'''''''''''''''''''''''''''''''': Maze[j][k]=1;
EndPlace.x=k;
EndPlace.y=j;
b_end=TRUE;
break;
default : break;
}
}
fclose(fp);
clrscr();
for(i=0;i<30;i++)
for(j=0;j<20;j++)
{
maze[j][i].td=Maze[j][i];
maze[j][i].foot=0;
maze[j][i].mark=0;
if(Maze[j][i]==0)
{
gotoxy(2*i+2,j+2);
putch(''''''''''''''''''''''''''''''''#'''''''''''''''''''''''''''''''');
}
}
gotoxy(2*StartPlace.x+2,StartPlace.y+2);
putch(''''''''''''''''''''''''''''''''S'''''''''''''''''''''''''''''''');
gotoxy(2*EndPlace.x+2,EndPlace.y+2);
putch(''''''''''''''''''''''''''''''''E'''''''''''''''''''''''''''''''');
return OK;
}
Status SaveMaze(char *filename)
/**//* The maze has been saved. */
{
FILE *fp;
char *buffer;
int i,j,k;
fp=fopen(filename,"wb");
buffer=(char *)malloc(600*sizeof(char));
for(i=0;i<600;i++)
{
j=i/30; k=i%30;
switch(Maze[j][k])
{
case 0: buffer[i]=''''''''''''''''''''''''''''''''0''''''''''''''''''''''''''''''''; break;
case 1: buffer[i]=''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''; break;
case 2: buffer[i]=''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''; break;
default : Error("Write"); break;
}
if(k==StartPlace.x&&j==StartPlace.y) buffer[i]=''''''''''''''''''''''''''''''''3'''''''''''''''''''''''''''''''';
if(k==EndPlace.x&&j==EndPlace.y) buffer[i]=''''''''''''''''''''''''''''''''4'''''''''''''''''''''''''''''''';
}
fwrite(buffer,600,1,fp);
free(buffer);
fclose(fp);
return OK;
}
void Error(char *message)
{
clrscr();
fprintf(stderr,"Error:%s\n",message);
exit(1);
} /**//* Error */

Status InitStack(Stack *s)
/**//* The stack s has been created and is initialized to be empty. */
{
s->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!s->base) Error("Overflow");
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
} /**//* InitStack */
Status DestroyStack(Stack *s)
/**//* The stack s has been destroyed. */
{
s->top=NULL;
s->stacksize=0;
free(s->base);
s->base=NULL;
return OK;
} /**//* DestroyStack */
Status ClearStack(Stack *s)
/**//* The stack has been clear to be maximum. */
{
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
} /**//* ClearStack */
Boolean StackEmpty(Stack *s)
/**//* Check if the stack s is empty. */
{
if(s->top==s->base) return TRUE;
else return FALSE;
} /**//* StackEmpty */
int StackLength(Stack *s)
/**//* Gain the length of the stack s. */
{
if(s->top>s->base) return (int)(s->top-s->base);
else return 0;
} /**//* StackLength */
Status Push(Stack *s,SElemType e)
/**//* The element e has been pushed into the stack s. */
{
if(s->top-s->base>=s->stacksize)
{
s->base=(SElemType *)realloc(s->base,
(s->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!s->base) Error("Overflow");
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=e;
return OK;
} /**//* Push */
SElemType Pop(Stack *s,SElemType e)
/**//* The element e has been removed from the stack s. */
{
if(s->top==s->base) Error("Pop");
e=*--s->top;
return e;
} /**//* Pop */
Status GetTop(Stack *s,SElemType *e)
/**//* The element e has got to the top of the stack s.*/
{
if(s->top==s->base) Error("GetTop");
*e=*(s->top-1);
return OK;
} /**//* GetTop */
/**//* Traverse the stack s using ''''''''''''''''''''''''''''''''visiting'''''''''''''''''''''''''''''''' function. */
/**//* Status StackTraverse(Stack *s,Status (* visit)(SElemType *se))
{
SElemType p;
int result;
if(s->top==s->base) return ERROR;
p=s->base;
while(!(p==s->top))
{
result=(*visit)(p);
p++;
}
return OK;
} */
Boolean Pass(PosType curpos)
/**//* Check if the current position can be passed. */
{
if(maze[curpos.x][curpos.y].td==1&&
maze[curpos.x][curpos.y].foot==0&&maze[curpos.x][curpos.y].mark==0)
return TRUE;
else return FALSE;
} /**//* Pass */
void MarkPrint(PosType seat)
/**//* Mark the position seat. */
{
maze[seat.x][seat.y].mark=-1;
/**//* Marking ''''''''''''''''''''''''''''''''-1'''''''''''''''''''''''''''''''' symbolize the current position cannot be put. */
} /**//* MarkPrint */
void FootPrint(PosType curpos)
/**//* The foot of the curpos of the maze has been set ''''''''''''''''''''''''''''''''true''''''''''''''''''''''''''''''''. */
{
maze[curpos.x][curpos.y].foot=1;
} /**//* FootPrint */
PosType NextPos(PosType seat,int di)
{
switch(di)
{
case 1: seat.y++; return seat; /**//* Eastward */
case 2: seat.x++; return seat; /**//* Southward */
case 3: seat.y--; return seat; /**//* Westward */
case 4: seat.x--; return seat; /**//* Northward */
default: seat.x=0; seat.y=0; return seat;
}
} /**//* NextPos */

/**//* The key to the program. */
/**//* Pre: The maze array & the startplace & the endplace.
Post: Find the one traverse of the maze and perform the mazepath.
Uses: The ADT stack class.
*/
Status MazePath(PosType start,PosType end)
{
PosType curpos;
int curstep;
SElemType e;
Stack *s,stack;
stack.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!stack.base) Error("Overflow");
stack.top=stack.base;
stack.stacksize=STACK_INIT_SIZE;
s=&stack;
curpos=start;
curstep=1;
do
{
if(Pass(curpos))
{
FootPrint(curpos);
e.ord=curstep; e.seat=curpos; e.di=1;
gotoxy((curpos.y+1)*2,curpos.x+2);
putch(''''''''''''''''''''''''''''''''@'''''''''''''''''''''''''''''''');
delay(8000); /**//* pospone time. */
Push(s,e);
if(curpos.x==end.x&&curpos.y==end.y) /**//* Proceed recursively. */
{
DestroyStack(s);
return TRUE;
}
curpos=NextPos(curpos,1); /**//* Try next position. */
curstep++;
}
else
{
if(!StackEmpty(s))
{
e=Pop(s,e); /**//* Removed e from s. */
while(e.di==4&&!StackEmpty(s)) /**//* Four directions have been checked
and s is not empty. */
{
MarkPrint(e.seat);
gotoxy((e.seat.y+1)*2,e.seat.x+2);
putch(''''''''''''''''''''''''''''''''@'''''''''''''''''''''''''''''''');
delay(8000); /**//* Pospone time. */
gotoxy((e.seat.y+1)*2,e.seat.x+2);
putch('''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''');
e=Pop(s,e); /**//* Remove e from s. */
curstep--;
}
if(e.di<4) /**//* The current position hasnot been checked. */
{
e.di++;
Push(s,e); /**//* Insert e into s. */
curpos=NextPos(e.seat,e.di); /**//* Try next position. */
}
}
}
}
while(!StackEmpty(s));
DestroyStack(s);
return FALSE;
} /**//* MazePath */
void main()
{
PosType start,end;
CreatMaze();
start.x=StartPlace.y;
start.y=StartPlace.x;
end.x=EndPlace.y;
end.y=EndPlace.x;
if(MazePath(start,end))
{
gotoxy(2,22);
printf("Path found\n");
}
else
{
gotoxy(2,22);
printf("Path not found\n");
}
getch();
clrscr();
}

Ⅷ 如何用c語言編寫迷宮游戲

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define N 20/*
迷宮的大小,可改變
*/
int oldmap[N][N];/*
遞歸用的數組
,
用全局變數節約時間
*/
int yes=0;/*yes
是判斷是否找到路的標志
,1
找到,
0
沒找到
*/
int way[100][2],wayn=0;/*way
數組是顯示路線用的
,wayn
是統計走了幾個格

*/
void Init(void);/*
圖形初始化
*/
void Close(void);/*
圖形關閉
*/
void DrawPeople(int *x,int *y,int n);/*
畫人工探索物圖
*/
void PeopleFind(int (*x)[N]);/*
人工探索
*/
void
WayCopy(int
(*x)[N],int
(*y)[N]);/*
為了
8
個方向的遞歸,把舊迷宮圖
拷貝給新數組
*/
int FindWay(int (*x)[N],int i,int j);/*
自動探索函數
*/
void MapRand(int (*x)[N]);/*
隨機生成迷宮函數
*/
void PrMap(int (*x)[N]);/*
輸出迷宮圖函數
*/
void Result(void);/*
輸出結果處理
*/
void Find(void);/*
成功處理
*/
void NotFind(void);/*
失敗處理
*/
void main(void)/*
主函數
*/
{
int map[N][N]; /*
迷宮數組
*/
char ch;
clrscr();
printf("\n Please select hand(1) else auto\n");/*
選擇探索方式
*/
scanf("%c",&ch);
Init(); /*
初始化
*/
MapRand(map);/*
生成迷宮
*/
PrMap(map);/*
顯示迷宮圖
*/
if(ch=='1')
PeopleFind(map);/*
人工探索
*/
else
FindWay(map,1,1);/*
系統自動從下標
1,1
的地方開始探索
*/
Result();/*
輸出結果
*/
Close();
}
void Init(void)/*
圖形初始化
*/
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc"); }
void DrawPeople(int *x,int *y,int n)/*畫人工控制圖*/ {/*如果將以下兩句注釋掉,則顯示人工走過的路徑,*/
setfillstyle(SOLID_FILL,WHITE); /*設置白色實體填充樣式*/ bar(100+(*y)*15-6,50+(*x)*15-6,100+(*y)*15+6,50+(*x)*15+6); /*恢復原通路*/
switch(n)/*判斷x,y的變化,8個方向的變化*/ {
case 1: (*x)--;break; /*上*/
case 2: (*x)--;(*y)++;break /*右上*/ case 3: (*y)++;break; /*右*/
case 4: (*x)++;(*y)++;break; /*右下*/ case 5: (*x)++;break; /*下*/
case 6: (*x)++;(*y)--;break; /*左下*/ case 7: (*y)--;break; /*左*/
case 8: (*x)--;(*y)--;break; /*左上*/ }
setfillstyle(SOLID_FILL,RED);/*新位置顯示探索物*/
bar(100+(*y)*15-6,50+(*x)*15-6,100+(*y)*15+6,50+(*x)*15+6); }
void PeopleFind(int (*map)[N])/*人工手動查找*/ {
int x,y;
char c=0;/*接收按鍵的變數*/ x=y=1;/*人工查找的初始位置*/ setcolor(11);
line(500,200,550,200); outtextxy(570,197,"d"); line(500,200,450,200); outtextxy(430,197,"a"); line(500,200,500,150); outtextxy(497,130,"w"); line(500,200,500,250); outtextxy(497,270,"x"); line(500,200,450,150); outtextxy(445,130,"q"); line(500,200,550,150); outtextxy(550,130,"e"); line(500,200,450,250); outtextxy(445,270,"z"); line(500,200,550,250);
outtextxy(550,270,"c");/*以上是畫8個方向的控制介紹*/
setcolor(YELLOW);
outtextxy(420,290,"Press 'Enter' to end");/*壓回車鍵結束*/ setfillstyle(SOLID_FILL,RED);
bar(100+y*15-6,50+x*15-6,100+y*15+6,50+x*15+6);/*入口位置顯示*/ while(c!=13)/*如果按下的不是回車鍵*/ {
c=getch();/*接收字元後開始各個方向的探索*/ if(c=='w'&&map[x-1][y]!=1) DrawPeople(&x,&y,1);/*上*/ else if(c=='e'&&map[x-1][y+1]!=1) DrawPeople(&x,&y,2);/*右上*/ else if(c=='d'&&map[x][y+1]!=1) DrawPeople(&x,&y,3);/*右*/ else if(c=='c'&&map[x+1][y+1]!=1) DrawPeople(&x,&y,4);/*右下*/ else if(c=='x'&&map[x+1][y]!=1) DrawPeople(&x,&y,5);/*下*/ else if(c=='z'&&map[x+1][y-1]!=1) DrawPeople(&x,&y,6); /*左下*/ else if(c=='a'&&map[x][y-1]!=1) DrawPeople(&x,&y,7); /*左*/ else if(c=='q'&&map[x-1][y-1]!=1) DrawPeople(&x,&y,8); /*左上*/ }
setfillstyle(SOLID_FILL,WHITE); /*消去紅色探索物,恢復原迷宮圖*/ bar(100+y*15-6,50+x*15-6,100+y*15+6,50+x*15+6); if(x==N-2&&y==N-2)/*人工控制找成功的話*/ yes=1; /*如果成功標志為1*/ }
void WayCopy(int (*oldmap)[N],int (*map)[N])/*拷貝迷宮數組 */ {
int i,j;
for(i=0;i<N;i++) for(j=0;j<N;j++) oldmap[i][j]=map[i][j]; }
int FindWay(int (*map)[N],int i,int j)/*遞歸找路*/ {

if(i==N-2&&j==N-2)/*走到出口*/ {
yes=1;/*標志為1,表示成功*/ return; }
map[i][j]=1;/*走過的地方變為1*/ WayCopy(oldmap,map); /*拷貝迷宮圖*/
if(oldmap[i+1][j+1]==0&&!yes)/*判斷右下方是否可走*/ {
FindWay(oldmap,i+1,j+1); if(yes)/*如果到達出口了,再把值賦給顯示路線的way數組,也正是這個原因,所以具體路線是從最後開始保存*/ { way[wayn][0]=i; way[wayn++][1]=j; return; } }
WayCopy(oldmap,map);
if(oldmap[i+1][j]==0&&!yes)/*判斷下方是否可以走,如果標志yes已經是1也不用找下去了*/ {
FindWay(oldmap,i+1,j); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } }
WayCopy(oldmap,map);
if(oldmap[i][j+1]==0&&!yes)/*判斷右方是否可以走*/ {
FindWay(oldmap,i,j+1); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } }
WayCopy(oldmap,map);
if(oldmap[i-1][j]==0&&!yes)/*判斷上方是否可以走*/ {

FindWay(oldmap,i-1,j); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } }
WayCopy(oldmap,map);
if(oldmap[i-1][j+1]==0&&!yes)/*判斷右上方是否可以走*/ {
FindWay(oldmap,i-1,j+1); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } }
WayCopy(oldmap,map);
if(oldmap[i+1][j-1]==0&&!yes)/*判斷左下方是否可以走*/ {
FindWay(oldmap,i+1,j-1); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } }
WayCopy(oldmap,map);
if(oldmap[i][j-1]==0&&!yes)/*判斷左方是否可以走*/ {
FindWay(oldmap,i,j-1); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } }
WayCopy(oldmap,map);
if(oldmap[i-1][j-1]==0&&!yes)/*判斷左上方是否可以走*/ {

FindWay(oldmap,i-1,j-1); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } }
return; }
void MapRand(int (*map)[N])/*開始的隨機迷宮圖*/ {
int i,j;
cleardevice();/*清屏*/
randomize(); /*隨機數發生器*/ for(i=0;i<N;i++) {
for(j=0;j<N;j++) { if(i==0||i==N-1||j==0||j==N-1)/*最外面一圈為牆壁*/ map[i][j]=1; else if(i==1&&j==1||i==N-2&&j==N-2)/*出發點與終點表示為可走的*/ map[i][j]=0; else map[i][j]=random(2);/*其它的隨機生成0或1*/ } } }
void PrMap(int (*map)[N])/*輸出迷宮圖*/ {
int i,j;
for(i=0;i<N;i++) for(j=0;j<N;j++) if(map[i][j]==0) { setfillstyle(SOLID_FILL,WHITE);/*白色為可走的路*/ bar(100+j*15-6,50+i*15-6,100+j*15+6,50+i*15+6); } else { setfillstyle(SOLID_FILL,BLUE);/*藍色為牆壁*/ bar(100+j*15-6,50+i*15-6,100+j*15+6,50+i*15+6);

} }
void Find(void)/*找到通路*/ {
int i;
setfillstyle(SOLID_FILL,RED);/*紅色輸出走的具體路線*/ wayn--;
for(i=wayn;i>=0;i--) {
bar(100+way[i][1]*15-6,50+way[i][0]*15-6,100+ way[i][1]*15+6,50+way[i][0]*15+6); sleep(1);/*控制顯示時間*/ }
bar(100+(N-2)*15-6,50+(N-2)*15-6,100+ (N-2)*15+6,50+(N-2)*15+6); /*在目標點標紅色*/ setcolor(GREEN);
settextstyle(0,0,2);/*設置字體大小*/ outtextxy(130,400,"Find a way!"); }
void NotFind(void)/*沒找到通路*/ {
setcolor(GREEN);
settextstyle(0,0,2);/*設置字體大小*/ outtextxy(130,400,"Not find a way!"); }
void Result(void)/*結果處理*/ {
if(yes)/*如果找到*/ Find();
else/*沒找到路*/ NotFind(); getch(); }
void Close(void)/*圖形關閉*/ {
closegraph(); }