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

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";
}
alert("停一下!");
}
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 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="尋找路徑" >
</body>
</html>


 

引文來源 最短路徑演算法-廣度優先搜索 JavaScript & VBScript & DHTML 腳本技術討論版 無憂腳本 - 體驗編寫HTML代碼的樂趣 評論這張
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()