最短路径算法-广度优先搜索

<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];
  if(!this.gotl()) return null;
  var rearr=[];
  var dd = this.bjw;
  while(this.rv[dd]!="N")
  {
    rearr[rearr.length]=dd.split(":");
    dd=this.rv[dd];
  }
  return rearr;
}

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;
	return true;
      }
      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;
  if(this.zj.length>0)
    return this.gotl();
  else
    return false;
}


function sou()
{
  var k = new ptClass(mstr," ",nnx,nny,ssx,ssy);
  var a = k.getptyl();
  if(a==null)
  {
    alert("道路不通!");
    return;
  }
  for(var ii=0; ii<a.length; ii++)
  {
    var t = a[ii];
    maptt.rows[t[1]].cells[t[0]].style.backgroundColor = "#ffff00";
  }
}


</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="查找路径" οnclick="sou()">

</body>
</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天际的海浪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值