WEB开发网
开发学院软件开发VC 写个过河算法 阅读

写个过河算法

 2007-03-15 21:55:34 来源:WEB开发网   
核心提示: CArray<InWhere, InWhere> m_wheres;变量记录所有的可能的结点,开始加入了,写个过河算法(2),省去些界面代码:InWhere where;//临时结点int i,j,k,l,m,n,o,p,q;for(i=1; i<4; i++)for(j

CArray<InWhere, InWhere> m_wheres;变量记录所有的可能的结点。开始加入了,省去些界面代码:InWhere where;  //临时结点
int i,j,k,l,m,n,o,p,q;
for(i=1; i<4; i++)
  for(j=1; j<4; j++)
    for(k=1; k<4; k++)
      for(l=1; l<4; l++)
        for(m=1; m<4; m++)
          for(n=1; n<4; n++)
            for(o=1; o<4; o++)
              for(p=1; p<4; p++)
                for(q = 1; q<4; q++)
                {
                  where.daughter1 = i;
                  where.daughter2 = j;
                  where.father = k;
                  where.mother = l;
                  where.plice = m;
                  where.shife = n;
                  where.son1 = o;
                  where.son2 = p;
                  where.boat = q;
                  if(where.Test())
                  {
                    m_wheres.Add(where);
                    ++AccordNum;
                    ++nItem;
                  }
  }  
最重要的是InWhere::Test()函数,如下:BOOL InWhere::Test()
{
  //测试符合场景状况的结点状态
  //必须要有个会驾船的和船在一边
  if(father !=boat && mother !=boat && plice !=boat)
    return FALSE;
  //如果有人在船上那么船必须为2
  //船上最多有2个人而且必须有个m_bCanDriver = TRUE的
  int i = 0;
  if(daughter1 == 2) i++;
  if(daughter2 == 2) i++;
  if(son1 == 2) i++;
  if(son2 == 2) i++;
  if(father == 2) i++;
  if(mother == 2) i++;
  if(plice == 2) i++;
  if(shife == 2) i++;
  if(i>2) return FALSE;
  if(i != 0 && boat != 2)
  return FALSE;
  //警察和小偷不在一起时候,小偷会伤害家人
  if(shife != plice)
  {
    if(daughter1 == shife || daughter2 == shife ||
      son1 == shife || son2 == shife ||
      father == shife || mother == shife)
    return FALSE;
  }
  //当爸爸妈妈不在一起时,妈妈骂儿子,爸爸骂女儿
  if(mother != father)
  {
    if(daughter1 == father || daughter2 == father ||
      son1 == mother || son2 == mother)
    return FALSE;
  }
  return TRUE;
}  
   TRUE表示这样的结点符合情况,好了结点加完了。都在m_wheres里面了现在开始找道路,就是各个结点间的连线。继续面向对象。移动类,同样如下:class Move 
{
public:
  BOOL Test(CArray<InWhere, InWhere>& p_Ain);//测试能否走的通的函数
  int m_nNext; //下次到达的结点编号
  int m_nNow; //记录目前所在的结点编号
  Move();
  virtual ~Move();
};
一如下
for(i = 0; i< m_wheres.GetSize(); i++)
  for(j = 0; j< m_wheres.GetSize(); j++)
  {
    mov.m_nNow = i;
    mov.m_nNext = j;
    if(i != j && mov.Test(m_wheres))
    {
      m_MoveAll.Add(mov);
    }
  }
去掉界面代码主要代码如上,最重要还是Move::Test函数BOOL Move::Test(CArray<InWhere, InWhere>& p_Ain)
{
  //测试某2点是否能够通过
  InWhere a = p_Ain.GetAt(m_nNow);
  InWhere b = a;
  InWhere c = p_Ain.GetAt(m_nNext);
  InWhere d = c;
  
  int num(0);
  //如果是开始在船上的话
  if(a.boat == 2)
  {
    ++num;
  }
  if(c.boat == 2)
  {
    ++num;
  }
  //头和尾只能有且有一个在船上
  if(num != 1)
    return FALSE;
  //只要在船上的状态等于岸边的状态就表示此路是行的通的
  //从船上到岸上的
  if(a.boat == 2)
  {
    //---*--->*
    if(a.daughter1 == 2)
      a.daughter1 = 3;
    if(a.daughter2 == 2)
      a.daughter2 = 3;
    if(a.father == 2)
      a.father = 3;
    if(a.mother == 2)
      a.mother = 3;
    if(a.plice == 2)
      a.plice = 3;
    if(a.shife == 2)
      a.shife = 3;
    if(a.son1 == 2)
      a.son1 = 3;
    if(a.son2 == 2)
      a.son2 = 3;
    a.boat =3;
    if(a == p_Ain.GetAt(m_nNext))
    return TRUE;
    //*<----*-----
    if(b.daughter1 == 2)
      b.daughter1 = 1;
    if(b.daughter2 == 2)
      b.daughter2 = 1;
    if(b.father == 2)
      b.father = 1;
    if(b.mother == 2)
      b.mother = 1;
    if(b.plice == 2)
      b.plice = 1;
    if(b.shife == 2)
      b.shife = 1;
    if(b.son1 == 2)
      b.son1 = 1;
    if(b.son2 == 2)
      b.son2 = 1;
    b.boat =1;
    if(b == p_Ain.GetAt(m_nNext))
    return TRUE;
    
  }
  //从岸到船上
  if(c.boat == 2)
  {
    //----*<---*
    if(c.daughter1 == 2)
      c.daughter1 = 3;
    if(c.daughter2 == 2)
      c.daughter2 = 3;
    if(c.father == 2)
      c.father = 3;
    if(c.mother == 2)
      c.mother = 3;
    if(c.plice == 2)
      c.plice = 3;
    if(c.shife == 2)
      c.shife = 3;
    if(c.son1 == 2)
      c.son1 = 3;
    if(c.son2 == 2)
      c.son2 = 3;
    c.boat =3;
    if(c == p_Ain.GetAt(m_nNow))
    return TRUE;
    //*---->*----
    if(d.daughter1 == 2)
      d.daughter1 = 1;
    if(d.daughter2 == 2)
      d.daughter2 = 1;
    if(d.father == 2)
      d.father = 1;
    if(d.mother == 2)
      d.mother = 1;
    if(d.plice == 2)
      d.plice = 1;
    if(d.shife == 2)
      d.shife = 1;
    if(d.son1 == 2)
      d.son1 = 1;
    if(d.son2 == 2)
      d.son2 = 1;
    d.boat =1;
    if(d == p_Ain.GetAt(m_nNow))
    return TRUE;
  }
  return FALSE;  
}
结束后,好了,一个整图的样子就展现在我们面前了:

Tags:过河 算法

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接