求用到深度搜索,广度所搜的游戏代码--迷宫(C语言)

要调试通过,有注释的优先,还有就是别拿网上漏洞百出的代码来忽悠我哦。 答案好就在追加40分。 在此谢谢了!
我是做毕业设计用的,不是那些太简单的迷宫,而且要随机生成的。

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <graphics.h>
#include <bios.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 BKCOLOR LIGHTBLUE
#define WALLCOLOR RED
#define CURSORCOLOR BLUE
#define CIRCLECOLOR CYAN
#define ERRORCOLOR CYAN
#define TEXTCOLOR 14

#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[16][17];
MazeType maze[16][17];
PosType StartPlace;
PosType EndPlace;

void Paint(void);
void CreatMaze(void);
void DrawWall(int cx,int cy,int color);
void DrawCursor(int cx,int cy,int color);
void Refresh(void);

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 Paint(void)
{
int i,j;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,".\\");
setbkcolor(LIGHTBLUE);
setcolor(10);
outtextxy(190,10,"MazePath Game");
setcolor(RED);
for(i=30;i<=450;i+=30)
line(30,i,480,i);
for(j=30;j<=480;j+=30)
line(j,30,j,450);
rectangle(29,29,481,451);
rectangle(28,28,482,452);
rectangle(490,320,630,470);
setcolor(1);
outtextxy(500,100,"Complete:F9");
outtextxy(500,120,"Start:Home");
outtextxy(500,140,"End:End");
outtextxy(500,160,"Draw wall:Enter");
outtextxy(500,180,"Delete wall:Del");
outtextxy(500,200,"Exit:Esc");
outtextxy(500,300,"Message:");
for(i=0;i<16;i++)
for(j=0;j<17;j++)
Maze[j][i]=0;
for(i=0;i<17;i++)
{
Maze[0][i]=1;
Maze[15][i]=1;
}
for(i=0;i<16;i++)
{
Maze[i][0]=1;
Maze[i][16]=1;
}
} /* Paint */

void DrawCursor(int cx,int cy,int color)
{
setcolor(color);
rectangle(cx-7,cy-7,cx+7,cy+7);
} /* DrawCursor */

void DrawWall(int x,int y,int color)
{
void DrawCursor(int cx,int cy,int color);
register int i;
setcolor(color);
x*=30; y*=30;
for(i=y;i<=y+30;i++)
line(x,i,x+30,i);
for(i=x;i<=x+30;i++)
line(i,y,i,y+30);
DrawCursor(x+15,y+15,CURSORCOLOR);
} /* DrawWall */

void Refresh(void)
{
register int i,j;
setcolor(BKCOLOR);
for(i=1;i<=480;i++)
line(1,i,480,i);
for(j=1;j<=480;j++)
line(i,1,i,480);
setcolor(WALLCOLOR);
rectangle(29,29,481,451);
rectangle(28,28,482,452);
rectangle(30,30,480,450);
for(i=1;i<=15;i++)
for(j=1;j<=14;j++)
if(Maze[j][i]) {
DrawWall(i,j,WALLCOLOR);
DrawCursor(i*30+15,j*30+15,WALLCOLOR);
}
setcolor(CIRCLECOLOR);
circle(StartPlace.x*30+15,StartPlace.y*30+15,6);
circle(EndPlace.x*30+15,EndPlace.y*30+15,6);
setcolor(BKCOLOR);
for(i=491;i<=629;i++)
line(i,321,i,469);
setcolor(BROWN);
setfillstyle(SOLID_FILL,BROWN);
fillellipse(StartPlace.x*30+15,StartPlace.y*30+15,6,6);
setcolor(CYAN);
setfillstyle(SOLID_FILL,CYAN);
fillellipse(EndPlace.x*30+15,EndPlace.y*30+15,6,6);
} /* Refresh */

void CreatMaze(void)
{
void Error(char *message);
char c;
int i,j;
Boolean flag=TRUE;
int x=1,y=1;
Boolean start=FALSE,end=FALSE;
DrawCursor(45,45,CURSORCOLOR);
do
{
c=getch();
switch(c)
{
case Up: if(y>1) {
if(!Maze[y][x])
DrawCursor(x*30+15,y*30+15,BKCOLOR);
else DrawCursor(x*30+15,y*30+15,WALLCOLOR);
DrawCursor(x*30+15,(--y)*30+15,CURSORCOLOR); }
break;
case Down: if(y<14) {
if(!Maze[y][x])
DrawCursor(x*30+15,y*30+15,BKCOLOR);
else DrawCursor(x*30+15,y*30+15,WALLCOLOR);
DrawCursor(x*30+15,(++y)*30+15,CURSORCOLOR); }
break;
case Left: if(x>1) {
if(!Maze[y][x])
DrawCursor(x*30+15,y*30+15,BKCOLOR);
else DrawCursor(x*30+15,y*30+15,WALLCOLOR);
DrawCursor((--x)*30+15,y*30+15,CURSORCOLOR); }
break;
case Right: if(x<15) {
if(!Maze[y][x])
DrawCursor(x*30+15,y*30+15,BKCOLOR);
else DrawCursor(x*30+15,y*30+15,WALLCOLOR);
DrawCursor((++x)*30+15,y*30+15,CURSORCOLOR); }
break;
case Home: if(!start&&!Maze[y][x]) {
setcolor(6);
setfillstyle(SOLID_FILL,6);
fillellipse(x*30+15,y*30+15,6,6);
start=TRUE; StartPlace.x=x;StartPlace.y=y; }
break;
case End: if(!end&&!Maze[y][x]) {
setcolor(CYAN);
setfillstyle(SOLID_FILL,CYAN);
fillellipse(x*30+15,y*30+15,6,6);
end=TRUE; EndPlace.x=x; EndPlace.y=y; }
break;
case Esc: setcolor(TEXTCOLOR);
outtextxy(540,380,"exit");
sleep(1);
closegraph();
exit(1);
case F9: if(start&&end) { Refresh(); flag=FALSE; }
else
{
setcolor(TEXTCOLOR);
outtextxy(493,323," Error: ");
outtextxy(493,343," You must set ");
if(!start) outtextxy(493,363," startplace. ");
else outtextxy(493,363," endplace.");
}
break;
case Enter: if(!(x==StartPlace.x&&y==StartPlace.y)&&
!(x==EndPlace.x&&y==EndPlace.y))
{
DrawWall(x,y,WALLCOLOR);
Maze[y][x]=1;
}
break;
case Del: DrawWall(x,y,BKCOLOR); setcolor(4);
rectangle(x*30,y*30,x*30+30,y*30+30);
Maze[y][x]=0;
if(x==StartPlace.x&&y==StartPlace.y)
{
StartPlace.x=0;
StartPlace.y=0;
start=FALSE;
}
if(x==EndPlace.x&&y==EndPlace.y)
{
EndPlace.x=0;
EndPlace.y=0;
end=FALSE;
}
break;
default: break;
}

}
while(flag);
for(i=0;i<17;i++)
for(j=0;j<16;j++)
{
maze[j][i].td=1-Maze[j][i];
maze[j][i].mark=0;
maze[j][i].foot=0;
}
} /* CreatMaze */

void Error(char *message)
{
closegraph();
fprintf(stderr,"Error:%s\n",message);
exit(1);
} /* Error */

Status InitStack(Stack *s)
{
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)
{
s->top=NULL;
s->stacksize=0;
free(s->base);
s->base=NULL;
return OK;
} /* DestroyStack */

Status ClearStack(Stack *s)
{
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
} /* ClearStack */

Boolean StackEmpty(Stack *s)
{
if(s->top==s->base) return TRUE;
else return FALSE;
} /* StackEmpty */

int StackLength(Stack *s)
{
if(s->top>s->base) return (int)(s->top-s->base);
else return 0;
} /* StackLength */

Status Push(Stack *s,SElemType e)
{
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)
{
if(s->top==s->base) Error("Pop");
e=*--s->top;
return e;
} /* Pop */

Status GetTop(Stack *s,SElemType *e)
{
if(s->top==s->base) Error("GetTop");
*e=*(s->top-1);
return OK;
} /* GetTop */

/*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)
{
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)
{
maze[seat.x][seat.y].mark=-1;
} /* MarkPrint */

void FootPrint(PosType curpos)
{
maze[curpos.x][curpos.y].foot=1;
} /* FootPrint */

PosType NextPos(PosType seat,int di)
{
switch(di)
{
case 1: seat.y++; return seat;

case 2: seat.x++; return seat;

case 3: seat.y--; return seat;

case 4: seat.x--; return seat;

default: seat.x=0; seat.y=0; return seat;
}
} /* NextPos */

Status MazePath(PosType start,PosType end)
{
PosType curpos;
int curstep;
SElemType e;
Stack *s=NULL,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;
setcolor(BLUE);
circle(curpos.y*30+15,curpos.x*30+15,6);
delay(8000);
Push(s,e);
if(curpos.x==end.x&&curpos.y==end.y)
{
DestroyStack(s);
return TRUE;
}
curpos=NextPos(curpos,1);
curstep++;
}
else
{
if(!StackEmpty(s))
{
e=Pop(s,e);
while(e.di==4&&!StackEmpty(s))
{
MarkPrint(e.seat);
setcolor(BLUE);
circle(e.seat.y*30+15,e.seat.x*30+15,6);
delay(8000);
setcolor(BKCOLOR);
circle(e.seat.y*30+15,e.seat.x*30+15,6);
e=Pop(s,e);
curstep--;
}
if(e.di<4)
{
e.di++;
Push(s,e);
curpos=NextPos(e.seat,e.di);
}
}
}
}
while(!StackEmpty(s));
DestroyStack(s);
return FALSE;
} /* MazePath */

void main(void)
{
PosType start,end;
Paint();
CreatMaze();
start.x=StartPlace.y;
start.y=StartPlace.x;
end.x=EndPlace.y;
end.y=EndPlace.x;
if(MazePath(start,end))
{
setcolor(TEXTCOLOR);
outtextxy(520,380,"Path found");
}
else
{
setcolor(TEXTCOLOR);
outtextxy(500,380,"Path not found");
}
while(bioskey(1)==0);
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2008-01-30
我正好收藏了一个,拿出来给你:(在TC下调试通过)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <graphics.h>
#include <bios.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 BKCOLOR LIGHTBLUE
#define WALLCOLOR RED
#define CURSORCOLOR BLUE
#define CIRCLECOLOR CYAN
#define ERRORCOLOR CYAN
#define TEXTCOLOR 14

#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[16][17];
MazeType maze[16][17];
PosType StartPlace;
PosType EndPlace;

void Paint(void);
void CreatMaze(void);
void DrawWall(int cx,int cy,int color);
void DrawCursor(int cx,int cy,int color);
void Refresh(void);

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 Paint(void)
{
int i,j;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,".\\");
setbkcolor(LIGHTBLUE);
setcolor(10);
outtextxy(190,10,"MazePath Game");
setcolor(RED);
for(i=30;i<=450;i+=30)
line(30,i,480,i);
for(j=30;j<=480;j+=30)
line(j,30,j,450);
rectangle(29,29,481,451);
rectangle(28,28,482,452);
rectangle(490,320,630,470);
setcolor(1);
outtextxy(500,100,"Complete:F9");
outtextxy(500,120,"Start:Home");
outtextxy(500,140,"End:End");
outtextxy(500,160,"Draw wall:Enter");
outtextxy(500,180,"Delete wall:Del");
outtextxy(500,200,"Exit:Esc");
outtextxy(500,300,"Message:");
for(i=0;i<16;i++)
for(j=0;j<17;j++)
Maze[j][i]=0;
for(i=0;i<17;i++)
{
Maze[0][i]=1;
Maze[15][i]=1;
}
for(i=0;i<16;i++)
{
Maze[i][0]=1;
Maze[i][16]=1;
}
} /* Paint */

void DrawCursor(int cx,int cy,int color)
{
setcolor(color);
rectangle(cx-7,cy-7,cx+7,cy+7);
} /* DrawCursor */

void DrawWall(int x,int y,int color)
{
void DrawCursor(int cx,int cy,int color);
register int i;
setcolor(color);
x*=30; y*=30;
for(i=y;i<=y+30;i++)
line(x,i,x+30,i);
for(i=x;i<=x+30;i++)
line(i,y,i,y+30);
DrawCursor(x+15,y+15,CURSORCOLOR);
} /* DrawWall */

void Refresh(void)
{
register int i,j;
setcolor(BKCOLOR);
for(i=1;i<=480;i++)
line(1,i,480,i);
for(j=1;j<=480;j++)
line(i,1,i,480);
setcolor(WALLCOLOR);
rectangle(29,29,481,451);
rectangle(28,28,482,452);
rectangle(30,30,480,450);
for(i=1;i<=15;i++)
for(j=1;j<=14;j++)
if(Maze[j][i]) {
DrawWall(i,j,WALLCOLOR);
DrawCursor(i*30+15,j*30+15,WALLCOLOR);
}
setcolor(CIRCLECOLOR);
circle(StartPlace.x*30+15,StartPlace.y*30+15,6);
circle(EndPlace.x*30+15,EndPlace.y*30+15,6);
setcolor(BKCOLOR);
for(i=491;i<=629;i++)
line(i,321,i,469);
setcolor(BROWN);
setfillstyle(SOLID_FILL,BROWN);
fillellipse(StartPlace.x*30+15,StartPlace.y*30+15,6,6);
setcolor(CYAN);
setfillstyle(SOLID_FILL,CYAN);
fillellipse(EndPlace.x*30+15,EndPlace.y*30+15,6,6);
} /* Refresh */

void CreatMaze(void)
{
void Error(char *message);
char c;
int i,j;
Boolean flag=TRUE;
int x=1,y=1;
Boolean start=FALSE,end=FALSE;
DrawCursor(45,45,CURSORCOLOR);
do
{
c=getch();
switch(c)
{
case Up: if(y>1) {
if(!Maze[y][x])
DrawCursor(x*30+15,y*30+15,BKCOLOR);
else DrawCursor(x*30+15,y*30+15,WALLCOLOR);
DrawCursor(x*30+15,(--y)*30+15,CURSORCOLOR); }
break;
case Down: if(y<14) {
if(!Maze[y][x])
DrawCursor(x*30+15,y*30+15,BKCOLOR);
else DrawCursor(x*30+15,y*30+15,WALLCOLOR);
DrawCursor(x*30+15,(++y)*30+15,CURSORCOLOR); }
break;
case Left: if(x>1) {
if(!Maze[y][x])
DrawCursor(x*30+15,y*30+15,BKCOLOR);
else DrawCursor(x*30+15,y*30+15,WALLCOLOR);
DrawCursor((--x)*30+15,y*30+15,CURSORCOLOR); }
break;
case Right: if(x<15) {
if(!Maze[y][x])
DrawCursor(x*30+15,y*30+15,BKCOLOR);
else DrawCursor(x*30+15,y*30+15,WALLCOLOR);
DrawCursor((++x)*30+15,y*30+15,CURSORCOLOR); }
break;
case Home: if(!start&&!Maze[y][x]) {
setcolor(6);
setfillstyle(SOLID_FILL,6);
fillellipse(x*30+15,y*30+15,6,6);
start=TRUE; StartPlace.x=x;StartPlace.y=y; }
break;
case End: if(!end&&!Maze[y][x]) {
setcolor(CYAN);
setfillstyle(SOLID_FILL,CYAN);
fillellipse(x*30+15,y*30+15,6,6);
end=TRUE; EndPlace.x=x; EndPlace.y=y; }
break;
case Esc: setcolor(TEXTCOLOR);
outtextxy(540,380,"exit");
sleep(1);
closegraph();
exit(1);
case F9: if(start&&end) { Refresh(); flag=FALSE; }
else
{
setcolor(TEXTCOLOR);
outtextxy(493,323," Error: ");
outtextxy(493,343," You must set ");
if(!start) outtextxy(493,363," startplace. ");
else outtextxy(493,363," endplace.");
}
break;
case Enter: if(!(x==StartPlace.x&&y==StartPlace.y)&&
!(x==EndPlace.x&&y==EndPlace.y))
{
DrawWall(x,y,WALLCOLOR);
Maze[y][x]=1;
}
break;
case Del: DrawWall(x,y,BKCOLOR); setcolor(4);
rectangle(x*30,y*30,x*30+30,y*30+30);
Maze[y][x]=0;
if(x==StartPlace.x&&y==StartPlace.y)
{
StartPlace.x=0;
StartPlace.y=0;
start=FALSE;
}
if(x==EndPlace.x&&y==EndPlace.y)
{
EndPlace.x=0;
EndPlace.y=0;
end=FALSE;
}
break;
default: break;
}

}
while(flag);
for(i=0;i<17;i++)
for(j=0;j<16;j++)
{
maze[j][i].td=1-Maze[j][i];
maze[j][i].mark=0;
maze[j][i].foot=0;
}
} /* CreatMaze */

void Error(char *message)
{
closegraph();
fprintf(stderr,"Error:%s\n",message);
exit(1);
} /* Error */

Status InitStack(Stack *s)
{
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)
{
s->top=NULL;
s->stacksize=0;
free(s->base);
s->base=NULL;
return OK;
} /* DestroyStack */

Status ClearStack(Stack *s)
{
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
} /* ClearStack */

Boolean StackEmpty(Stack *s)
{
if(s->top==s->base) return TRUE;
else return FALSE;
} /* StackEmpty */

int StackLength(Stack *s)
{
if(s->top>s->base) return (int)(s->top-s->base);
else return 0;
} /* StackLength */

Status Push(Stack *s,SElemType e)
{
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)
{
if(s->top==s->base) Error("Pop");
e=*--s->top;
return e;
} /* Pop */

Status GetTop(Stack *s,SElemType *e)
{
if(s->top==s->base) Error("GetTop");
*e=*(s->top-1);
return OK;
} /* GetTop */

/*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)
{
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)
{
maze[seat.x][seat.y].mark=-1;
} /* MarkPrint */

void FootPrint(PosType curpos)
{
maze[curpos.x][curpos.y].foot=1;
} /* FootPrint */

PosType NextPos(PosType seat,int di)
{
switch(di)
{
case 1: seat.y++; return seat;

case 2: seat.x++; return seat;

case 3: seat.y--; return seat;

case 4: seat.x--; return seat;

default: seat.x=0; seat.y=0; return seat;
}
} /* NextPos */

Status MazePath(PosType start,PosType end)
{
PosType curpos;
int curstep;
SElemType e;
Stack *s=NULL,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;
setcolor(BLUE);
circle(curpos.y*30+15,curpos.x*30+15,6);
delay(8000);
Push(s,e);
if(curpos.x==end.x&&curpos.y==end.y)
{
DestroyStack(s);
return TRUE;
}
curpos=NextPos(curpos,1);
curstep++;
}
else
{
if(!StackEmpty(s))
{
e=Pop(s,e);
while(e.di==4&&!StackEmpty(s))
{
MarkPrint(e.seat);
setcolor(BLUE);
circle(e.seat.y*30+15,e.seat.x*30+15,6);
delay(8000);
setcolor(BKCOLOR);
circle(e.seat.y*30+15,e.seat.x*30+15,6);
e=Pop(s,e);
curstep--;
}
if(e.di<4)
{
e.di++;
Push(s,e);
curpos=NextPos(e.seat,e.di);
}
}
}
}
while(!StackEmpty(s));
DestroyStack(s);
return FALSE;
} /* MazePath */

void main(void)
{
PosType start,end;
Paint();
CreatMaze();
start.x=StartPlace.y;
start.y=StartPlace.x;
end.x=EndPlace.y;
end.y=EndPlace.x;
if(MazePath(start,end))
{
setcolor(TEXTCOLOR);
outtextxy(520,380,"Path found");
}
else
{
setcolor(TEXTCOLOR);
outtextxy(500,380,"Path not found");
}
while(bioskey(1)==0);
}

高分跪求c++迷宫求解问题 (要用深度优先和广度优先两种方法)
试编程找到从任意一点(x1,y1)到任意一点(x2,y2)的【最短】路径。路径不要直接输出,要先... RT现有一个M*N的迷宫,迷宫的地图用二维数组存储。其中,0表示此顶点可以通过,1表示不能通过。试编程找到从任意一点(x1,y1)到任意一点(x2,y2)的【最短】路径。路径不要直接输出,要先用一种结构把它存起来,找到...

DFS&BFS(附迷宫解法C++代码)
DFS和BFS,即深度优先搜索与广度优先搜索,是解决问题时常用的两种策略。它们常应用于图或树的查找与遍历问题,或等价为图或树的问题中。广度优先搜索从出发点开始,逐层深入地遍历,先访问距离出发点最近的节点,因此更重视全局视角,目标是在最近的位置找到解。深度优先搜索则深入至某一路径的尽头,再回...

广度优先搜索C语言算法
例如:迷宫的最短路径计算,推箱子的移动最小步数等小游戏,都是按广度搜索来进行的。这个算法是教程中很经典的,有很多例子和代码。你可以好好研究!如下是一段迷宫的最佳路径求解算法。include const int dx[4]={-1,0,1,0};const int dy[4]={0,1,0,-1};int maze[5][5],prev[5][5]...

PASCAL问题 迷宫 迷宫问题
深度搜索:var i,j,n,m,ans:longint;a:array[1..100,1..100] of longint;procedure f(x,y,step:longint);begin if (x=n)and(y=m) then begin if step<ans then ans:=step;exit;end;if (x<1)or(y<1)or(x>n)or(y>m) then exit;if a[x,y]=1 then exit;f(x+1,y,s...

求广度优先算法C++走迷宫程序,可以显示路径
用的是深度优先的算法,可以寻找到走出迷宫的路径 但本题要求求出最短的路径,这就要使用广度优先的算法 一般在程序中需要用到先进先出的队列数据结构 下面是程序的代码,主要原理是用到 quei,quej和prep三个数组来构成队列 分别储存路径的行,列坐标和上一个节点在队列中的位置 大致算法如下,右三个...

程序设计 老鼠走迷宫
程序设计老鼠走迷宫可以通过搜索算法、深度优先搜索(DFS)、广度优先搜索(BFS)或A*算法等方法实现。程序设计老鼠走迷宫的核心是找到从起点到终点的最短或最优路径。一种常见的方法是使用搜索算法。搜索算法的工作原理是探索迷宫中的所有可能路径,直到找到出口。深度优先搜索(DFS)和广度优先搜索(BFS)是两种...

走迷宫的原理
1. 深度优先搜索(DFS):DFS 是一种基于递归的搜索策略,其原理是从起点开始,沿着一条路径不断向迷宫深处探索,直到无法继续前进为止。此时,回溯到上一步并尝试其他可能的路径。DFS 算法在搜索过程中不关心路径的长短,因此在解决迷宫问题时可能会导致非最短路径。2. 广度优先搜索(BFS):BFS 是一...

迷宫问题
深度优先算法思路:进行搜索的时候面对很多选择时,每遇到一个合适的就继续递归搜索下去,若失败则回溯到上一层。若画成一颗树的话就是按层数不断往下搜索,失败的时候返回上一层。广度优先算法思路:进行搜索的时候面对很多选择时,先不急着进行递归,而是先把所有的选择都放到一个队列中,然后依次对这些...

广度优先搜索算法和深度优先搜索算法有什么区别?
广度优先搜索(BFS)则像一位善于交际的能手,它同时与起始节点的邻居们交流,然后一层层地向外拓展。这种策略通过队列来组织搜索过程。DFS在最坏情况下可能需要存储整个深度的节点,空间复杂度与图的深度有关。BFS在最坏情况下需要存储所有与起始节点距离为d的节点,空间复杂度与图的宽度相关。DFS常用于...

C++ 基本算法 - 广度优先搜索
广度优先搜索(BFS)是算法中一种以广度为优先的遍历方式,与深度优先搜索(DFS)的深度优先策略截然不同。它在遇到岔口时,会先访问直接可达的所有节点,形成类似水波扩散的效果。以下是广度优先搜索在迷宫问题中的具体实现:在迷宫中,从起点 A 开始,BFS 会首先访问 A(第一层),接着是 B 和 C...

相似回答