⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 网络社团--技术文档--迷宫求解的过程演示.htm

📁 迷宫求解的过程演示.rar
💻 HTM
📖 第 1 页 / 共 3 页
字号:
            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>&nbsp; __fastcall ~SPoint()<BR>&nbsp; {</P>
            <P>&nbsp; }<BR>&nbsp; __fastcall SPoint()<BR>&nbsp; 
            {<BR>&nbsp;&nbsp; PriorityDirection=1;<BR>&nbsp;&nbsp; 
            Count=-1;<BR>&nbsp; }<BR>&nbsp; __fastcall SPoint(TPoint 
            tPoint)<BR>&nbsp; {<BR>&nbsp;&nbsp; 
            PriorityDirection=1;<BR>&nbsp;&nbsp; Count=-1;<BR>&nbsp;&nbsp; 
            X=tPoint.x;<BR>&nbsp;&nbsp; Y=tPoint.y;<BR>&nbsp; }<BR>&nbsp; 
            __fastcall SPoint(int tX,int tY)<BR>&nbsp; {<BR>&nbsp;&nbsp; 
            PriorityDirection=1;<BR>&nbsp;&nbsp; Count=-1;<BR>&nbsp;&nbsp; 
            X=tX;<BR>&nbsp;&nbsp; Y=tY;<BR>&nbsp; }<BR>&nbsp;int 
            operator==(const SPoint&amp; Source)<BR>&nbsp; {<BR>&nbsp;&nbsp; 
            return(Source.X==X&amp;&amp;Source.Y==Y);<BR>&nbsp; }<BR>&nbsp;int 
            operator!=(const SPoint&amp; Source)<BR>&nbsp; {<BR>&nbsp;&nbsp; 
            return(Source.X!=X||Source.Y!=Y);<BR>&nbsp; }</P>
            <P>int X;<BR>int Y;<BR>int Count;<BR>int 
            PriorityDirection;//一个优先方向另加四个方向<BR>};</P>
            <P><BR>template&lt;class T&gt;class CShed 
            //栈<BR>{<BR>public:<BR>&nbsp; TList *List;<BR>&nbsp; __fastcall 
            CShed();<BR>&nbsp; __fastcall ~CShed();<BR>&nbsp; T 
            *Pop();<BR>&nbsp; void Push(T *point);</P>
            <P>};</P>
            <P>&nbsp;</P>
            <P>class CLabyrinth<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            TCanvas *LabyrinthCanvas;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int 
            ImageHeight;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int ImageWidth;</P>
            <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int 
            LabyrinthHeight;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int 
            LabyrinthWidth;</P>
            <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int 
            LabyrinthCol;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int 
LabyrinthRow;</P>
            <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int 
            UnitHeight;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int UnitWidth;</P>
            <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int 
            LeftSpace;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int TopSpace;</P>
            <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TStringList 
            *AlreadPassList;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int 
            *LabyrinthData;</P>
            <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Graphics::TBitmap 
            *Bitmap_STATE_PASS;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            Graphics::TBitmap 
            *Bitmap_STATE_PASSED;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            Graphics::TBitmap 
            *Bitmap_STATE_CANNOTPASS;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            Graphics::TBitmap 
            *Bitmap_STATE_ENTRY;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            Graphics::TBitmap 
            *Bitmap_STATE_END;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void 
            SetMemory();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void SetRowCol(int 
            tRow,int tCol);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool 
            GetNewPoint(int Direction,int &amp;tRow,int &amp;tCol,int 
            &amp;tState);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void SetState(int 
            tRow,int tCol,int tState);<BR>&nbsp;&nbsp;&nbsp; 
            public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __fastcall 
            CLabyrinth(AnsiString FileName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            __fastcall CLabyrinth(int tWidth,int tHeight,TCanvas 
            *tCanvas=NULL);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __fastcall 
            ~CLabyrinth();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void __fastcall 
            Assin(const CLabyrinth&amp; 
            Source)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            LabyrinthCol=Source.LabyrinthCol;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            LabyrinthRow=Source.LabyrinthRow;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            LabyrinthData=new 
            int[LabyrinthRow*LabyrinthCol];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            for(int 
            k=0;k&lt;LabyrinthRow*LabyrinthCol;k++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            LabyrinthData[k]=Source.LabyrinthData[k];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            }</P>
            <P>&nbsp;&nbsp;&nbsp; public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void 
            ChangRowCol(int tRow,int tCol);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            void GetRowCol(int &amp;tRow,int 
            &amp;tCol);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void Updata();</P>
            <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void SetSize(int tWidth,int 
            tHeight);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void GetSize(int 
            &amp;tWidth,int &amp;tHeight);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            void GetSpace(int &amp;tLeftSpace,int 
            &amp;tTopSpace);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void 
            SetCanvas(TCanvas *tCanvas);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void 
            Refresh();//整个画板进行刷新<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void 
            Refresh(TRect rect,int tState=-1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            void Refresh(TPoint point,int 
            tState=-1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void Refresh(int 
            tRow,int tCol,int tState=-1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void 
            SaveToFile(AnsiString 
            tFileName="");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool 
            LoadFromFile(AnsiString 
            tFileName="");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int ConvertRC(int 
            tRow,int tCol);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int GetState(int 
            tRow,int tCol);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int 
            GetState(TPoint tPoint);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void 
            ChangeState(int OldState,int NewState,int 
            Times=-1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void FindResult(TPoint 
            *StartPoint=NULL);<BR>&nbsp;&nbsp;&nbsp; 
            public:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool 
            PowerExit;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool 
            IsChange;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AnsiString FileName;</P>
            <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TMyEvent FMyEvent;</P>
            <P>&nbsp; };</P>
            <P>&nbsp;</P>
            <P><BR>#endif</P>
            <P>////////////////////////////////////////////////////////////////////////////////////////////////////////////</P>
            <P>//下面是实现部分</P>
            <P>#include &lt;stdio.h&gt;<BR>#pragma hdrstop<BR>#include 
            "Labyrinth_Struct.h"</P>
            <P><BR>__fastcall CLabyrinth::CLabyrinth(AnsiString 
            FileName)<BR>{<BR>&nbsp;LoadFromFile(FileName);<BR>}<BR>void 
            CLabyrinth::Updata()<BR>{<BR>&nbsp;UnitHeight=ImageHeight/LabyrinthRow;<BR>&nbsp;UnitWidth=ImageWidth/LabyrinthCol;</P>
            <P>&nbsp;LabyrinthHeight=UnitHeight*LabyrinthRow;<BR>&nbsp;LabyrinthWidth=UnitWidth*LabyrinthCol;</P>
            <P>&nbsp;LeftSpace=(ImageWidth-LabyrinthWidth)/2;<BR>&nbsp;TopSpace=(ImageHeight-LabyrinthHeight)/2;</P>
            <P>}</P>
            <P>__fastcall CLabyrinth::CLabyrinth(int tWidth,int tHeight,TCanvas 
            *tCanvas)<BR>{<BR>&nbsp;LabyrinthData=NULL;<BR>&nbsp;PowerExit=false;<BR>&nbsp;AlreadPassList=new 
            TStringList();<BR>&nbsp;Bitmap_STATE_PASS=new 
            Graphics::TBitmap();<BR>&nbsp;Bitmap_STATE_PASSED=new 
            Graphics::TBitmap();<BR>&nbsp;Bitmap_STATE_CANNOTPASS=new 
            Graphics::TBitmap();<BR>&nbsp;Bitmap_STATE_ENTRY=new 
            Graphics::TBitmap();<BR>&nbsp;Bitmap_STATE_END=new 
            Graphics::TBitmap();<BR>&nbsp;Bitmap_STATE_PASS-&gt;LoadFromResourceName((int)HInstance, 
            "PASSNUNLL");<BR>&nbsp;Bitmap_STATE_PASSED-&gt;LoadFromResourceName((int)HInstance, 
            "PASS");<BR>&nbsp;Bitmap_STATE_CANNOTPASS-&gt;LoadFromResourceName((int)HInstance, 
            "CANNOTPASS");<BR>&nbsp;Bitmap_STATE_ENTRY-&gt;LoadFromResourceName((int)HInstance, 
            "STATE_ENTRY");<BR>&nbsp;Bitmap_STATE_END-&gt;LoadFromResourceName((int)HInstance, 
            "STATE_END");<BR>&nbsp;IsChange=false;<BR>&nbsp;LabyrinthData=NULL;<BR>&nbsp;LabyrinthCol=40;<BR>&nbsp;LabyrinthRow=40;<BR>&nbsp;LabyrinthCanvas=tCanvas;<BR>&nbsp;ImageHeight=tHeight;<BR>&nbsp;ImageWidth=tWidth;<BR>&nbsp;SetRowCol(LabyrinthRow,LabyrinthCol);<BR>}<BR>__fastcall 
            CLabyrinth::~CLabyrinth()<BR>{<BR>&nbsp;delete 
            Bitmap_STATE_PASS;<BR>&nbsp;delete 
            Bitmap_STATE_PASSED;<BR>&nbsp;delete 
            Bitmap_STATE_CANNOTPASS;<BR>&nbsp;delete 
            Bitmap_STATE_ENTRY;<BR>&nbsp;delete Bitmap_STATE_END;</P>
            <P>&nbsp;if(LabyrinthData)<BR>&nbsp; {<BR>&nbsp; delete 
            []LabyrinthData;<BR>&nbsp; LabyrinthData=NULL;<BR>&nbsp; }<BR>}</P>
            <P><BR>void 
            CLabyrinth::SetMemory()<BR>{<BR>&nbsp;if(LabyrinthData)<BR>&nbsp; 
            {<BR>&nbsp; delete []LabyrinthData;<BR>&nbsp; 
            LabyrinthData=NULL;<BR>&nbsp; }</P>
            <P>&nbsp;LabyrinthData=new 
            int[LabyrinthRow*LabyrinthCol];<BR>&nbsp;for(int 
            k=1;k&lt;LabyrinthRow*LabyrinthCol;k++)<BR>&nbsp;&nbsp;&nbsp; 
            LabyrinthData[k]=STATE_PASS;<BR>&nbsp;LabyrinthData[0]=STATE_ENTRY;<BR>&nbsp;LabyrinthData[LabyrinthRow*LabyrinthCol-1]=STATE_END;<BR>}<BR>void 
            CLabyrinth::SetRowCol(int tRow,int 
            tCol)<BR>{<BR>&nbsp;LabyrinthCol=tCol;<BR>&nbsp;LabyrinthRow=tRow;<BR>&nbsp;Updata();<BR>&nbsp;SetMemory();<BR>}<BR>void 
            CLabyrinth::SetCanvas(TCanvas 
            *tCanvas)<BR>{<BR>&nbsp;LabyrinthCanvas=tCanvas;<BR>}<BR>void 
            CLabyrinth::Refresh()<BR>{<BR>&nbsp;if(LabyrinthCanvas!=NULL)<BR>&nbsp; 
            {<BR>&nbsp; LabyrinthCanvas-&gt;Brush-&gt;Color=clBlack;<BR>&nbsp; 
            LabyrinthCanvas-&gt;FillRect(Rect(0,0,ImageWidth,ImageHeight));<BR>&nbsp; 
            }<BR>&nbsp;for(int k=0;k&lt;LabyrinthRow;k++)<BR>&nbsp;&nbsp; 
            for(int l=0;l&lt;LabyrinthCol;l++)<BR>&nbsp;&nbsp;&nbsp; 
            Refresh(k+1,l+1);<BR>}<BR>void CLabyrinth::Refresh(TRect rect,int 
            tState)<BR>{<BR>&nbsp;int 
            tLeft,tTop,tRight,tBottom;<BR>&nbsp;tLeft=rect.Left-LeftSpace;<BR>&nbsp;tTop=rect.Top-TopSpace;<BR>&nbsp;tBottom=rect.Bottom-TopSpace;<BR>&nbsp;tRight=rect.Right-LeftSpace;</P>
            <P>&nbsp;int tLeftCol=tLeft/UnitWidth+1;<BR>&nbsp;int 
            tLeftRow=tTop/UnitHeight+1;<BR>&nbsp;int 
            tRightCol=tRight/UnitWidth;<BR>&nbsp;int 
            tRightRow=tBottom/UnitHeight;<BR>&nbsp;tRightCol+=tRight%UnitWidth&gt;0?1:0;<BR>&nbsp;tRightRow+=tBottom%UnitHeight&gt;0?1:0;<BR>&nbsp;for(int 
            k=tLeftRow;k&lt;=tRightRow;k++)<BR>&nbsp; for(int 
            l=tLeftCol;l&lt;=tRightCol;l++)<BR>&nbsp;&nbsp;&nbsp; 
            Refresh(k,l,tState);</P>
            <P>}<BR>void CLabyrinth::Refresh(TPoint point,int 
            tState)<BR>{<BR>&nbsp; int 
            tCol=(point.x-LeftSpace)/UnitWidth;<BR>&nbsp; int 
            tRow=(point.y-TopSpace)/UnitHeight;<BR>&nbsp; 
            tCol+=point.x%UnitWidth&gt;0?1:0;<BR>&nbsp; 
            tRow+=point.y%UnitHeight&gt;0?1:0;<BR>&nbsp; 
            Refresh(tRow,tCol,tState);</P>
            <P>}<BR>void CLabyrinth::Refresh(int tRow,int tCol,int 
            tState)<BR>{<BR>&nbsp;if(LabyrinthCanvas==NULL)<BR>&nbsp;&nbsp; 
            return;<BR>&nbsp;if(tRow&lt;0||tCol&lt;0||tRow&gt;LabyrinthRow||tCol&gt;LabyrinthCol)<BR>&nbsp;&nbsp; 
            return;<BR>&nbsp;int 
            OldState=LabyrinthData[ConvertRC(tRow,tCol)];<BR>&nbsp;if(tState!=-1)<BR>&nbsp; 
            {<BR>&nbsp; 
            if(OldState==STATE_ENTRY||OldState==STATE_END)<BR>&nbsp;&nbsp;&nbsp; 
            return;<BR>&nbsp; 
            LabyrinthData[ConvertRC(tRow,tCol)]=tState;<BR>&nbsp; 
            IsChange=true;<BR>&nbsp; }<BR>&nbsp; else<BR>&nbsp;&nbsp;&nbsp; 
            tState=OldState;</P>
            <P>&nbsp;switch(tState)<BR>&nbsp;{<BR>&nbsp; case STATE_PASS: 
            LabyrinthCanvas-&gt;StretchDraw(Rect(UnitWidth*(tCol-1)+LeftSpace,UnitHeight*(tRow-1)+TopSpace,UnitWidth*tCol+LeftSpace,UnitHeight*tRow+TopSpace),Bitmap_STATE_PASS);break;<BR>&nbsp; 
            case 
            STATE_PASSED:LabyrinthCanvas-&gt;StretchDraw(Rect(UnitWidth*(tCol-1)+LeftSpace,UnitHeight*(tRow-1)+TopSpace,UnitWidth*tCol+LeftSpace,UnitHeight*tRow+TopSpace),Bitmap_STATE_PASSED); 
            break;<BR>&nbsp; case 
            STATE_CANNOTPASS:LabyrinthCanvas-&gt;StretchDraw(Rect(UnitWidth*(tCol-1)+LeftSpace,UnitHeight*(tRow-1)+TopSpace,UnitWidth*tCol+LeftSpace,UnitHeight*tRow+TopSpace),Bitmap_STATE_CANNOTPASS); 
            break;<BR>&nbsp; case 
            STATE_ENTRY:LabyrinthCanvas-&gt;StretchDraw(Rect(UnitWidth*(tCol-1)+LeftSpace,UnitHeight*(tRow-1)+TopSpace,UnitWidth*tCol+LeftSpace,UnitHeight*tRow+TopSpace),Bitmap_STATE_ENTRY); 
            break;<BR>&nbsp; case 
            STATE_END:LabyrinthCanvas-&gt;StretchDraw(Rect(UnitWidth*(tCol-1)+LeftSpace,UnitHeight*(tRow-1)+TopSpace,UnitWidth*tCol+LeftSpace,UnitHeight*tRow+TopSpace),Bitmap_STATE_END); 
            break;<BR>&nbsp; default: break;<BR>&nbsp;}</P>
            <P><BR>}<BR>int CLabyrinth::ConvertRC(int tRow,int 
            tCol)<BR>{<BR>&nbsp;return((tRow-1)*LabyrinthCol+tCol-1);</P>
            <P>}</P>
            <P>void CLabyrinth::SetSize(int tWidth,int 
            tHeight)<BR>{<BR>&nbsp;LabyrinthHeight=tHeight;<BR>&nbsp;LabyrinthWidth=tWidth;<BR>&nbsp;UnitHeight=LabyrinthHeight/LabyrinthRow;<BR>&nbsp;UnitWidth=LabyrinthWidth/LabyrinthCol;<BR>&nbsp;LabyrinthHeight=UnitHeight*LabyrinthRow;<BR>&nbsp;LabyrinthWidth=UnitWidth*LabyrinthCol;<BR>&nbsp;Refresh();</P>
            <P><BR>}<BR>void CLabyrinth::GetSpace(int &amp;tLeftSpace,int 
            &amp;tTopSpace)<BR>{<BR>&nbsp;tLeftSpace=LeftSpace;<BR>&nbsp;tTopSpace=TopSpace;</P>
            <P>}</P>
            <P>void CLabyrinth::GetSize(int &amp;tWidth,int 
            &amp;tHeight)<BR>{<BR>&nbsp;tHeight=LabyrinthHeight;<BR>&nbsp;tWidth=LabyrinthWidth;</P>
            <P>}<BR>int CLabyrinth::GetState(int tRow,int 
            tCol)<BR>{<BR>&nbsp;if(tRow&gt;0&amp;&amp;tCol&gt;00&amp;&amp;tRow&lt;=LabyrinthRow&amp;&amp;tCol&lt;=LabyrinthCol)<BR>&nbsp;&nbsp; 
            return(LabyrinthData[ConvertRC(tRow,tCol)]);<BR>&nbsp;&nbsp; return 
            STATE_ERROR;<BR>}</P>
            <P>int CLabyrinth::GetState(TPoint tPoint)<BR>{<BR>&nbsp; int 
            tCol=tPoint.x/UnitWidth;<BR>&nbsp; int 
            tRow=tPoint.y/UnitHeight;<BR>&nbsp; 
            tCol+=tPoint.x%UnitWidth&gt;0?1:0;<BR>&nbsp; 
            tRow+=tPoint.y%UnitHeight&gt;0?1:0;<BR>&nbsp; 
            if(tRow&gt;0&amp;&amp;tCol&gt;0&amp;&amp;tRow&lt;=LabyrinthRow&amp;&amp;tCol&lt;=LabyrinthCol)<BR>&nbsp;&nbsp; 
            return(LabyrinthData[ConvertRC(tRow,tCol)]);<BR>&nbsp;&nbsp; return 
            STATE_ERROR;</P>
            <P>}<BR>void CLabyrinth::SaveToFile(AnsiString 
            tFileName)<BR>{<BR>&nbsp; 
            if(tFileName.IsEmpty())<BR>&nbsp;&nbsp;&nbsp; 
            tFileName=FileName;<BR>&nbsp; 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -