程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 蟻群算法小程序(C/C++語言實現)(四)

蟻群算法小程序(C/C++語言實現)(四)

編輯:關於C++

ms = MAX_SMELL;
                             break;
                   }
                            if(IsTrace(i,j)) continue;
                            if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
                  }
                    break;
        case DOWN:  for(i=xxx-ANT_EYESHOT;i<=xxx+ANT_EYESHOT;i++)
                        for(j=yyy+1;j<=yyy+ANT_EYESHOT;j++)
                  {
                            if(!JudgeCanGo(i,j)) continue;
                            if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
                               (i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
                   {
                                ms = MAX_SMELL;
                             break;
                   }
                            if(IsTrace(i,j)) continue;
                            if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
                  }
                    break;
        case LEFT:  for(i=xxx-ANT_EYESHOT;i<xxx;i++)
                        for(j=yyy-ANT_EYESHOT;j<=yyy+ANT_EYESHOT;j++)
                  {
                            if(!JudgeCanGo(i,j)) continue;
                            if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
                               (i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
                   {
                                ms = MAX_SMELL;
                             break;
                   }
  if(IsTrace(i,j)) continue;
                            if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
                  }
                    break;
        case RIGHT: for(i=xxx+1;i<=xxx+ANT_EYESHOT;i++)
                        for(j=yyy-ANT_EYESHOT;j<=yyy+ANT_EYESHOT;j++)
                  {
                            if(!JudgeCanGo(i,j)) continue;[Page]
                            if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
                               (i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
                   {
                                ms = MAX_SMELL;
                             break;
                   }
                            if(IsTrace(i,j)) continue;
                            if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
                  }
                    break;
        default:    break;
    }
    return(ms);
}

int IsTrace(int xxx,int yyy)
{
    int i;

    for(i=0;i<TRACE_REMEMBER;i++)
        if(ant[AntNow].tracex[i]==xxx&&ant[AntNow].tracey[i]==yyy) return(1);
    return(0);
}

int MaxLocation(int num1,int num2,int num3)
{
    int maxnum;

    if(num1==0&&num2==0&&num3==0) return(0);

    maxnum = num1;
    if(num2>maxnum) maxnum = num2;
    if(num3>maxnum) maxnum = num3;

    if(maxnum==num1) return(1);
    if(maxnum==num2) return(2);
    if(maxnum==num3) return(3);
}

int CanGo(int xxx,int yyy,int ddir)
/* input: xxx,yyy - location of ant
          ddir - now dir
   output: 0 - forward and left and right can go
           1 - forward can not go
           2 - left can not go
           3 - right can not go
           4 - forward and left can not go
           5 - forward and right can not go
           6 - left and right can not go
           7 - forward and left and right all can not go
*/
{
    int tx,ty,tdir;
    int okf,okl,okr;

    /* forward can go ? */
    tdir = ddir;
    tx = xxx;
    ty = yyy;

switch(tdir)
    {
        case UP:    ty--;
                    break;
        case DOWN:  ty++;
                    break;
        case LEFT:  tx--;
                    break;
        case RIGHT: tx++;
                    break;
        default:    break;
    } /* of switch dir */
    if(JudgeCanGo(tx,ty)) okf = 1;
    else okf = 0;

    /* turn left can go ? */
    tdir = TurnLeft(ddir);
    tx = xxx;
    ty = yyy;
    switch(tdir)
    {
        case UP:    ty--;
                    break;
        case DOWN:  ty++;
                    break;
        case LEFT:  tx--;
                    break;
        case RIGHT: tx++;
                    break;
        default:    break;
    } /* of switch dir */
    if(JudgeCanGo(tx,ty)) okl = 1;
    else okl = 0;

    /* turn right can go ? */
    tdir = TurnRight(ddir);
    tx = xxx;
    ty = yyy;
    switch(tdir)
    {
        case UP:    ty--;
                    break;
        case DOWN:  ty++;
                    break;
        case LEFT:  tx--;
                    break;
        case RIGHT: tx++;
                    break;
        default:    break;
    } /* of switch dir */
    if(JudgeCanGo(tx,ty)) okr = 1;
    else okr = 0;

    if(okf&&okl&&okr) return(0);
    if(!okf&&okl&&okr) return(1);
    if(okf&&!okl&&okr) return(2);
    if(okf&&okl&&!okr) return(3);
    if(!okf&&!okl&&okr) return(4);
    if(!okf&&okl&&!okr) return(5);
    if(okf&&!okl&&!okr) return(6);
    if(!okf&&!okl&&!okr) return(7);
    return(7);[Page]
}

int JudgeCanGo(int xxx,int yyy)
/* input: location to judeg
   output: 0 -- can not go
           1 -- can go
*/
{
    int i,j;

    if(xxx<=0||xxx>MAXX) return(0);
    if(yyy<=0||yyy>MAXY) return(0);
    if(block[xxx][yyy]) return(0);
    return(1);
}

int TurnLeft(int ddir)
{
    switch(ddir)
    {
        case UP:    return(LEFT);
        case DOWN:  return(RIGHT);
        case LEFT:  return(DOWN);
        case RIGHT: return(UP);
        default:    break;
} /* of switch dir */
}

int TurnRight(int ddir)
{
    switch(ddir)
    {
        case UP:    return(RIGHT);
        case DOWN:  return(LEFT);
        case LEFT:  return(UP);
        case RIGHT: return(DOWN);
        default:    break;
    } /* of switch dir */
}

int TurnBack(int ddir)
{
    switch(ddir)
    {
        case UP:    return(DOWN);
        case DOWN:  return(UP);
        case LEFT:  return(RIGHT);
        case RIGHT: return(LEFT);
        default:    break;
    } /* of switch dir */
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved