📄 vc62.~cpp
字号:
//---------------------------------------
void __fastcall TForm1::hand(TObject *Sender)
{
Shape1->Brush->Color=(clWhite);
Shape2->Brush->Color=(clWhite);
Mouse_EndA[3]=0;
Mouse_EndA[2]=0;
Mouse_EndA[1]=0;
Mouse_End[3]=0;
Mouse_End[2]=0;
Mouse_End[1]=0;
Step=0;
Mouse_X=0;
Mouse_Y=0;
Timer1->Enabled=false;
}
//--------------------------------------------
void __fastcall TForm1::Auto(TObject *Sender)
{
int i,j,k;
for(i=0;i<64;i++)
{
for(j=0;j<5;j++)
{
for(k=0;k<5;k++)
{
Link[i][j][k]=0;
}
}
}
for(i=0;i<64;i++)
{
for(j=0;j<10;j++)
{
Path[i][j]=0; //清除路线信息
}
}
for(i=0;i<10;i++)
{
Path_long[i]=0;
}
Shape1->Brush->Color=(clWhite);
Shape2->Brush->Color=(clWhite);
Mouse_EndA[3]=0;
Mouse_EndA[2]=0;
Mouse_EndA[1]=0;
Mouse_End[3]=0;
Mouse_End[2]=0;
Mouse_End[1]=0;
Step=0;
Pathp=0;
Mouse_X=0;
Mouse_Y=0;
Mouse_Step=0;
Mouse_Turn=0;
Path_enp=0;
Step=1;
Timer1->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
hand(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Go(TObject *Sender, int dir)
{
if(dir==0)
{
Mouse_Y--;
draw(Sender);
}
else if(dir==1)
{
Mouse_X--;
draw(Sender);
}
else if(dir==2)
{
Mouse_Y++;
draw(Sender);
}
else if(dir==3)
{
Mouse_X++;
draw(Sender);
}
Mouse_dir0=dir;
}
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
int i,j;
int inv_dir;
int dir;
int dir_en=0;
int dir_cou=0;
int tt;
int tmpp=0;
int tmp[4]={0,0,0,0};
int dir_0;
dir_0=Mouse_dir0;
inv_dir=(Mouse_dir0+2)&0x03; //上一次前进方向的反向
for(i=0;i<4;i++)
{
if(Check(i)) //检查各个方向能否走得通
{
dir_en|=(1<<i);
tmp[0]=i;
dir_cou++;
}
}
if(dir_cou==1) //死路
{
dir_cou=0;
dir=tmp[0];
Go(Sender,dir);
}
else
{
if(dir_cou>2) //存在多条路
{
for(i=0;i<Pathp;i++)
{
if((Mouse_X==Path[i][0])&&(Mouse_Y==Path[i][1]))
break;
}
if(i==Pathp) //此点之前无记录
{
Path[Pathp][0]=Mouse_X; //凳记点
Path[Pathp][1]=Mouse_Y;
Path[Pathp][2]=dir_en; //可行方向
Path[Pathp][3]=(1<<inv_dir); //记录已行方向
Path[Pathp][4]=inv_dir; //记录已行方向
Path[Pathp][6]=Mouse_Step; //走了多少步
Path[Pathp][7]=Mouse_Turn; //转了多少弯
Pathp++;
}
else //此点已有记录
{
Mouse_Step=Path[i][6];
Mouse_Turn=Path[i][7]; //恢复这点的步数和转弯数
}
}
dir_en&=(~(1<<inv_dir)); // 去掉之前的路,不走回头路
dir_cou--;
tmpp=0;
for(i=0;i<4;i++)
{
if(dir_en&(1<<i)) //可行
{
tmp[tmpp++]=i;
}
}
if(tmpp>1) //存在多条路
{
for(i=0;i<Pathp;i++)
{ //找出凳记点
if((Mouse_X==Path[i][0])&&(Mouse_Y==Path[i][1]))
{
tt=Path[i][2]-Path[i][3];
if(tt) //走从前未走过的路
{
for(j=0;j<4;j++)
{
if(tt&(1<<j))
{
dir=j;
break;
}
}
}
else //所有路都走过了,走最先进入的路
dir=Path[i][4];
Path[i][3]|=(1<<dir);
Path[i][5]=dir; //正在使用哪条路
break;
}
}
}
else //只有一条路
{
dir=tmp[0];
}
// dir=tmp[Rand(dir_cou)];
Go(Sender,dir);
Mouse_Step++; //步数加一
if(dir_0!=dir) //转弯了吗?
Mouse_Turn++;
Label3->Caption="步数 "+IntToStr(Mouse_Step)+" ";
Label4->Caption="转弯数 "+IntToStr(Mouse_Turn)+" ";
}
for(i=4;i;i--)
{
Mouse_EndA[i]=Mouse_EndA[i-1];
Mouse_End[i]=Mouse_End[i-1];
}
Mouse_EndA[0]=dir_cou; //判断是否为终点
Mouse_End[0]=dir;
if((Mouse_EndA[3]==2)&&(Mouse_EndA[2]==1)&&(Mouse_EndA[1]==1)
&&(Mouse_EndA[0]==1)&&N4R(Mouse_End))
{
// Timer1->Enabled=false;
Shape1->Brush->Color=(clRed);
if(Path_enp<10)
{
for(i=0;i<Pathp;i++) //保存可以到达终点的路径
{
Path_en[Path_enp][i][0]=Path[i][0];
Path_en[Path_enp][i][1]=Path[i][1];
Path_en[Path_enp][i][2]=Path[i][5]; //走哪条路去,走哪条路回来
Path_en[Path_enp][i][3]=Path[i][4];
}
//转一个弯相当于走0。5步
Path_long[Path_enp]=((int)Mouse_Step)*10+((int)Mouse_Turn)*5;
Path_enp++;
}
Mouse_End_X=Mouse_X;
Mouse_End_Y=Mouse_Y; //终点在哪
}
if((!Mouse_X)&&(!Mouse_Y)) //已搜索完所有路并回到起点
{
Step=2; //进入第二步,选择合造的路
//决定用两一条路
int min, minc;
min=Path_long[0];
minc=0; //先设路线0为最短
for(i=1;i<Path_enp;i++) //求出等效步数
{
if(Path_long[i]<min) //出现比路线0还短的
{
min=Path_long[i];
minc=i;
}
}
Chose=minc; //决定用哪一条路
Timer1->Enabled=false; //定时器1停止工作,开定时器2
Timer2->Enabled=true;
Shape2->Brush->Color=(clAqua);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
int i;
int inv_dir;
int dir;
int dir_en;
int dir_cou=0;
inv_dir=Mouse_dir0+1; //上一次前尽方向的反向
if(inv_dir==2)
inv_dir=0;
else if(inv_dir==4)
inv_dir=2;
dir_en=0;
for(i=0;i<4;i++)
{
if(Check(i))
{
dir_en|=(1<<i);
dir_cou++;
}
}
if(dir_cou>2) //存在多条路
{
for(i=0;i<Pathp;i++) //找出存在内部的路
{
if((Path_en[Chose][i][0]==Mouse_X)&&(Path_en[Chose][i][1]==Mouse_Y))
{
if(Step==2)
{
dir=Path_en[Chose][i][2];
}
else if(Step==3)
{
dir=Path_en[Chose][i][3];
}
break;
}
}
}
else
{
if(dir_cou==2) //有2条路
{
dir_en&=(~(1<<inv_dir)); // 去掉之前的路,不走回头路
}
for(i=0;i<4;i++) //找出哪一条路可以走
{
if(dir_en&(1<<i))
{
dir=i;
break;
}
}
}
Go(Sender,dir);
if(Step==3) //返回
{
if((Mouse_X==0)&&(Mouse_Y==0))
{
Step=0;
Shape2->Brush->Color=clRed;
Timer2->Enabled=false; //关定时器
}
}
if((Mouse_X==Mouse_End_X)&&(Mouse_Y==Mouse_End_Y)) //到终点了吗?
{
Step=3; //进入第三步,返回
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N9Click(TObject *Sender)
{
hand(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N10Click(TObject *Sender)
{
Auto(Sender);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -