当前位置:编程学习 > C/C++ >>

狼追兔子问题C模拟解

说,一只兔子在O点处,它的洞易做图在正北20m的B点处,一只狼位于兔子正东33米的A点处,模拟如下追逐问题:狼以一倍于兔子的速度紧盯兔子追击,问兔子到达洞口前是否会被狼逮住?

网上很多人说能被追上,包括类似的一题距离兔子100码,洞在60码处的。那么究竟能不能呢?

通过计算机模拟,不用解算微分方程,就可以找到答案。

计算机要模拟,基本上就是用第推算法,给出初始条件,按公式计算下一步,如此往复直到一定条件终止计算。

模拟追逐问题,就把时间切片来看,这一步是什么样下一步又是如何,这里把速度可以转换为步进距离来看。

假设平面坐标系上,兔子位于原点O,正北为y轴正向,狼从I象限追进。

 

[cpp]  #include <stdio.h>  
#include <math.h>  
 
double Td,Tx,Ty,Ld,Lx,Ly; 
 
double dist() 

    return sqrt((Tx-Lx)*(Tx-Lx)+(Ty-Ly)*(Ty-Ly)); 

 
//狼位置变化,指向兔子的线段上跑动Ld距离,相应ldx,ldy分量  
double NextL() 

    double dx=fabs(Tx-Lx); 
    double dy=fabs(Ty-Ly); 
    double k,dk; 
    int usek=1; 
    double ldx; 
    double ldy; 
    //  
    if(dx > 0.00000000001 ) 
    { 
        k=dy/dx; 
    }else 
    { 
        usek=0; 
        dk=dx/dy; 
    } 
    //  
    if(usek==1) 
    { 
        ldx=sqrt((Ld*Ld)/(k*k+1)); 
        ldy=k*ldx; 
    }else 
    { 
        ldy=sqrt((Ld*Ld)/(dk*dk+1)); 
        ldx=dk*ldy; 
    } 
    //仅限I象限非震荡追进  
    Lx=Lx-ldx; 
    Ly=Ly+ldy; 

 
//兔子位置变化  
double NextT() 

    Tx=Tx; 
    Ty=Ty+Td; 

 
void loop() 

//坐标建立,初始化  
 Tx=0.0; 
 Ty=0.0; 
 Lx=33.0; 
 Ly=0.0; 
//兔子步进距离  
 Td=0.1; 
//狼步进距离  
 Ld=Td*2.0; 
    while( dist() >= Td ) 
    { 
        NextL(); 
        NextT(); 
        printf("兔子:(%lf,%lf) 狼:(%lf,%lf)\n",Tx,Ty,Lx,Ly); 
    } 
    //  

 
int main(int argc,char** argv) 

    loop(); 
    return 0; 

#include <stdio.h>
#include <math.h>

double Td,Tx,Ty,Ld,Lx,Ly;

double dist()
{
 return sqrt((Tx-Lx)*(Tx-Lx)+(Ty-Ly)*(Ty-Ly));
}

//狼位置变化,指向兔子的线段上跑动Ld距离,相应ldx,ldy分量
double NextL()
{
 double dx=fabs(Tx-Lx);
 double dy=fabs(Ty-Ly);
 double k,dk;
 int usek=1;
 double ldx;
 double ldy;
 //
 if(dx > 0.00000000001 )
 {
  k=dy/dx;
 }else
 {
  usek=0;
  dk=dx/dy;
 }
 //
 if(usek==1)
 {
  ldx=sqrt((Ld*Ld)/(k*k+1));
  ldy=k*ldx;
 }else
 {
  ldy=sqrt((Ld*Ld)/(dk*dk+1));
  ldx=dk*ldy;
 }
 //仅限I象限非震荡追进
 Lx=Lx-ldx;
 Ly=Ly+ldy;
}

//兔子位置变化
double NextT()
{
 Tx=Tx;
 Ty=Ty+Td;
}

void loop()
{
//坐标建立,初始化
 Tx=0.0;
 Ty=0.0;
 Lx=33.0;
 Ly=0.0;
//兔子步进距离
 Td=0.1;
//狼步进距离
 Ld=Td*2.0;
 while( dist() >= Td )
 {
  NextL();
  NextT();
  printf("兔子:(%lf,%lf) 狼:(%lf,%lf)\n",Tx,Ty,Lx,Ly);
 }
 //
}

int main(int argc,char** argv)
{
 loop();
 return 0;
}


这里,NextT给出了兔子的位置第推公式,NextL给出了狼的位置第推公式,在循环时因为狼是根据兔子的位子来做下一步推算的所以要先于兔子位置变换来做移动(反过来的话也可,就是说狼还能稍稍预测兔子的动向,如果狼能预测判断兔子路径完全跑直线可追上兔子无疑,然而事实一般都是紧追不舍的多。)


运行结果

兔子:(0.000000,0.100000) 狼:(32.800000,0.000000)
兔子:(0.000000,0.200000) 狼:(32.600001,0.000610)
兔子:(0.000000,0.300000) 狼:(32.400005,0.001833)
兔子:(0.000000,0.400000) 狼:(32.200013,0.003673)
兔子:(0.000000,0.500000) 狼:(32.000028,0.006135)
兔子:(0.000000,0.600000) 狼:(31.800052,0.009221)
兔子:(0.000000,0.700000) 狼:(31.600087,0.012936)
兔子:(0.000000,0.800000) 狼:(31.400134,0.017284)
兔子:(0.000000,0.900000) 狼:(31.200196,0.022267)
兔子:(0.000000,1.000000) 狼:(31.000275,0.027892)
兔子:(0.000000,1.100000) 狼:(30.800373,0.034160)
兔子:(0.000000,1.200000) 狼:(30.600493,0.041077)
兔子:(0.000000,1.300000) 狼:(30.400636,0.048646)
兔子:(0.000000,1.400000) 狼:(30.200805,0.056872)
兔子:(0.000000,1.500000) 狼:(30.001003,0.065757)
兔子:(0.000000,1.600000) 狼:(29.801231,0.075308)
兔子:(0.000000,1.700000) 狼:(29.601492,0.085527)
兔子:(0.000000,1.800000) 狼:(29.401789,0.096419)
兔子:(0.000000,1.900000) 狼:(29.202124,0.107988)
兔子:(0.000000,2.000000) 狼:(29.002500,0.120238)
兔子:(0.000000,2.100000) 狼:(28.802918,0.133173)
兔子:(0.000000,2.200000) 狼:(28.603383,0.146799)
兔子:(0.000000,2.300000) 狼:(28.403896,0.161118)
兔子:(0.000000,2.400000) 狼:(28.204461,0.176136)
兔子:(0.000000,2.500000) 狼:(28.005080,0.191857)
兔子:(0.000000,2.600000) 狼:(27.805756,0.208285)
兔子:(0.000000,2.700000) 狼:(27.606491,0.225425)
兔子:(0.000000,2.800000) 狼:(27.407290,0.243281)
兔子:(0.000000,2.900000) 狼:(27.208155,0.261857)
兔子:(0.000000,3.000000) 狼:(27.009088,0.281159)
兔子:(0.000000,3.100000) 狼:(26.810094,0.301191)
兔子:(0.000000,3.200000) 狼:(26.611175,0.321957)
兔子:(0.000000,3.300000) 狼:(26.412334,0.343462)
兔子:(0.000000,3.400000) 狼:(26.213576,0.365710)
兔子:(0.000000,3.500000) 狼:(26.014902,0.388707)
兔子:(0.000000,3.600000) 狼:(25.816317,0.412457)
兔子:(0.000000,3.700000) 狼:(25.617825,0.436965)
兔子:(0.000000,3.800000) 狼:(25.419428,0.462236)
兔子:(0.000000,3.900000) 狼:(25.221130,0.488274)
兔子:(0.000000,4.000000) 狼:(25.022935,0.515084)
兔子:(0.000000,4.100000) 狼:(24.824847,0.542671)
兔子:(0.000000,4.200000) 狼:(24.626869,0.571041)
兔子:(0.000000,4.300000) 狼:(24.429006,0.600198)
兔子:(0.000000,4.400000) 狼:(24.231261,0.630146)
兔子:(0.000000,4.500000) 狼:(24.033638,0.660892)
兔子:(0.000000,4.600000) 狼:(23.836142,0.692440)
兔子:(0.000000,4.700000) 狼:(23.6387

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,