最近在学习算法,看了图解算法,但是感觉写的太简单,理论比较多,代码实现比较和例子比较少,看完后,又接着看了《啊哈!算法》,这个感觉例子比较多,也比较适合我这种算法入门的读者。
书上的例子一一打了一遍,因为作者是用c写的,而我是没接触过c,用了自己比较熟悉的js把例子打了一遍。用广搜写了贪吃蛇自动吃果实,只能吃100多个,自认为还能完善,于是等完善后在放在博客上面吧。
这里先写写用dfs(深搜) 和 bfs(广搜)算法实现 炸弹人的统计哪个点能消灭的怪物最多的算法
这里是地图,要求从3,3位置走,在那个地方消灭的怪物最多
这里是用bfs 完成目标的算法 ,基本上都有注释
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>炸弹人</title>
</head>
<body>
<script>
function note(x,y){
this.x = x;
this.y = y;
}
var a = [
["#","#","#","#","#","#","#","#","#","#","#","#","#"],//1
["#","G","G",".","G","G","G","#","G","G","G",".","#"],//2
["#","#","#",".","#","G","#","G","#","G","#","G","#"],//3
["#",".",".",".",".",".",".",".","#",".",".","G","#"],//4
["#","G","#",".","#","#","#",".","#","G","#","G","#"],//5
["#","G","G",".","G","G","G",".","#",".","G","G","#"],//6
["#","G","#",".","#","G","#",".","#",".","#",".","#"],//7
["#","#","G",".",".",".","G",".",".",".",".",".","#"],//8
["#","G","#",".","#","G","#","#","#",".","#","G","#"],//9