求编程高手给个JS迷宫的代码。

JavaScript013

求编程高手给个JS迷宫的代码。,第1张

以下是我作的迷宫:

我作的只有九格的,不过你可以自己改制成任意多格的迷宫.前提是一定要是用我的6个css样式来布局迷宫.否则会出错.核心的javascript我已经测试过了.没有问题

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">

<title>无标题文档</title>

<style>

.rb{

border-style:double

border-width:0px

border-color:#000000

border-left-width:1px

border-top-width:1px

}

.rt{

border-style:double

border-width:0px

border-color:#000000

border-left-width:1px

border-bottom-width:1px

}

.lb{

border-style:double

border-width:0px

border-color:#000000

border-right-width:1px

border-top-width:1px

}

.lt

{

border-style:double

border-width:0px

border-color:#000000

border-right-width:1px

border-bottom-width:1px

}

.lr{

border-style:double

border-width:0px

border-color:#000000

border-top-width:1px

border-bottom-width:1px

}

.tb

{

border-style:double

border-width:0px

border-color:#000000

border-left-width:1px

border-right-width:1px

}

</style>

</head>

<body>

<div id="layer1" style="position:absoluteleft:15pxtop:21pxwidth:27pxheight:29pxz-index:1background-color: #FF0000layer-background-color: #FF0000border: 1px none #000000"></div>

<table width="120" height="120" border="0" cellpadding="0" cellspacing="0" style="border-color:#000000border-width:1pxborder-style:solid">

<tr>

<td class="rb"> </td>

<td class="rb"> </td>

<td class="lb"> </td>

</tr>

<tr>

<td class="tb"> </td>

<td class="rt"> </td>

<td class="lb"> </td>

</tr>

<tr>

<td class="rt"> </td>

<td class="lr"> </td>

<td class="lt"> </td>

</tr>

</table>

<script language="javascript">

//得用css来布局迷宫,迷宫的多少行多少列,只要改下面的数据就可以了.不过表格的单元格一定要是40*40

var col=3//列数

var row=3//行数

var i=0//位置数

var player=document.getElementById("layer1")

var td=document.getElementsByTagName("table")[0].getElementsByTagName("td")

document.onkeydown=function (e)

{

e=window.event||e

if(e.keyCode==37)//向下运动

{

if(i%col!=0)//不在最左边

{

if((td[i].className=="lb"||td[i].className=="lt"||td[i].className=="lr")&&(td[i-1].className=="rb"||td[i-1].className=="rt"||td[i-1].className=="lr"))//可以通过

{

player.style.left=parseInt(player.style.left)-40+"px"

i=i-1

}

}

}//向左运动

else if(e.keyCode==38)//向上运动

{

if(i>=col)//不在最上边

{

if((td[i].className=="lt"||td[i].className=="rt"||td[i].className=="tb")&&(td[i-col].className=="tb"||td[i-col].className=="rb"||td[i-col].className=="lb"))//可以通过

{

player.style.top=parseInt(player.style.top)-40+"px"

i=i-col

}

}

}//向上运动

else if(e.keyCode==39)//向右运动

{

if((i%col)!=(col-1))//不在最右边

{

if((td[i].className=="lr"||td[i].className=="rt"||td[i].className=="rb")&&(td[i+1].className=="lb"||td[i+1].className=="lt"||td[i+1].className=="lr"))//可以通过

{

player.style.left=parseInt(player.style.left)+40+"px"

i=i+1

}

}

}//向右运动

else if(e.keyCode==40)//向下运动

{

if(i<=col*(row-1))//不在最下边

{

if((td[i].className=="lb"||td[i].className=="rb"||td[i].className=="tb")&&(td[i+col].className=="tb"||td[i+col].className=="rt"||td[i+col].className=="lt"))//可以通过

{

player.style.top=parseInt(player.style.top)+40+"px"

i=i+col

}

}

}//向下运动

}

</script>

</body></html>

用python递归求解

dirs=[(0,1),(1,0),(0,-1),(-1,0)] #当前位置四个方向的偏移量

path=[] #存找到的路径

def mark(maze,pos): #给迷宫maze的位置pos标"2"表示“倒过了”

maze[pos[0]][pos[1]]=2

def passable(maze,pos): #检查迷宫maze的位置pos是否可通行

return maze[pos[0]][pos[1]]==0

def find_path(maze,pos,end):

mark(maze,pos)

if pos==end:

print(pos,end=" ") #已到达出口,输出这个位置。成功结束

path.append(pos)

return True

for i in range(4): #否则按四个方向顺序检查

nextp=pos[0]+dirs[i][0],pos[1]+dirs[i][1]

#考虑下一个可能方向

if passable(maze,nextp):#不可行的相邻位置不管

if find_path(maze,nextp,end):#如果从nextp可达出口,输出这个位置,成功结束

print(pos,end=" ")

path.append(pos)

return True

return False

def see_path(maze,path): #使寻找到的路径可视化

for i,p in enumerate(path):

if i==0:

maze[p[0]][p[1]] ="E"

elif i==len(path)-1:

maze[p[0]][p[1]]="S"

else:

maze[p[0]][p[1]] =3

print("\n")

for r in maze:

for c in r:

if c==3:

print('\033[031m'+"*"+" "+'\033[0m',end="")

elif c=="S" or c=="E":

print('\033[034m'+c+" " + '\033[0m', end="")

elif c==2:

print('\033[032m'+"#"+" "+'\033[0m',end="")

elif c==1:

print('\033[040m'+" "*2+'\033[0m',end="")

else:

print(" "*2,end="")

print()

if __name__ == '__main__':

maze=[[1,1,1,1,1,1,1,1,1,1,1,1,1,1],\

[1,0,0,0,1,1,0,0,0,1,0,0,0,1],\

[1,0,1,0,0,0,0,1,0,1,0,1,0,1],\

[1,0,1,0,1,1,1,1,0,1,0,1,0,1],\

[1,0,1,0,0,0,0,0,0,1,1,1,0,1],\

[1,0,1,1,1,1,1,1,1,1,0,0,0,1],\

[1,0,1,0,0,0,0,0,0,0,0,1,0,1],\

[1,0,0,0,1,1,1,0,1,0,1,1,0,1],\

[1,0,1,0,1,0,1,0,1,0,1,0,0,1],\

[1,0,1,0,1,0,1,0,1,1,1,1,0,1],\

[1,0,1,0,0,0,1,0,0,1,0,0,0,1],\

[1,1,1,1,1,1,1,1,1,1,1,1,1,1]]

start=(1,1)

end=(10,12)

find_path(maze,start,end)

see_path(maze,path)

假设8个方位被简单定义为 char a[8]

int path(point *location)

{

if(“location不为出口”&&“location.a[0]未涉足过”)

path(location->a[0])

else if(“location不为出口”&&“location.a[1]未涉足过”)

path(location->a[0])

else if(“location不为出口”&&“location.a[2]未涉足过”)

path(location->a[0])

` ````````````````````

``````````

``````

``

else return 0

}

这是一个迭代过程,需要对每一个方位的位置都遍历一遍,也是一个深度优先的遍历过程。

我在这只给lz一个示意,具体的算法在《数据结构》的书上基本都有,蛮经典的。希望能对lz有所帮组。

加油!