2006/10/18 | Flash地图广度优先搜索
类别(Flash&AS2) | 评论(0) | 阅读(342) | 发表于 23:50

      今天写了个地图的广度优先算法,广度搜索算法还是以前的数据结构课程里学过,似乎从出校门那天起就还给学校了,如今有用到就捡起来看了看,研究了大半天终于把理论与实践结合起来了,下面是我的Flash版的BFS代码:

 private function searchPath(sx, sy, __depth, prex, prey:Number):Void {
  num++;
  //trace("num="+num);
  //trace("------------------------当前点为:"+sx+","+sy);
  closeA.push({sx:sx, sy:sy, d:__depth, prex:prex, prey:prey});
  var dirA:Array = new Array();
  //down
  dirA[0] = new Array(1, 0, 1);
  //right
  dirA[1] = new Array(0, 1, 1);
  //up
  dirA[2] = new Array(-1, 0, 1);
  //left
  dirA[3] = new Array(0, -1, 1);
  //rightup
  /* dirA[4] = new Array(-1, 1, 1);
  //leftup
  dirA[5] = new Array(-1, -1, 1);
  //rightdown
  dirA[6] = new Array(1, -1, 1);
  //rightup
  dirA[7] = new Array(1, 1, 1); */
  mapArray[sx][sy].setIsChecked(true);
  var n:Number = 0;
  while (n<4) {
   var rowID:Number = sx+dirA[n][0];
   var colID:Number = sy+dirA[n][1];
   //trace(" mapArray[rowID][colID]="+mapArray[rowID][colID]);
   //trace("rowID,colID="+rowID+","+colID);
   //trace("colID="+colID);
   //trace(" mapArray[rowID][colID].isChecked="+mapArray[rowID][colID].isChecked);
   //if ((rowID>=0 && rowID<8) && (colID>=0 && colID<8)) {
   if (rowID == endRowID && colID == endColID)
   {
    var depth:Number = __depth+dirA[n][2];
    //closeA.push({sx:rowID, sy:colID, d:depth, prex:sx, prey:sy});
    trace("------------------end Point is :"+endRowID+","+endColID);
    //trace("closeA[i] depth="+closeA[closeA.length-1].d);
    /*mapArray[startRowID][startColID].graphic.gotoAndStop(1);
    startRowID = endRowID;
    startColID = endColID;
    mapArray[startRowID][startColID].graphic.gotoAndStop(2);*/
    return;
   }
   if (false == mapArray[rowID][colID].isChecked)
   {
    /*if (rowID == endRowID && colID == endColID) {
    //trace("endPoint's depth is:"+depth);
    closeA.push({sx:rowID, sy:colID, d:depth});
    isPathFind = true;
    return;
    } else {
    }*/
    mapArray[rowID][colID].setIsChecked(true);
    var depth:Number = __depth+dirA[n][2];
    openA.push({sx:rowID, sy:colID, d:depth, prex:sx, prey:sy});
   }
   //}       
   n++;
  }
  //trace("openA.length="+openA.length);
  if (openA.length == 0)
  {
   return;
  }
  if (openA.length>0)
  {
   //openA.sort(sortFval);
   //trace("数组中取下一个节点");
   var tempArray:Object = openA.shift();
   //searchPath(rowID, colID, ex, ey);
   searchPath(tempArray.sx, tempArray.sy, tempArray.d, tempArray.prex, tempArray.prey);
  }
 }

private function searchPreNode(px, py:Number) {
  //trace("~~~~~~~~~~~`x,y="+px+","+py);
  nodeArray.push({x:px, y:py});
  if (px == startRowID && py == startColID)
  {
   //trace("找到了根节点");
   return;
  }
  for (var i:Number = 0; i<closeA.length; i++)
  {
   //trace("x,y="+closeA[i].sx+","+closeA[i].sy);
   //trace("depth="+closeA[i].d);
   //trace("prex,prey="+closeA[i].prex+","+closeA[i].prey);
   //trace("prey="+closeA[i].prey);
   //trace("---------------------");
   if (closeA[i].sx == px && closeA[i].sy == py)
   {
    searchPreNode(closeA[i].prex, closeA[i].prey);
    return;
   }
  }
 }

欢迎交流!

0

评论Comments

日志分类
首页[116]
Flash&AS2[56]
FMS[3]
Flex&AS3[5]
Asp[5]
My Works[13]
My Life[34]