⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 最短路径.htm

📁 javascript实现的最短路径搜索.
💻 HTM
字号:
<html>
<head>
<title> New Document </title>
</head>

<body>
<script language="JavaScript">
var mstr = [
"************************************",
"*                                  *",
"*            S                     *",
"*                                  *",
"*                                  *",
"*                                  *",
"*                                  *",
"*            ***********           *",
"*            *         *           *",
"*          ***         *           *",
"*          *           *           *",
"*          *                       *",
"*         **                       *",
"*                N                 *",
"*                                  *",
"*                                  *",
"*          *******************     *",
"*                                  *",
"*                                  *",
"*    **********                    *",
"*                                  *",
"*                                  *",
"*                                  *",
"************************************"
];
for(j=0;j<mstr.length;j++)
  mstr[j]=mstr[j].split("");

var nnx,nny,ssx,ssy;


function ptClass(marr,pg,nx,ny,sx,sy)
{
  this.marr=marr;
  this.pg=pg;
  this.nx=nx;
  this.ny=ny;
  this.sx=sx;
  this.sy=sy;
  this.bjw;
  this.rv={};
  this.zj=[];
  this.zjb=[];
}
ptClass.prototype.TX = [ 1, 0,-1, 0];
ptClass.prototype.TY = [ 0, 1, 0,-1];

ptClass.prototype.getptyl = function ()
{
  this.rv[this.nx+":"+this.ny]="N";
  this.zj[0]=[this.nx,this.ny];
  this.gotl();
}

ptClass.prototype.dis = function (p)
{
  if(!p){alert("道路不通!"); return;}
  var rearr=[];
  var dd = this.bjw;
  while(this.rv[dd]!="N")
  {
    rearr[rearr.length]=dd.split(":");
    dd=this.rv[dd];
  }

  for(var ii=0; ii<rearr.length; ii++)
  {
    var t = rearr[ii];
    maptt.rows[t[1]].cells[t[0]].style.backgroundColor = "#ffff00";
  }

}

ptClass.prototype.gotl = function ()
{
  this.zjb=[];
  for(var ii=0; ii<this.zj.length; ii++)
  {
    var fr = this.zj[ii];
    var vs = fr[0]+":"+fr[1];
    for(var p=0; p<4; p++)
    {
      var x = fr[0]+this.TX[p];
      var y = fr[1]+this.TY[p];
      if(x==this.sx && y==this.sy)
      {
	this.bjw = vs;
	this.dis(true);
	return;
      }
      var zb = x+":"+y;
      if(this.marr[y][x]==this.pg && !this.rv[zb])
      {
        this.rv[zb] = vs;
        this.zjb[this.zjb.length] = [x,y];
      }
    }
  }
  this.zj=this.zjb;
                         this.yan();
  if(this.zj.length>0)
     setTimeout("kk.gotl();",300);
  else
  {
    this.dis(false);
    return;
  }
}


ptClass.prototype.yan = function ()
{
  for(var ii=0; ii<this.zj.length; ii++)
  {
    var t = this.zj[ii];
    maptt.rows[t[1]].cells[t[0]].style.backgroundColor = "#CC66FF";
  }
}

var kk;
function sou()
{
  kk = new ptClass(mstr," ",nnx,nny,ssx,ssy);
  kk.getptyl();
}


</script>

<table id="maptt" cellspacing="1" cellpadding="0" border="0" bgcolor="#000000">

<script>
var ntw = mstr[0].length;
var nth = mstr.length;
for(j=0;j<nth;j++)
{
  document.write("<tr>");
  for(i=0;i<ntw;i++)
  {
    var sn = "#ffffff";
    switch(mstr[j][i])
    {
     case "*": sn = "#cccccc";
      break;
     case "N": sn = "#0000ff";
      nnx=i; nny=j;
      break;
     case "S": sn = "#ff0000";
      ssx=i; ssy=j;
      break;
    } 
    document.write('<td style="background-color:'+sn+'" width="20" height="20"></td>');
  }
  document.write("</tr>");
}
</script>
</table>
<input type="button" value="查找路径" onclick="sou();this.disabled=true">

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -