fine_short_path
来源:互联网 发布:淘宝网店怎么装修视频 编辑:程序博客网 时间:2024/06/11 22:41
import copy
def find_short_path(hero_location, dragon_location, fire_locations, wind_locations, jump_locations, map_size):
"""
return: the shortest path or paths, like this:
[{"sequence": [(1,1), (2,2)], "length": 4}]
"""
flag_map = [
[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, 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, 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],
]
x, y = hero_location
flag_map[x][y] = 1
shortest_paths = _find_short_path(hero_location, dragon_location, fire_locations, wind_locations, jump_locations, map_size, flag_map)
return shortest_paths
def get_length(location, wind_locations):
if location in wind_locations:
return 3
else:
return 1
def _find_short_path(hero_location, dragon_location, fire_locations, wind_locations, jump_locations, map_size, flag_map):
"""
return: the shortest path or paths, like this:
[{"sequence": [(1,1), (2,2)], "length": 4}]
"""
shortest_paths = []
next_locations = next_steps(hero_location, dragon_location, fire_locations, wind_locations, jump_locations, flag_map)
if not next_locations:
return False
else:
for location in next_locations:
x, y = location
flag_map[x][y] = 1
if location == dragon_location:
paths = [{"sequence": [location], "length": get_length(location, wind_locations)}]
else:
paths = _find_short_path(location, dragon_location, fire_locations, wind_locations, jump_locations, map_size, flag_map)
flag_map[x][y] = 0
if not paths:
return False
for path in paths:
path["sequence"].insert(0, hero_location)
path["length"] = get_length(hero_location, wind_locations)
if not shortest_paths or path["length"] == shortest_paths[0]["length"]:
shortest_paths.append(path)
elif path["length"] < shortest_paths[0]["length"]:
shortest_paths = [path]
else:
pass
return shortest_paths
def next_steps(location, fire_locations, jump_locations, map_size, flag_map):
"""
return: the possible locations to step on, like this:
[(2,1), (2, 2), (1, 2)]
"""
next_locations = [
(location[0] + 0, location[1] + 1),
(location[0] + 1, location[1] + 0),
(location[0] + 1, location[1] + 1),
(location[0] + 1, location[1] - 1),
(location[0] - 1, location[1] + 1),
(location[0] + 0, location[1] - 1),
(location[0] - 1, location[1] + 0),
(location[0] - 1, location[1] - 1),
]
valid_next_locations = copy.deepcopy(next_locations)
for item in next_locations:
x, y = item
if item[0] < 0 or item[0] >= map_size:
valid_next_locations.pop(item)
elif item[1] < 0 or item[1] >= map_size:
valid_next_locations.pop(item)
elif item in fire_locations:
valid_next_locations.pop(item)
elif item in jump_locations:
valid_next_locations.pop(item)
i, j = jump_locations[item]
if flag_map[i][j] == 0:
valid_next_locations.append(jump_locations[item])
elif flag_map[x][y] == 1:
valid_next_locations.pop(item)
return valid_next_locations
def find_short_path(hero_location, dragon_location, fire_locations, wind_locations, jump_locations, map_size):
"""
return: the shortest path or paths, like this:
[{"sequence": [(1,1), (2,2)], "length": 4}]
"""
flag_map = [
[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, 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, 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],
]
x, y = hero_location
flag_map[x][y] = 1
shortest_paths = _find_short_path(hero_location, dragon_location, fire_locations, wind_locations, jump_locations, map_size, flag_map)
return shortest_paths
def get_length(location, wind_locations):
if location in wind_locations:
return 3
else:
return 1
def _find_short_path(hero_location, dragon_location, fire_locations, wind_locations, jump_locations, map_size, flag_map):
"""
return: the shortest path or paths, like this:
[{"sequence": [(1,1), (2,2)], "length": 4}]
"""
shortest_paths = []
next_locations = next_steps(hero_location, dragon_location, fire_locations, wind_locations, jump_locations, flag_map)
if not next_locations:
return False
else:
for location in next_locations:
x, y = location
flag_map[x][y] = 1
if location == dragon_location:
paths = [{"sequence": [location], "length": get_length(location, wind_locations)}]
else:
paths = _find_short_path(location, dragon_location, fire_locations, wind_locations, jump_locations, map_size, flag_map)
flag_map[x][y] = 0
if not paths:
return False
for path in paths:
path["sequence"].insert(0, hero_location)
path["length"] = get_length(hero_location, wind_locations)
if not shortest_paths or path["length"] == shortest_paths[0]["length"]:
shortest_paths.append(path)
elif path["length"] < shortest_paths[0]["length"]:
shortest_paths = [path]
else:
pass
return shortest_paths
def next_steps(location, fire_locations, jump_locations, map_size, flag_map):
"""
return: the possible locations to step on, like this:
[(2,1), (2, 2), (1, 2)]
"""
next_locations = [
(location[0] + 0, location[1] + 1),
(location[0] + 1, location[1] + 0),
(location[0] + 1, location[1] + 1),
(location[0] + 1, location[1] - 1),
(location[0] - 1, location[1] + 1),
(location[0] + 0, location[1] - 1),
(location[0] - 1, location[1] + 0),
(location[0] - 1, location[1] - 1),
]
valid_next_locations = copy.deepcopy(next_locations)
for item in next_locations:
x, y = item
if item[0] < 0 or item[0] >= map_size:
valid_next_locations.pop(item)
elif item[1] < 0 or item[1] >= map_size:
valid_next_locations.pop(item)
elif item in fire_locations:
valid_next_locations.pop(item)
elif item in jump_locations:
valid_next_locations.pop(item)
i, j = jump_locations[item]
if flag_map[i][j] == 0:
valid_next_locations.append(jump_locations[item])
elif flag_map[x][y] == 1:
valid_next_locations.pop(item)
return valid_next_locations
0 0
- fine_short_path
- jquery mobile 页面缓存 导致事件无法触发
- 完全卸载mysql数据库图文教程
- 三层交换机和路由器的主要区别
- fuseki在windows的简单配置
- HDU Caocao's Bridges
- fine_short_path
- 关于利用live555,RTSP取流
- 94.Binary Tree Inorder Traversal
- MySQL 5.7 Fabric: any good?
- 解决eclipse中org.w3c.dom.Node类老报The method getTextContent() is undefined 问题
- 数据库的设计原则
- 【leetcode】 Maximum Gap
- 黑马程序员--OC-内存
- 变量、初始化块和构造方法的初始化顺序问题(笔试题)