迷宫算法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
迷宫算法实例
北航罗振伟(842962969),欢迎交流!
运行结果
// mazeView.cpp : implementation of the CMazeView class
//
#include "stdafx.h"
#include "maze.h"
#include "InputDlg.h"
#include "mazeDoc.h"
#include "mazeView.h"
#define Y 20
#define X 100
#define MaxSize 1000
#define M 20
#define N 18
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMazeView
IMPLEMENT_DYNCREATE(CMazeView, CView)
BEGIN_MESSAGE_MAP(CMazeView, CView)
//{{AFX_MSG_MAP(CMazeView)
ON_COMMAND(ID_find, Onfind)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMazeView construction/destruction
CMazeView::CMazeView()
{
// TODO: add construction code here
}
CMazeView::~CMazeView()
{
}
int mgg[M+2][N+2]=
{
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,2},
{2,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,2},
{2,0,0,0,1,0,1,1,1,0,0,0,1,1,1,0,1,0,0,2},
{2,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,2},
{2,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,2},
{2,0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,0,0,0,2},
{2,0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,1,0,0,2},
{2,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,1,0,0,2},
{2,0,0,0,0,0,1,1,1,0,1,0,1,1,1,0,1,0,0,2},
{2,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,2},
{2,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,1,1,1,1,0,0,1,1,1,1,1,0,0,0,1,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,2},
{2,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,2},
{2,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,2},
{2,0,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,1,1,2},
{2,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,2},
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
};
int mg[M+2][N+2];
int a=1,b=1,t=0;
struct
{ int i,j; /*方块的位置*/
int pre; /*本路径中上一方块在Qu中的下标*/
}Qu[MaxSize];
int front=-1,rear=-1; /*分别为队首指针和队尾指针*/
void CMazeView::print(int front) /*从队列中输出路径*/
{
int k=front,j,ns=0;
do /*反向找到最短路径,将该路径上的方块的pre成员设置成-1*/ { j=k;
drawrect(Qu[k].j,Qu[k].i,RGB(255,0,0,));
k=Qu[k].pre;
} while (k!=0);
drawrect(Qu[k].j,Qu[k].i,RGB(255,0,0,));k=0;
}
void CMazeView::mgpath(int xi,int yi,int xe,int ye) /*搜索路径为:(xi,yi)->(xe,ye)*/
{
front=-1,rear=-1;
int i,j,find=0,di;
rear++;
Qu[rear].i=xi;Qu[rear].j=yi; /*(xi,yi)进队*/
Qu[rear].pre=-1;
mg[1][1]=-1; /*将其赋值-1,以避免回过来重复搜索*/
while (front<=rear) /*队列不为空且未找到路径时循环*/
{
front++; /*出队,由于不是环形队列,该出队元素仍在队列中*/
i=Qu[front].i;j=Qu[front].j;
if (i==xe && j==ye) /*找到了出口,输出路径*/
{
find=1;
print(front); /*调用print函数输出路径*/
}
for (di=0;di<4;di++) /*循环扫描每个方位,把每个可走的方块插入队列中*/
{
switch(di)
{
case 0:i=Qu[front].i-1;j=Qu[front].j;break;