前些日子,帮一个朋友做了个作业,是关于迷宫求解的演示
迷宫求解是个很经典的问题,这个我想不是讨论的问题
我想让大家看看我的思路,提提意见
下面是程式
//头文档
#ifndef Labyrinth_StructH
#define Labyrinth_StructH
#include <vcl.h>
#pragma hdrstop
typedef void __fastcall (__closure *TMyEvent)(int ID);
const int EVENT_PAINT=0;
const int EVENT_BACKDATE=1;
const int EVENT_OK=3;
const int STATE_ERROR=-1;
const int STATE_PASS=0;//能够通过
const int STATE_PASSED=1; //标记已通过
const int STATE_CANNOTPASS=2;//不能通过
const int STATE_ENTRY=3; //入口点
const int STATE_END=4; //出口点
class SPoint
{
public:
__fastcall ~SPoint()
{
}
__fastcall SPoint()
{
PriorityDirection=1;
Count=-1;
}
__fastcall SPoint(TPoint tPoint)
{
PriorityDirection=1;
Count=-1;
X=tPoint.x;
Y=tPoint.y;
}
__fastcall SPoint(int tX,int tY)
{
PriorityDirection=1;
Count=-1;
X=tX;
Y=tY;
}
int operator==(const SPoint& Source)
{
return(Source.X==X&&Source.Y==Y);
}
int operator!=(const SPoint& Source)
{
return(Source.X!=X||Source.Y!=Y);
}
int X;
int Y;
int Count;
int PriorityDirection;//一个优先方向另加四个方向
};
template<class T>class CShed //栈
{
public:
TList *List;
__fastcall CShed();
__fastcall ~CShed();
T *Pop();
void Push(T *point);
};
class CLabyrinth
{
TCanvas *LabyrinthCanvas;
int ImageHeight;
int ImageWidth;
int LabyrinthHeight;
int LabyrinthWidth;
int LabyrinthCol;
int LabyrinthRow;
int UnitHeight;
int UnitWidth;
int LeftSpace;
int TopSpace;
TStringList *AlreadPassList;
int *LabyrinthData;
Graphics::TBitmap *Bitmap_STATE_PASS;
Graphics::TBitmap *Bitmap_STATE_PASSED;
Graphics::TBitmap *Bitmap_STATE_CANNOTPASS;
Graphics::TBitmap *Bitmap_STATE_ENTRY;
Graphics::TBitmap *Bitmap_STATE_END;
void SetMemory();
void SetRowCol(int tRow,int tCol);
bool GetNewPoint(int Direction,int &tRow,int &tCol,int &tState);
void SetState(int tRow,int tCol,int tState);
public:
__fastcall CLabyrinth(AnsiString FileName);
__fastcall CLabyrinth(int tWidth,int tHeight,TCanvas *tCanvas=NULL);
__fastcall ~CLabyrinth();
void __fastcall Assin(const CLabyrinth& Source)
{
LabyrinthCol=Source.LabyrinthCol;
LabyrinthRow=Source.LabyrinthRow;
LabyrinthData=new int[LabyrinthRow*LabyrinthCol];
for(int k=0;k<LabyrinthRow*LabyrinthCol;k )
LabyrinthData[k]=Source.LabyrinthData[k];
}
public:
void ChangRowCol(int tRow,int tCol);
void GetRowCol(int &tRow,int &tCol);
void Updata();
void SetSize(int tWidth,int tHeight);
void GetSize(int &tWidth,int &tHeight);
void GetSpace(int &tLeftSpace,int &tTopSpace);
void SetCanvas(TCanvas *tCanvas);
void Refresh();//整个画板进行刷新
void Refresh(TRect rect,int tState=-1);
void Refresh(TPoint point,int tState=-1);
void Refresh(int tRow,int tCol,int tState=-1);
void SaveToFile(AnsiString tFileName="");
bool LoadFromFile(AnsiString tFileName="");
int ConvertRC(int tRow,int tCol);
int GetState(int tRow,int tCol);
int GetState(TPoint tPoint);
void ChangeState(int OldState,int NewState,int Times=-1);
void FindResult(TPoint *StartPoint=NULL);
public:
bool PowerExit;
bool IsChange;
AnsiString FileName;
TMyEvent FMyEvent;
};
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//下面是实现部分
#include <stdio.h>
#pragma hdrstop
#include "Labyrinth_Struct.h"
__fastcall CLabyrinth::CLabyrinth(AnsiString FileName)
{
LoadFromFile(FileName);
}
void CLabyrinth::Updata()
{
UnitHeight=ImageHeight/LabyrinthRow;
UnitWidth=ImageWidth/LabyrinthCol;
LabyrinthHeight=UnitHeight*LabyrinthRow;
LabyrinthWidth=UnitWidth*LabyrinthCol;
LeftSpace=(ImageWidth-LabyrinthWidth)/2;
TopSpace=(ImageHeight-LabyrinthHeight)/2;
}
__fastcall CLabyrinth::CLabyrinth(int tWidth,int tHeight,TCanvas *tCanvas)
{
LabyrinthData=NULL;
PowerExit=false;
AlreadPassList=new TStringList();
Bitmap_STATE_PASS=new Graphics::TBitmap();
Bitmap_STATE_PASSED=new Graphics::TBitmap();
Bitmap_STATE_CANNOTPASS=new Graphics::TBitmap();
Bitmap_STATE_ENTRY=new Graphics::TBitmap();
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




