📄 超酷的走迷宫程序_数据结构与算法_数据结构算法_c语言_c 语言之家.htm
字号:
init(Sender);<BR>
man->Left=5;<BR>
man->Top=
5;<BR>
i=0;<BR>
j=0;<BR>}</P>
<P>void __fastcall
TForm1::ClearCanvas(TObject
*Sender)<BR>{<BR>
Canvas->Brush->Style=bsSolid;<BR>
Canvas->Brush->Color=clBtnFace;<BR>
Canvas->FillRect(ClientRect);<BR>}</P>
<P>void __fastcall TForm1::runByLeft(TObject
*Sender)<BR>{<BR>N6->Checked=true;<BR>N7->Checked=false;<BR>if((i<M-1)
|| (j<N-1))<BR>{<BR>switch( dir
){<BR> case
0:<BR>
//* 以3,0,1的次序尝试 */
<BR>
if((i<M-1) &&
(bg[i+1][j]==0))<BR>
{<BR>
y=y+step;i=i+1;<BR>
dir=3;<BR>
}
<BR>
else if((j>0) &&
(bg[i][j-1]==0))<BR>
{<BR>
x=x-step;j=j-1;<BR>
dir=0;
<BR>
}<BR>
else if((i>0) &&
(bg[i-1][j]==0))<BR>
{<BR>
y=y-step;i=i-1;<BR>
dir=1;
<BR>
}<BR>
else
<BR>
{<BR>
x=x+step;j=j+1;<BR>
dir=2;
<BR>
}<BR>
break;<BR> case
1:<BR>
if((j>0) &&
(bg[i][j-1]==0))<BR>
{<BR>
x=x-step;j=j-1;<BR>
dir=0;
<BR>
}<BR>
else if((i>0) &&
(bg[i-1][j]==0))<BR>
{<BR>
y=y-step;i=i-1;<BR>
dir=1;
<BR>
}<BR>
else if((j<N-1) &&
(bg[i][j+1]==0))<BR>
{<BR>
x=x+step;j=j+1;<BR>
dir=2;
<BR>
}<BR>
else<BR>
{
<BR>
y=y+step;i=i+1;<BR>
dir=3;
<BR> }
;<BR>
break;<BR> case
2:<BR>
if((i>0) &&
(bg[i-1][j]==0))<BR>
{<BR>
y=y-step;i=i-1;<BR>
dir=1;
<BR>
}
<BR>
else if((j<N-1) &&
(bg[i][j+1]==0))<BR>
{<BR>
x=x+step;j=j+1;<BR>
dir=2;
<BR>
}<BR>
else if((i<M-1) &&
(bg[i+1][j]==0))<BR>
{<BR>
y=y+step;i=i+1;<BR>
dir=3;
<BR> }
<BR>
else<BR>
{
<BR>
x=x-step;j=j-1;<BR>
dir=0;<BR>
} ;<BR>
break;<BR> case
3:<BR>
if((j<N-1) &&
(bg[i][j+1]==0))<BR>
{<BR>
x=x+step;j=j+1;<BR>
dir=2;
<BR>
}<BR>
else if((i<M-1) &&
(bg[i+1][j]==0))<BR>
{<BR>
y=y+step;i=i+1;<BR>
dir=3;
<BR>
}<BR>
else if((j>0) &&
(bg[i][j-1]==0))<BR>
{<BR>
x=x-step;j=j-1;<BR>
dir=0;<BR>
} <BR>
else
<BR> {
<BR>
y=y-step;i=i-1;<BR>
dir=1;<BR>
}<BR>
break;<BR> }
<BR>
man->Left=x;<BR>
man->Top=y;<BR>}<BR>
//--------<BR>drawMaze(Sender);<BR>}</P>
<P>void __fastcall TForm1::runShort(TObject
*Sender)<BR>{<BR>N7->Checked=true;<BR>N6->Checked=false;<BR>if
((bestroad[runnum].rj<=7) &&
(bestroad[runnum].ri<=7)
)<BR>{<BR>
man->Left=bestroad[runnum].rj*step;<BR>
man->Top=bestroad[runnum].ri*step;<BR>}
;<BR>count=count+1;<BR>drawMaze(Sender);<BR>runnum=runnum+1;<BR>}</P>
<P>void __fastcall TForm1::clkLeft(TObject
*Sender)<BR>{<BR>N6->Checked=true;<BR>N7->Checked=false;<BR>}</P>
<P>void __fastcall TForm1::clkShort(TObject
*Sender)<BR>{<BR>N7->Checked=true;<BR>N6->Checked=false;<BR>}</P>
<P>void __fastcall
TForm1::findShortRoad(TObject
*Sender)<BR>{<BR> int
aa[8][8];<BR> int step
;<BR> int len ;<BR> int
size;<BR> int x ;<BR> int
y ;<BR> int i ;<BR> int j
;<BR> int dir ;<BR> int
dj[4];<BR> int di[4];<BR>
int routelen ;<BR> int bgin
;<BR> int ed ;<BR> int k
;<BR> int t ;<BR> int num
;<BR> int suc ;<BR> int
cnt ;<BR> int x0 ;<BR> int
y0 ;<BR> int tmp
;<BR>step=20;<BR>len=10;<BR>size=20;<BR>x=0;<BR>y=0;<BR>i=0;<BR>j=0;<BR>routelen=0;<BR>dj[0]=-1;<BR>dj[1]=1;<BR>dj[2]=0;<BR>dj[3]=0;<BR>di[0]=0;<BR>di[1]=0;<BR>di[2]=-1;<BR>di[3]=1;<BR>x=x+len;<BR>y=y+len;<BR>x0=x;<BR>y0=y;<BR>i=0;<BR>j=0;<BR>for
(i=0 ;i<8;i++)<BR>
{<BR> for(j=0
;j<8;j++)<BR>
{<BR>
aa[i][j]=bg[i][j];<BR>
}<BR>}
<BR>aa[0][0]=1;<BR>bgin=0;<BR>ed=0;<BR>road[0].ri=0;<BR>road[0].rj=0;<BR>road[0].pre=-1;<BR>le=0;<BR>suc=0;<BR>while(true)
<BR>{<BR>
cnt=0;<BR> for (k=bgin
;k<=ed;k++)<BR>
{<BR>
for
(t=0;t<4;t++)<BR>
{<BR>
i=road[k].ri+di[t];<BR>
j=road[k].rj+dj[t];<BR>
if((i<8) && (i>=0)
&& (j<8) &&
(j>=0) && (aa[i][j]==0))
<BR>
{<BR>
cnt=cnt+1;<BR>
le=le+1;<BR>
aa[i][j]=le;<BR>
road[ed+cnt].pre=k;<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -