📄 网络社团--技术文档--迷宫求解的过程演示.htm
字号:
STATE_PASS=0;//可以通过<BR>const int STATE_PASSED=1; //标记已经通过<BR>const
int STATE_CANNOTPASS=2;//不能通过<BR>const int STATE_ENTRY=3;
//入口点<BR>const int STATE_END=4; //出口点</P>
<P>class SPoint<BR>{<BR>public:</P>
<P> __fastcall ~SPoint()<BR> {</P>
<P> }<BR> __fastcall SPoint()<BR>
{<BR> PriorityDirection=1;<BR>
Count=-1;<BR> }<BR> __fastcall SPoint(TPoint
tPoint)<BR> {<BR>
PriorityDirection=1;<BR> Count=-1;<BR>
X=tPoint.x;<BR> Y=tPoint.y;<BR> }<BR>
__fastcall SPoint(int tX,int tY)<BR> {<BR>
PriorityDirection=1;<BR> Count=-1;<BR>
X=tX;<BR> Y=tY;<BR> }<BR> int
operator==(const SPoint& Source)<BR> {<BR>
return(Source.X==X&&Source.Y==Y);<BR> }<BR> int
operator!=(const SPoint& Source)<BR> {<BR>
return(Source.X!=X||Source.Y!=Y);<BR> }</P>
<P>int X;<BR>int Y;<BR>int Count;<BR>int
PriorityDirection;//一个优先方向另加四个方向<BR>};</P>
<P><BR>template<class T>class CShed
//栈<BR>{<BR>public:<BR> TList *List;<BR> __fastcall
CShed();<BR> __fastcall ~CShed();<BR> T
*Pop();<BR> void Push(T *point);</P>
<P>};</P>
<P> </P>
<P>class CLabyrinth<BR> {<BR>
TCanvas *LabyrinthCanvas;<BR> int
ImageHeight;<BR> int ImageWidth;</P>
<P> int
LabyrinthHeight;<BR> int
LabyrinthWidth;</P>
<P> int
LabyrinthCol;<BR> int
LabyrinthRow;</P>
<P> int
UnitHeight;<BR> int UnitWidth;</P>
<P> int
LeftSpace;<BR> int TopSpace;</P>
<P> TStringList
*AlreadPassList;<BR> int
*LabyrinthData;</P>
<P> Graphics::TBitmap
*Bitmap_STATE_PASS;<BR>
Graphics::TBitmap
*Bitmap_STATE_PASSED;<BR>
Graphics::TBitmap
*Bitmap_STATE_CANNOTPASS;<BR>
Graphics::TBitmap
*Bitmap_STATE_ENTRY;<BR>
Graphics::TBitmap
*Bitmap_STATE_END;<BR> void
SetMemory();<BR> void SetRowCol(int
tRow,int tCol);<BR> bool
GetNewPoint(int Direction,int &tRow,int &tCol,int
&tState);<BR> void SetState(int
tRow,int tCol,int tState);<BR>
public:<BR> __fastcall
CLabyrinth(AnsiString FileName);<BR>
__fastcall CLabyrinth(int tWidth,int tHeight,TCanvas
*tCanvas=NULL);<BR> __fastcall
~CLabyrinth();<BR> void __fastcall
Assin(const CLabyrinth&
Source)<BR>
{<BR>
LabyrinthCol=Source.LabyrinthCol;<BR>
LabyrinthRow=Source.LabyrinthRow;<BR>
LabyrinthData=new
int[LabyrinthRow*LabyrinthCol];<BR>
for(int
k=0;k<LabyrinthRow*LabyrinthCol;k++)<BR>
LabyrinthData[k]=Source.LabyrinthData[k];<BR>
}</P>
<P> public:<BR> void
ChangRowCol(int tRow,int tCol);<BR>
void GetRowCol(int &tRow,int
&tCol);<BR> void Updata();</P>
<P> void SetSize(int tWidth,int
tHeight);<BR> void GetSize(int
&tWidth,int &tHeight);<BR>
void GetSpace(int &tLeftSpace,int
&tTopSpace);<BR> void
SetCanvas(TCanvas *tCanvas);<BR> void
Refresh();//整个画板进行刷新<BR> void
Refresh(TRect rect,int tState=-1);<BR>
void Refresh(TPoint point,int
tState=-1);<BR> void Refresh(int
tRow,int tCol,int tState=-1);<BR> void
SaveToFile(AnsiString
tFileName="");<BR> bool
LoadFromFile(AnsiString
tFileName="");<BR> int ConvertRC(int
tRow,int tCol);<BR> int GetState(int
tRow,int tCol);<BR> int
GetState(TPoint tPoint);<BR> void
ChangeState(int OldState,int NewState,int
Times=-1);<BR> void FindResult(TPoint
*StartPoint=NULL);<BR>
public:<BR> bool
PowerExit;<BR> bool
IsChange;<BR> AnsiString FileName;</P>
<P> TMyEvent FMyEvent;</P>
<P> };</P>
<P> </P>
<P><BR>#endif</P>
<P>////////////////////////////////////////////////////////////////////////////////////////////////////////////</P>
<P>//下面是实现部分</P>
<P>#include <stdio.h><BR>#pragma hdrstop<BR>#include
"Labyrinth_Struct.h"</P>
<P><BR>__fastcall CLabyrinth::CLabyrinth(AnsiString
FileName)<BR>{<BR> LoadFromFile(FileName);<BR>}<BR>void
CLabyrinth::Updata()<BR>{<BR> UnitHeight=ImageHeight/LabyrinthRow;<BR> UnitWidth=ImageWidth/LabyrinthCol;</P>
<P> LabyrinthHeight=UnitHeight*LabyrinthRow;<BR> LabyrinthWidth=UnitWidth*LabyrinthCol;</P>
<P> LeftSpace=(ImageWidth-LabyrinthWidth)/2;<BR> TopSpace=(ImageHeight-LabyrinthHeight)/2;</P>
<P>}</P>
<P>__fastcall CLabyrinth::CLabyrinth(int tWidth,int tHeight,TCanvas
*tCanvas)<BR>{<BR> LabyrinthData=NULL;<BR> PowerExit=false;<BR> AlreadPassList=new
TStringList();<BR> Bitmap_STATE_PASS=new
Graphics::TBitmap();<BR> Bitmap_STATE_PASSED=new
Graphics::TBitmap();<BR> Bitmap_STATE_CANNOTPASS=new
Graphics::TBitmap();<BR> Bitmap_STATE_ENTRY=new
Graphics::TBitmap();<BR> Bitmap_STATE_END=new
Graphics::TBitmap();<BR> Bitmap_STATE_PASS->LoadFromResourceName((int)HInstance,
"PASSNUNLL");<BR> Bitmap_STATE_PASSED->LoadFromResourceName((int)HInstance,
"PASS");<BR> Bitmap_STATE_CANNOTPASS->LoadFromResourceName((int)HInstance,
"CANNOTPASS");<BR> Bitmap_STATE_ENTRY->LoadFromResourceName((int)HInstance,
"STATE_ENTRY");<BR> Bitmap_STATE_END->LoadFromResourceName((int)HInstance,
"STATE_END");<BR> IsChange=false;<BR> LabyrinthData=NULL;<BR> LabyrinthCol=40;<BR> LabyrinthRow=40;<BR> LabyrinthCanvas=tCanvas;<BR> ImageHeight=tHeight;<BR> ImageWidth=tWidth;<BR> SetRowCol(LabyrinthRow,LabyrinthCol);<BR>}<BR>__fastcall
CLabyrinth::~CLabyrinth()<BR>{<BR> delete
Bitmap_STATE_PASS;<BR> delete
Bitmap_STATE_PASSED;<BR> delete
Bitmap_STATE_CANNOTPASS;<BR> delete
Bitmap_STATE_ENTRY;<BR> delete Bitmap_STATE_END;</P>
<P> if(LabyrinthData)<BR> {<BR> delete
[]LabyrinthData;<BR> LabyrinthData=NULL;<BR> }<BR>}</P>
<P><BR>void
CLabyrinth::SetMemory()<BR>{<BR> if(LabyrinthData)<BR>
{<BR> delete []LabyrinthData;<BR>
LabyrinthData=NULL;<BR> }</P>
<P> LabyrinthData=new
int[LabyrinthRow*LabyrinthCol];<BR> for(int
k=1;k<LabyrinthRow*LabyrinthCol;k++)<BR>
LabyrinthData[k]=STATE_PASS;<BR> LabyrinthData[0]=STATE_ENTRY;<BR> LabyrinthData[LabyrinthRow*LabyrinthCol-1]=STATE_END;<BR>}<BR>void
CLabyrinth::SetRowCol(int tRow,int
tCol)<BR>{<BR> LabyrinthCol=tCol;<BR> LabyrinthRow=tRow;<BR> Updata();<BR> SetMemory();<BR>}<BR>void
CLabyrinth::SetCanvas(TCanvas
*tCanvas)<BR>{<BR> LabyrinthCanvas=tCanvas;<BR>}<BR>void
CLabyrinth::Refresh()<BR>{<BR> if(LabyrinthCanvas!=NULL)<BR>
{<BR> LabyrinthCanvas->Brush->Color=clBlack;<BR>
LabyrinthCanvas->FillRect(Rect(0,0,ImageWidth,ImageHeight));<BR>
}<BR> for(int k=0;k<LabyrinthRow;k++)<BR>
for(int l=0;l<LabyrinthCol;l++)<BR>
Refresh(k+1,l+1);<BR>}<BR>void CLabyrinth::Refresh(TRect rect,int
tState)<BR>{<BR> int
tLeft,tTop,tRight,tBottom;<BR> tLeft=rect.Left-LeftSpace;<BR> tTop=rect.Top-TopSpace;<BR> tBottom=rect.Bottom-TopSpace;<BR> tRight=rect.Right-LeftSpace;</P>
<P> int tLeftCol=tLeft/UnitWidth+1;<BR> int
tLeftRow=tTop/UnitHeight+1;<BR> int
tRightCol=tRight/UnitWidth;<BR> int
tRightRow=tBottom/UnitHeight;<BR> tRightCol+=tRight%UnitWidth>0?1:0;<BR> tRightRow+=tBottom%UnitHeight>0?1:0;<BR> for(int
k=tLeftRow;k<=tRightRow;k++)<BR> for(int
l=tLeftCol;l<=tRightCol;l++)<BR>
Refresh(k,l,tState);</P>
<P>}<BR>void CLabyrinth::Refresh(TPoint point,int
tState)<BR>{<BR> int
tCol=(point.x-LeftSpace)/UnitWidth;<BR> int
tRow=(point.y-TopSpace)/UnitHeight;<BR>
tCol+=point.x%UnitWidth>0?1:0;<BR>
tRow+=point.y%UnitHeight>0?1:0;<BR>
Refresh(tRow,tCol,tState);</P>
<P>}<BR>void CLabyrinth::Refresh(int tRow,int tCol,int
tState)<BR>{<BR> if(LabyrinthCanvas==NULL)<BR>
return;<BR> if(tRow<0||tCol<0||tRow>LabyrinthRow||tCol>LabyrinthCol)<BR>
return;<BR> int
OldState=LabyrinthData[ConvertRC(tRow,tCol)];<BR> if(tState!=-1)<BR>
{<BR>
if(OldState==STATE_ENTRY||OldState==STATE_END)<BR>
return;<BR>
LabyrinthData[ConvertRC(tRow,tCol)]=tState;<BR>
IsChange=true;<BR> }<BR> else<BR>
tState=OldState;</P>
<P> switch(tState)<BR> {<BR> case STATE_PASS:
LabyrinthCanvas->StretchDraw(Rect(UnitWidth*(tCol-1)+LeftSpace,UnitHeight*(tRow-1)+TopSpace,UnitWidth*tCol+LeftSpace,UnitHeight*tRow+TopSpace),Bitmap_STATE_PASS);break;<BR>
case
STATE_PASSED:LabyrinthCanvas->StretchDraw(Rect(UnitWidth*(tCol-1)+LeftSpace,UnitHeight*(tRow-1)+TopSpace,UnitWidth*tCol+LeftSpace,UnitHeight*tRow+TopSpace),Bitmap_STATE_PASSED);
break;<BR> case
STATE_CANNOTPASS:LabyrinthCanvas->StretchDraw(Rect(UnitWidth*(tCol-1)+LeftSpace,UnitHeight*(tRow-1)+TopSpace,UnitWidth*tCol+LeftSpace,UnitHeight*tRow+TopSpace),Bitmap_STATE_CANNOTPASS);
break;<BR> case
STATE_ENTRY:LabyrinthCanvas->StretchDraw(Rect(UnitWidth*(tCol-1)+LeftSpace,UnitHeight*(tRow-1)+TopSpace,UnitWidth*tCol+LeftSpace,UnitHeight*tRow+TopSpace),Bitmap_STATE_ENTRY);
break;<BR> case
STATE_END:LabyrinthCanvas->StretchDraw(Rect(UnitWidth*(tCol-1)+LeftSpace,UnitHeight*(tRow-1)+TopSpace,UnitWidth*tCol+LeftSpace,UnitHeight*tRow+TopSpace),Bitmap_STATE_END);
break;<BR> default: break;<BR> }</P>
<P><BR>}<BR>int CLabyrinth::ConvertRC(int tRow,int
tCol)<BR>{<BR> return((tRow-1)*LabyrinthCol+tCol-1);</P>
<P>}</P>
<P>void CLabyrinth::SetSize(int tWidth,int
tHeight)<BR>{<BR> LabyrinthHeight=tHeight;<BR> LabyrinthWidth=tWidth;<BR> UnitHeight=LabyrinthHeight/LabyrinthRow;<BR> UnitWidth=LabyrinthWidth/LabyrinthCol;<BR> LabyrinthHeight=UnitHeight*LabyrinthRow;<BR> LabyrinthWidth=UnitWidth*LabyrinthCol;<BR> Refresh();</P>
<P><BR>}<BR>void CLabyrinth::GetSpace(int &tLeftSpace,int
&tTopSpace)<BR>{<BR> tLeftSpace=LeftSpace;<BR> tTopSpace=TopSpace;</P>
<P>}</P>
<P>void CLabyrinth::GetSize(int &tWidth,int
&tHeight)<BR>{<BR> tHeight=LabyrinthHeight;<BR> tWidth=LabyrinthWidth;</P>
<P>}<BR>int CLabyrinth::GetState(int tRow,int
tCol)<BR>{<BR> if(tRow>0&&tCol>00&&tRow<=LabyrinthRow&&tCol<=LabyrinthCol)<BR>
return(LabyrinthData[ConvertRC(tRow,tCol)]);<BR> return
STATE_ERROR;<BR>}</P>
<P>int CLabyrinth::GetState(TPoint tPoint)<BR>{<BR> int
tCol=tPoint.x/UnitWidth;<BR> int
tRow=tPoint.y/UnitHeight;<BR>
tCol+=tPoint.x%UnitWidth>0?1:0;<BR>
tRow+=tPoint.y%UnitHeight>0?1:0;<BR>
if(tRow>0&&tCol>0&&tRow<=LabyrinthRow&&tCol<=LabyrinthCol)<BR>
return(LabyrinthData[ConvertRC(tRow,tCol)]);<BR> return
STATE_ERROR;</P>
<P>}<BR>void CLabyrinth::SaveToFile(AnsiString
tFileName)<BR>{<BR>
if(tFileName.IsEmpty())<BR>
tFileName=FileName;<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -