`

广度优先搜索算法

阅读更多
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>8方向双向广搜</title>
</head>

<body>
<script type="text/javascript">
var tb = [], o, r, c, n=0, newHash;
var dir=[ [-1,0], [1,0], [0,-1], [0,1], [-1,-1], [1,1], [1,-1], [-1,1] ];
function sou(hash){
	newHash = {};
	for(var i in hash){
		for(var j=0; j<8; j++){
			r = hash[i].row + dir[j][0];
			c = hash[i].col + dir[j][1];
			if(r<0||c<0||r==rows||c==cols||tb[r][c].tp==1||tb[r][c].tp==hash[i].tp) continue;
			o = tb[r][c];
			if(o.tp!=0) {alert(new Date()-time);return show(o)(hash[i]);}
			o.parent = hash[i];
			o.tp = o.parent.tp;
			//o.obj.style.backgroundColor = "#CC66FF";
			newHash[r+"."+c] = o;
		}
	}
	//alert("第"+(++n)+"步");
	sou(newHash);
}
function show(o){
	if(!o.parent) return show;
	o.obj.style.backgroundColor = "yellow";
	return show(o.parent);
}

var mapArr = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0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,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
];

var rows = mapArr.length+2;
var cols = mapArr[0].length+2;
var hash={};
for(r=0; r<rows; r++){
	tb[r] = [];
	for(c=0; c<cols; c++){
		o = {row:r, col:c};
		o.tp = r>0&&c>0&&r<rows-1&&c<cols-1 ? mapArr[r-1][c-1] : 1;
		var bg = "blue";
		switch(o.tp){
			case 1: bg = "red"; break;
			case 2: bg = "green"; hash[r+"."+c]=o; break;
			case 3: bg = "black"; hash[r+"."+c]=o; break;
		}
		o.obj = document.createElement("img");
		o.obj.style.cssText = "position:absolute; left:"+c*10+"px; top:"+r*10+"px; background-color:"+bg+"; width:10px; height:10px;";
		this.tb[r][c] = o;
		document.body.appendChild(o.obj);
	}
}
document.write('<input style="position:absolute;top:'+(rows*10+20)+'px" type="button" value="查找路径" onclick="time=new Date();sou(hash)" />')
</script>
</body>
</html>

 

http://bbs.51js.com/viewthread.php?tid=86484&extra=page%3D3

分享到:
评论

相关推荐

    宽度优先搜索 算法.doc

    采用宽度优先搜索算法,编程实现八数码问题的求解。初始状态和目标状态可自定;采用宽度优先搜索算法,编程实现八数码问题的求解。初始状态和目标状态可自定采用宽度优先搜索算法,编程实现八数码问题的求解。初始...

    代码 基于BFS广度优先搜索算法代码

    代码 基于BFS广度优先搜索算法代码代码 基于BFS广度优先搜索算法代码代码 基于BFS广度优先搜索算法代码代码 基于BFS广度优先搜索算法代码代码 基于BFS广度优先搜索算法代码代码 基于BFS广度优先搜索算法代码代码 ...

    MATLAB源码集锦-基于BFS广度优先搜索算法代码

    本资源“MATLAB源码集锦-基于BFS广度优先搜索算法代码”是专门为MATLAB用户提供的,旨在帮助他们理解和实现BFS算法。 BFS算法的基本思想是从起点开始,首先访问其所有邻居,然后访问这些邻居的邻居,以此类推,直到...

    深度优先搜索算法和广度优先搜索算法

    深度优先搜索算法和广度优先搜索算法 深度优先搜索算法(DFS)是一种常用的图遍历算法,它通过递归地访问图中的每个顶点来实现图的遍历。DFS 算法的基本思想是,从图中的一个顶点出发,沿着边访问图中的其它顶点,...

    深度优先、广度优先搜索算法C语言版

    数据结构课程中的深度优先搜索算法、广度优先搜索算法的C语言程序,在Turbo C 2.0上调试通过。

    数据结构深度、广度优先搜索算法C语言版

    数据结构课程中的深度优先搜索算法、广度优先搜索算法的C语言程序,在Turbo C 2.0上调试通过。

    搜索算法_广度优先搜索算法判断图的连通性_matlab

    资源名:搜索算法_广度优先搜索算法判断图的连通性_matlab 资源类型:matlab项目全套源码 源码说明: 全部项目源码都是经过测试校正后百分百成功运行的,如果您下载后不能运行可联系我进行指导或者更换。 适合人群:...

    【三维路径规划】基于matlab广度优先搜索算法无人机三维路径规划【含Matlab源码 270期】.zip

    接下来,我们将深入探讨广度优先搜索算法以及如何在三维空间中应用它来规划无人机的飞行路径。 首先,广度优先搜索是用于遍历或搜索树或图的一种算法。它的基本思想是从起始节点开始,沿着树的宽度优先遍历,即先...

    广度优先搜索算法判断图的连通性.doc

    广度优先搜索算法判断图的连通性 在计算机科学和信息科学中,图论是一个重要的研究方向。图是一种非线性数据结构,用于描述事物之间的关系。在图论中,判断图的连通性是一个重要的问题,即判断图中的所有节点是否...

    易语言宽度优先搜索算法

    在易语言中实现宽度优先搜索算法,我们可以利用队列数据结构来辅助完成。 易语言宽度优先搜索算法的核心在于队列的操作,包括初始化(queue_init)、判断队列是否为空(queue_empty)、入队(queue_push)、获取队...

    人工智能实验 宽度优先搜索

    在此实验中,我们将使用C++语言实现宽度优先搜索算法,并应用于解决八数码问题。 宽度优先搜索算法的基本思想是,从图的起始节点开始,逐层遍历图中的节点,直到找到目标节点。宽度优先搜索算法的优点是,它可以...

    宽度优先算法解决八数码问题实验报告.zip

    **宽度优先搜索算法(Breadth-First Search, BFS)** 宽度优先搜索是图论中的一个经典算法,常用于寻找图中的最短路径或者解决树的层次遍历问题。在这个实验报告中,它被应用于解决著名的八数码问题,也称为滑动...

    实现深度优先搜索与广度优先搜索算法.doc

    深度优先搜索与广度优先搜索算法 深度优先搜索(Depth-First Search,DFS)和广度优先搜索(Breadth-First Search,BFS)是图论中两种常用的图遍历算法。它们都是从图的某个节点出发,遍历图中的所有节点,并输出...

    广度优先搜索算法.doc

    "广度优先搜索算法" 广度优先搜索算法(Breadth-First-Search,BFS)是一种常用的搜索算法,它用于解决问题中找出符合约束条件的目标结点或找出一条从初始结点到达目标结点的路径。该算法的主要特点是:有一组具体...

    用C语言实现八数码问题的宽度优先搜索

    宽度优先搜索算法的思想是从初始状态开始,逐步生成新的状态,并将其存储在队列中,然后从队列中取出状态,继续生成新的状态,直到达到目标状态。 在这个程序中,我们首先定义了一些宏,例如TIME,用于限定搜索的步...

    c#广度优先算法做的迷宫

    **广度优先搜索算法** 广度优先搜索是一种用于遍历或搜索树或图的算法。它从根节点开始,先访问所有相邻的节点,然后再遍历这些相邻节点的相邻节点,直到找到目标节点或者遍历完所有节点。在迷宫问题中,每个节点...

    图的深度和广度优先搜索算法程序框架.doc

    图的深度和广度优先搜索算法程序框架 图的深度和广度优先搜索算法是图论中最基本也是最重要的两个算法,它们广泛应用于计算机科学、数据结构、算法设计等领域。本文将详细介绍图的深度和广度优先搜索算法的实现,...

    人工智能的广度优先搜索

    本文将深入探讨“人工智能的广度优先搜索”这一重要知识点,以及它与全局和局部优先算法的关系。 **一、广度优先搜索(Breadth-First Search, BFS)** 广度优先搜索是一种在图或树结构中进行遍历的方法,其基本...

    宽度优先搜索算法BFS 解决8数码问题

    基于C++的 BFS算法解决8数码问题 没有做界面 直接是输出步骤 算法是亮点

    c/c++语言实现的广度优先搜索算法

    c语言实现的广度优先搜索算法,BFS,经典的

Global site tag (gtag.js) - Google Analytics