js实现广度优先搜索和深度优先搜索

在学习算法过程中,通过《啊哈!算法》一书,使用JavaScript实现了深度优先搜索(DFS)和广度优先搜索(BFS)。本文将介绍如何运用这两种搜索算法解决炸弹人游戏中找出消灭怪物最多的路径问题,并提供BFS和DFS的代码实现。
摘要由CSDN通过智能技术生成

最近在学习算法,看了图解算法,但是感觉写的太简单,理论比较多,代码实现比较和例子比较少,看完后,又接着看了《啊哈!算法》,这个感觉例子比较多,也比较适合我这种算法入门的读者。

书上的例子一一打了一遍,因为作者是用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
                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值