Skip to content

Instantly share code, notes, and snippets.

@yesterdaysun
Last active November 1, 2023 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yesterdaysun/ee0bfa5b6d8a54d53c0fe0e79b8e923f to your computer and use it in GitHub Desktop.
Save yesterdaysun/ee0bfa5b6d8a54d53c0fe0e79b8e923f to your computer and use it in GitHub Desktop.
###############################################路径规划##################################################
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