清除浮动方法之一(在家内部修墙,即内墙法)


<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.box1{

background-color:pink;
}
.pp1{
float: left;
width: 100px;
height: 100px;
background-color:green;
}
.pp2{
float: left;
width: 100px;
height: 100px;
background-color:greenyellow;
}
.cl{
clear: both;

}
</style>
</head>
<body>
<div class="box1">
<p class="pp1"></p>
<p class="pp2"></p>
<divclass="cl"></div>          /*奇特技巧:在家内部修的墙,可以撑出父级的高,还可以清除浮动*/

</div>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了解决这个问题,我们可以将拆除内墙的步骤加入到之前的 A* 搜索算中。具体来说,我们可以在搜索过程中不断更新迷宫矩阵,将拆除的内墙对应的格子标记为1,然后重新计算最短路径。 以下是基于问题3的代码进行修改后的实现: ```python from queue import PriorityQueue def a_star(start, end, maze): def heuristic(node): return abs(node[0] - end[0]) + abs(node[1] - end[1]) def cost(node): return maze[node[0]][node[1]] visited = set() queue = PriorityQueue() queue.put((heuristic(start), start, [start], maze)) while not queue.empty(): _, node, path, maze = queue.get() if node == end: return path if node in visited: continue visited.add(node) for neighbor in [(node[0]-1, node[1]), (node[0]+1, node[1]), (node[0], node[1]-1), (node[0], node[1]+1)]: if neighbor[0] < 0 or neighbor[0] >= len(maze) or neighbor[1] < 0 or neighbor[1] >= len(maze[0]): continue if neighbor in visited: continue if maze[neighbor[0]][neighbor[1]] == 0: continue new_path = path + [neighbor] new_maze = [row[:] for row in maze] # make a copy of the maze if neighbor == (5, 9): new_maze[4][9] = 1 elif neighbor == (9, 5): new_maze[9][4] = 1 queue.put((heuristic(neighbor) + len(new_path) + cost(neighbor), neighbor, new_path, new_maze)) return None ``` 其中,我们将maze作为搜索状态的一部分,在每次扩展节点时更新maze。如果当前节点是J5或者J9,则更新maze并计算新的路径。 在这个问题中,我们可以使用深度优先搜索或者广度优先搜索来枚举拆除内墙的可能性,并计算每种可能性下的最短路径。最后选择最优解即可。 以下是一个使用深度优先搜索解决问题的示例代码(Python实现): ```python import copy def dfs(start, end, maze, steps, visited, min_steps): if start == end: min_steps[0] = min(min_steps[0], steps) return if steps >= min_steps[0]: return for i in range(len(maze)): for j in range(len(maze[0])): if maze[i][j] == 0: new_maze = copy.deepcopy(maze) new_maze[i][j] = 1 if (i, j) == (4, 9) or (i, j) == (9, 4): new_end = (9, 9) if (i, j) == (4, 9) else (4, 4) new_steps = steps + 1 + len(a_star(start, new_end, new_maze)) + len(a_star(new_end, end, new_maze)) else: new_steps = steps + len(a_star(start, end, new_maze)) if new_steps < min_steps[0] and (i, j) not in visited: visited.add((i, j)) dfs(start, end, new_maze, new_steps, visited, min_steps) visited.remove((i, j)) maze = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 1, 1, 1, 0, 1, 1], [1, 1, 1, 1, 1, 1, 0, 1, 1, 1], [1, 0, 1, 1, 1, 1, 1, 1, 0, 1], [1, 1, 1, 0, 1, 1, 1, 0, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 0, 1, 1, 1, 1, 1] ] start = (0, 0) end = (9, 9) visited = set() min_steps = [float('inf')] dfs(start, end, maze, len(a_star(start, end, maze)), visited, min_steps) print("最短时间:", min_steps[0]) ``` 其中,我们使用了一个min_steps列表来记录最短时间,使用一个visited集合来避免重复枚举内墙的可能性。在dfs函数中,我们首先计算没有拆除内墙时的最短时间,然后枚举所有可能的内墙拆除方案,计算新的最短时间,并递归调用dfs函数。如果新的最短时间小于当前的最短时间,则更新min_steps。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值