指引网

当前位置: 主页 > 编程开发 > C >

迷宫探路IV(递归算法)

来源:网络 作者:佚名 点击: 时间:2017-07-19 23:01
[摘要] 

/* 迷宫探路(recursive)*/
/* recursivemaze.c */
/* 2003-10-16 */
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <graphics.h>
#define N 22
#define M 22
#define MAXLEN M*N
int bg[M][N];
int aa[M][N];
struct pace{
    int dir;
    int ri;
    int rj;
}road[MAXLEN];
int length=0;
int dj[]={1,0,-1,0};
int di[]={0,1,0,-1};
void makebg(int,int);
void drawbg(int[][],int,int,int,int,int);
void drawman(int,int,int);
void rect(int,int,int,int);
int go(int ,int ,int);
void main(){/* main()开始 */
int step=20;
int len=10;
int size=20;
int x=0,y=0;
int i=0,j=0;
int gdriver=DETECT,gmode;
makebg(M,N);
/* registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,"c:\turboc2");*/

initgraph(&gdriver,&gmode,"c:\tc20\bgi");

cleardevice();
setwritemode(XOR_PUT);
settextstyle(1,0,3);
setcolor(GREEN);
outtextxy(100,180,"RECURSIVE MAZE");
setcolor(BLUE);
setfillstyle(LINE_FILL,BLUE);
/*drawbg(bg,M,N,size,0,0);*/
drawbg(aa,M,N,size,0,0);
setcolor(WHITE);
x+=len;y+=len;
drawman(x,y,len);
/* 电脑控制 */
aa[0][0]=1;
road[0].ri=0;
road[0].rj=0;
road[0].dir=0;
go(0,0,0);
/* output */
getch();
drawman(x,y,len);
for(i=0;i<=length;i++){
    drawman(x+road[i].rj*step,y+road[i].ri*step,len);
    delay(80000);
    drawman(x+road[i].rj*step,y+road[i].ri*step,len);
}
i--;
drawman(x+road[i].rj*step,y+road[i].ri*step,len);
getch();
closegraph();
}
/* main()结束 */
/* 绘制小人 */
void drawman(int x,int y,int len){
    int r=len/4;
    rect(x-r,y-len,x+r,y-len+2*r);
    line(x,y-len+2*r,x,y);
    line(x-len,y,x+len,y);
    line(x,y,x-len,y+len);
    line(x,y,x+len,y+len);
}
/* 绘制迷宫地图 */
void drawbg(int bg[][N],int a,int b,int size,int x,int y){
    int startx=x;
    int i,j;
    for(i=0;i<a;i++){
        for(j

------分隔线----------------------------