最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于javascript實(shí)現(xiàn)獲取最短路徑算法代碼實(shí)例

 更新時(shí)間:2020年02月20日 14:13:46   作者:weihexin  
這篇文章主要介紹了基于javascript實(shí)現(xiàn)獲取最短路徑算法代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了基于javascript實(shí)現(xiàn)獲取最短路徑算法代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

代碼如下

//A算法 自動尋路 路徑
class GetAutoPath{

 constructor(id, map, sPos, ePos, mapArr){
  //this.type = id.type;
  this.id = id;
  this.map = map;
  this.sPos = sPos;
  this.ePos = ePos;
  this.mapArr = mapArr;
  this.maxMach = 10000;
  this.openArr = [];
  this.closeArr = [];
  this.minPath = [];
  if(!this.isPath(this.sPos.x, this.sPos.y)){this.sPos = this.getNewDot(sPos, ePos);}
  if(!this.isPath(this.ePos.x, this.ePos.y)){this.ePos = this.getNewDot(ePos, sPos);}
  //console.log(this.mapArr);
  return this.run();
 }
 
 posts(txt, arr){//post消息
  //let id = this.id, sPos = this.sPos, ePos = this.ePos, arrs = arr || [];
  return {id:this.id, map:this.map, arr:arr || [], sPos:this.sPos, ePos:this.ePos, txt:txt}
 }
 
 isPath(x, y){//isPath = true 合法路徑 = isBanPath === undefined
  let isPath = false, ym = this.mapArr.get(y), xm; //console.log(ym); debugger;
  if(ym !== undefined){
   xm = ym.get(x);
   if(xm !== undefined){
    if(xm.isBanPath === undefined){isPath = true;}
   }
  }
  //if(this.mapArr[y] !== undefined && this.mapArr[y][x] !== undefined && this.mapArr[y][x].isPath === 1){isPath = true;}
  return isPath;
 }
 
 getEqual(arr, x, y){//獲取目標(biāo)數(shù)組相同的坐標(biāo)
  let isPos = false;
  if(arr.length === 0){
   isPos = false;
  }else{
   isPos = arr.some(function (o){return o.x === x && o.y === y;});
  }
  return isPos;
 }

 getDot(x, y){//獲取周圍8個(gè)方向坐標(biāo)
  return [{x:x-1,y:y},{x:x+1,y:y},{x:x,y:y-1},{x:x,y:y+1},{x:x-1,y:y-1},{x:x+1,y:y+1},{x:x+1,y:y-1},{x:x-1,y:y+1}]
 } 
 
 getNewDot(setPos, pos){//重定義起點(diǎn)或終點(diǎn)
  let dot = setPos, pointDot, k, arr = [], arrs = [], g, end, maxMachT = 0;
  while(!end && maxMachT < this.maxMach){
   maxMachT++;
   pointDot = this.getDot(dot.x, dot.y);
   for(k in pointDot){
    g = Math.round(Math.sqrt(Math.abs(pointDot[k].x - pos.x) + Math.abs(pointDot[k].y - pos.y)) * 100) / 100;
    if(!this.isPath(pointDot[k].x, pointDot[k].y)){//不合法
     arr.push({x:pointDot[k].x, y:pointDot[k].y, g:g});
     arr.sort(function(a, b){return a.g - b.g;});
    }else{//合法
     arrs.push({x:pointDot[k].x, y:pointDot[k].y, g:g});
     arrs.sort(function(a, b){return a.g - b.g;});
    }
    if(arrs.length > 0){end = true;}
   }
   dot = {x:arr[0].x, y:arr[0].y, g:arr[0].g}; arr = []; 
  }
  if(!arrs[0].x || !arrs[0].y){return this.posts("沒有符合的坐標(biāo)");}
  return {x:arrs[0].x, y:arrs[0].y};
 }
 
 run(){
  if(this.sPos.x === undefined || this.ePos.x === undefined){return this.posts("沒有符合的坐標(biāo)");}
  let sPos = this.sPos, ePos = this.ePos, point, key, i, newPoint, ger, gers, g, h, f, maxMachT = 0;
  this.openArr[0] = {x : sPos.x, y : sPos.y, f : 0, p : 0, ger : 0}
  while(this.openArr.length > 0){
   maxMachT++;
   point = this.openArr[0]; this.closeArr.push(point); this.openArr.splice(0,1);
   key = this.closeArr.length - 1;//設(shè)置當(dāng)前節(jié)點(diǎn)
   newPoint = this.getDot(point.x, point.y);//獲取周圍點(diǎn)
   for(i in newPoint){//設(shè)置周圍點(diǎn)
    ger = Math.round(Math.sqrt(Math.abs(newPoint[i].x - point.x) + Math.abs(newPoint[i].y - point.y)) * 100) / 100;//到當(dāng)前節(jié)點(diǎn)的曼哈頓距離,保留兩位小數(shù)點(diǎn)
    gers = ger + point.ger;
    g = Math.round(gers * 100) / 100;
    h = Math.abs(newPoint[i].x - ePos.x) + Math.abs(newPoint[i].y - ePos.y);
    f = g + h;
    if(this.isPath(newPoint[i].x, newPoint[i].y) && !this.getEqual(this.openArr, newPoint[i].x, newPoint[i].y) && !this.getEqual(this.closeArr, newPoint[i].x, newPoint[i].y)){this.openArr.push({x:newPoint[i].x, y:newPoint[i].y, f:f, p:key, ger:ger});}
   }
   this.openArr.sort(function(a, b){return a.f - b.f;});//排序
   if(this.getEqual(this.closeArr, ePos.x, ePos.y) || this.getEqual(this.openArr, ePos.x, ePos.y)){//end
    this.minPath.unshift(this.closeArr[key]);
    while(this.minPath.length > 0){
     if(this.minPath[0].p == 0){return this.posts('success', this.minPath);}else{this.minPath.unshift(this.closeArr[this.minPath[0].p]);}
    }
   }else if(maxMachT === this.maxMach){
    return this.posts("沒有符合的坐標(biāo)");
   }
  }
  return this.posts("沒有符合的坐標(biāo)");
 }
 
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談Mybatis版本升級踩坑及背后原理分析

    淺談Mybatis版本升級踩坑及背后原理分析

    這篇文章主要介紹了淺談Mybatis版本升級踩坑及背后原理分析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Java利用POI讀取、寫入Excel的方法指南

    Java利用POI讀取、寫入Excel的方法指南

    這篇文章主要給大家介紹了關(guān)于Java利用POI讀取、寫入Excel的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • java 通過cmd 調(diào)用命令啟動tomcat的操作

    java 通過cmd 調(diào)用命令啟動tomcat的操作

    這篇文章主要介紹了java 通過cmd 調(diào)用命令啟動tomcat的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 解決rror updating database.Cause:java.sql.SQLSyntaxErrorException問題

    解決rror updating database.Cause:java.sql.SQLSyntaxE

    這篇文章主要介紹了解決rror updating database.Cause:java.sql.SQLSyntaxErrorException問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java超詳細(xì)分析@Autowired原理

    Java超詳細(xì)分析@Autowired原理

    @Autowired注解可以用在類屬性,構(gòu)造函數(shù),setter方法和函數(shù)參數(shù)上,該注解可以準(zhǔn)確地控制bean在何處如何自動裝配的過程。在默認(rèn)情況下,該注解是類型驅(qū)動的注入
    2022-06-06
  • springboot統(tǒng)一接口返回?cái)?shù)據(jù)的實(shí)現(xiàn)

    springboot統(tǒng)一接口返回?cái)?shù)據(jù)的實(shí)現(xiàn)

    這篇文章主要介紹了springboot統(tǒng)一接口返回?cái)?shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java 中的類和對象詳情

    Java 中的類和對象詳情

    這篇文章主要介紹了Java 中的類和對象,類可以看成是創(chuàng)建Java對象的模板,下面文章圍繞著Java 類與對象詳細(xì)內(nèi)容展開,需要的朋友可以參考一下
    2021-11-11
  • java實(shí)現(xiàn)壓縮字符串和java字符串過濾

    java實(shí)現(xiàn)壓縮字符串和java字符串過濾

    這篇文章主要介紹了java實(shí)現(xiàn)壓縮字符串和java字符串過濾,需要的朋友可以參考下
    2014-04-04
  • java語言描述Redis分布式鎖的正確實(shí)現(xiàn)方式

    java語言描述Redis分布式鎖的正確實(shí)現(xiàn)方式

    這篇文章主要介紹了java語言描述Redis分布式鎖的正確實(shí)現(xiàn)方式,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Java 超詳細(xì)圖解集合框架的數(shù)據(jù)結(jié)構(gòu)

    Java 超詳細(xì)圖解集合框架的數(shù)據(jù)結(jié)構(gòu)

    什么是集合框架呢?集合框架是為表示和操作集合而規(guī)定的一種統(tǒng)一的標(biāo)準(zhǔn)的體系結(jié)構(gòu)。最簡單的集合如數(shù)組、列表和隊(duì)列等,任何集合框架一般包含:對外的接口、接口的實(shí)現(xiàn)和對集合運(yùn)算的算法
    2022-04-04

最新評論

吉水县| 修文县| 廊坊市| 东兴市| 微博| 彭山县| 香格里拉县| 那曲县| 泽库县| 游戏| 贵南县| 广元市| 河南省| 汾阳市| 福安市| 淮安市| 贡嘎县| 嘉定区| 永昌县| 凭祥市| 通辽市| 博白县| 阿荣旗| 杭锦旗| 潼关县| 正定县| 永修县| 旅游| 无为县| 平谷区| 宁蒗| 鹿泉市| 荣昌县| 英山县| 满洲里市| 麻阳| 建水县| 中宁县| 庄河市| 辰溪县| 怀集县|