Last active
November 1, 2023 07:00
-
-
Save cxin-william/0d87a374149fc347c10fe8748ebf2725 to your computer and use it in GitHub Desktop.
帮助我的机器人走出迷宫
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#############################################################LV2############################################################ | |
''' | |
看来你们已经基本掌握了条件语句和循环语句的基本用法了 | |
利用今天所学完成最终测试吧 | |
程序约精炼越好哦😀 | |
得分越高越好哦😁 | |
跳转到153行,开始吧😎!!! | |
''' | |
import random | |
import sys | |
#######################################!!!!!中间这段别改哦!!!!!去145行哦!!!############################################################################## | |
import time # 导入时间模块 | |
#################################### | |
x = 1 # 横向坐标默认值 | |
y = 1 # 纵向坐标默认值 | |
step_number = 0 # 记录行走步数的变量 | |
star_number = 0 # 记录吃糖豆得分 | |
start = time.time() # 获取开始时的时间戳 | |
####################################13 21 4 | |
map_list = [ | |
[" ", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", | |
"@", "@", "@", "@", "@", "@", "@", "@"], | |
["=>", "Y", " ", " ", "@", "@", " ", " ", " ", " ", " ", " ", "@", " ", " ", "机器人创新协会", " ", "@"], | |
[" ", "@", " ", " ", "@", "@", "@", "@", " ", " ", "@", " ", "@", " ", " ", " ", "第一次培训课", " ", " ", "@"], | |
[" ", "@", "O", " ", " ", " ", " ", "@", " ", "@", "@", " ", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", | |
"@", "@", "@", "@", "@", "@", "@", "@"], | |
[" ", "@", "@", "@", "@", "@", " ", "@", " ", "@", "@", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", | |
" ", " ", "@", "@", "@", "@", "@", "@"], | |
[" ", "@", " ", " ", " ", "@", " ", "@", " ", "@", "@", "@", "@", " ", " ", " ", " ", " ", " ", " ", " ", " ", "@", | |
" ", " ", "@", " ", " ", " ", "O", "@"], | |
[" ", "@", " ", "@", " ", "@", " ", "@", " ", " ", "@", "@", "@", " ", " ", " ", " ", " ", " ", " ", " ", " ", "@", | |
" ", " ", "@", " ", "@", "@", "@", "@"], | |
[" ", "@", " ", "@", " ", " ", " ", "@", " ", " ", " ", " ", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", | |
" ", "@", "@", " ", " ", " ", " ", "@"], | |
[" ", "@", " ", "@", "@", "@", "@", "@", " ", " ", "@", " ", "@", "@", "@", "@", "@", " ", " ", " ", " ", " ", " ", | |
" ", " ", " ", " ", " ", "@", " ", "@"], | |
[" ", "@", "O", " ", " ", " ", " ", " ", " ", " ", "@", " ", "@", "O", " ", " ", " ", " ", "@", "@", "@", "@", "@", | |
" ", "@", "@", "@", " ", "@", " ", " ", "=>"], | |
[" ", "@", " ", "@", "@", "@", "@", "@", "@", "@", "@", " ", "@", "@", "@", "@", "@", " ", "@", "@", "@", "@", "@", | |
" ", "@", "@", "@", " ", "@", "@", "@"], | |
[" ", "@", " ", " ", " ", " ", " ", "@", " ", "@", "@", " ", "@", "@", "@", "@", "@", " ", "@", "@", "@", "O", " ", | |
" ", " ", " ", " ", " ", "@", "@", "@"], | |
[" ", "@", "@", "@", "@", "@", " ", "@", " ", "@", "@", " ", "@", " ", " ", " ", " ", " ", " ", "@", "@", "@", "@", | |
"@", "@", "@", "@", " ", "@", " ", "@"], | |
[" ", "@", " ", " ", " ", " ", "O", "@", " ", "@", "@", " ", "@", "@", " ", "@", "@", "@", " ", " ", " ", " ", " ", | |
" ", "O", " ", "@", " ", " ", " ", "@"], | |
[" ", "@", " ", "@", "@", "@", "@", "@", " ", " ", " ", " ", " ", " ", " ", "@", "@", "@", "@", "@", "@", " ", "@", | |
"@", "@", "@", "@", "@", "@", " ", "@"], | |
[" ", "@", "O", " ", " ", " ", " ", " ", " ", " ", " ", " ", "@", "@", " ", " ", " ", " ", " ", "O", " ", " ", " ", | |
" ", " ", " ", " ", " ", " ", " ", "@"], | |
[" ", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", "@", | |
"@", "@", "@", "@", "@", "@", "@", "@"]] | |
starx = [13, 14, 15, 16, 17, 18, 19, 20, 21] | |
sx = random.sample(starx, 5) | |
map_list[4][sx[0]] = "O" | |
map_list[5][sx[1]] = "O" | |
map_list[4][sx[2]] = "O" | |
map_list[6][sx[3]] = "O" | |
map_list[6][sx[4]] = "O" | |
# 更新地图 | |
def up_map(): | |
# 打印小标题并指定打印区域的文字以及背景颜色 | |
print("\033[200;30;94m ---------lv3---挑战一下------") | |
for i, values in enumerate(map_list): # 遍历二维列表中的18个子列表 | |
for j in range(len(values)): # 遍历子列表中的元素 | |
# 打印每个子列表中的所有元素,并且不换行打印 | |
print(map_list[i][j], end="") | |
print("") # 每打印一个子列表所有元素,换行一次 | |
print("得分", star_number) | |
print("\033[0m", end="") # 背景色结束位置 | |
def godown(): | |
global x, y, step_number, map_list, star_number | |
time.sleep(0.1) | |
if map_list[y + 1][x] == "O": | |
star_number = star_number + 1 | |
if map_list[y + 1][x] != "@": # 判断行走位置不是墙时,@(表示墙) | |
map_list[y][x] = " " # 将小人原来的位置设置为空 | |
y += 1 # 修改Y坐标位置 | |
map_list[y][x] = "Y" # 将移动后的位置设置为Y(表示小人) | |
step_number += 1 # 修改小人移动步数 | |
else: | |
print('\033[31m碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示 | |
up_map() # 更新地图 | |
jiance() # 检查位置 | |
surround() # 周围检查 | |
def goup(): | |
global x, y, step_number, map_list, star_number | |
time.sleep(0.1) | |
if map_list[y - 1][x] == "O": | |
star_number = star_number + 1 | |
if map_list[y - 1][x] != "@": # 判断行走位置不是墙时,@(表示墙) | |
map_list[y][x] = " " # 将小人原来的位置设置为空 | |
y -= 1 # 修改Y坐标位置 | |
map_list[y][x] = "Y" # 将移动后的位置设置为Y(表示小人) | |
step_number += 1 # 修改小人移动步数 | |
else: | |
print('\033[31m碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示 | |
up_map() # 更新地图 | |
jiance() # 检查位置 | |
surround() # 周围检查 | |
def goright(): | |
global x, y, step_number, map_list, star_number | |
time.sleep(0.1) | |
if map_list[y][x + 1] == "O": | |
star_number = star_number + 1 | |
if map_list[y][x + 1] != "@": # 判断行走位置不是墙时,@(表示墙) | |
map_list[y][x] = " " # 将小人原来的位置设置为空 | |
x += 1 # 修改Y坐标位置 | |
map_list[y][x] = "Y" # 将移动后的位置设置为Y(表示小人) | |
step_number += 1 # 修改小人移动步数 | |
else: | |
print('\033[31m碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示 | |
up_map() # 更新地图 | |
jiance() # 检查位置 | |
surround() # 周围检查 | |
def goleft(): | |
global x, y, step_number, map_list, star_number | |
time.sleep(0.1) | |
if map_list[y][x - 1] == "O": | |
star_number = star_number + 1 | |
if map_list[y][x - 1] != "@": # 判断行走位置不是墙时,@(表示墙) | |
map_list[y][x] = " " # 将小人原来的位置设置为空 | |
x -= 1 # 修改Y坐标位置 | |
map_list[y][x] = "Y" # 将移动后的位置设置为Y(表示小人) | |
step_number += 1 # 修改小人移动步数 | |
else: | |
print('\033[31m碰到墙壁!\033[0m') # 当用户输入的位置是墙时,做出提示 | |
up_map() # 更新地图 | |
jiance() # 检测位置 | |
surround() # 周围检查 | |
def jiance(): | |
time.sleep(0.1) | |
global x, y, start | |
if x == 31 and y == 9: | |
print("\033[31m恭喜你呀!完成了lv3的训练!满分15分你拿到了%d分😏\033[0m" % (star_number)) | |
print("\033[31m今天的培训到这就结束喽😘\033[0m") | |
print("\033[31m共计行走了", step_number, "步!\033[0m") | |
print("\033[31m共计用时", float(time.time() - start), "秒!\033[0m") | |
sys.exit(0) | |
def surround(): | |
global x, y, step_number, map_list, shang, xia, zuo, you | |
shang = map_list[y - 1][x] | |
xia = map_list[y + 1][x] | |
zuo = map_list[y][x - 1] | |
you = map_list[y][x + 1] | |
up_map() # 更新模拟地图 | |
###############################################路径规划################################################## |
Author
cxin-william
commented
Nov 1, 2023
•
还有没有自动走,吃完所有糖豆并且通关的方法?大概是用深度优先搜索(DFS), 或者广度优先搜索(BFS)算法
route_list = [
'r 1',
'd 2',
'r 4',
'd 4',
'l 2',
'u 2',
'l 2',
'd 6',
'r 4',
'd 2',
'l 4',
'd 2',
'r 9',
'u 1',
'r 3',
'd 2',
'r 7',
'u 2',
'r 3',
'l 6',
'u 1',
'l 1',
'u 3',
'l 4',
'r 4',
'u 1',
'r 6',
'u 4',
'l 10',
'd 2',
'r 8',
'u 1',
'l 8',
'u 1',
'r 10',
'd 7',
'l 2',
'r 6',
'u 4',
'l 1',
'u 2',
'r 3',
'l 3',
'd 2',
'r 3',
'd 2',
'r 2',
]
for route in route_list:
print(route)
direction_count = route.split(' ')
step_count = int(direction_count[1])
if direction_count[0] == 'r':
[goright() for i in range(step_count)]
if direction_count[0] == 'l':
[goleft() for i in range(step_count)]
if direction_count[0] == 'd':
[godown() for i in range(step_count)]
if direction_count[0] == 'u':
[goup() for i in range(step_count)]
###############################################路径规划##################################################
global shang, xia, zuo, you
surround() # 周围检查
processed = set()
def is_gold(target):
return target == "O"
def is_exit(target):
return target == "=>" and x != 1
def can_go_next(target):
return target == " " or target == "O"
actions = {
"shang": {
"next_position": lambda: (x, y - 1),
"next_value": lambda: shang,
"action": goup,
"reverse": godown,
},
"xia": {
"next_position": lambda: (x, y + 1),
"next_value": lambda: xia,
"action": godown,
"reverse": goup,
},
"you": {
"next_position": lambda: (x + 1, y),
"next_value": lambda: you,
"action": goright,
"reverse": goleft,
},
"zuo": {
"next_position": lambda: (x - 1, y),
"next_value": lambda: zuo,
"action": goleft,
"reverse": goright,
},
}
def dfs(prev, target_exit=False):
for direction in ["shang", "you", "xia", "zuo"]:
action = actions[direction]
next_value = action["next_value"]()
next_position = action["next_position"]()
if next_position not in processed:
if (not target_exit and is_gold(next_value)) or (target_exit and is_exit(next_value)):
action["action"]()
processed.add(next_position)
dfs(direction, target_exit)
for direction in ["shang", "you", "xia", "zuo"]:
action = actions[direction]
next_value = action["next_value"]()
next_position = action["next_position"]()
if next_position not in processed:
if can_go_next(next_value):
action["action"]()
processed.add(next_position)
dfs(direction, target_exit)
if prev:
actions[prev]["reverse"]()
dfs(None, False)
processed.clear()
dfs(None, True)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment