用Python制作迷宫图

发布于 2024-10-12 15:21:52 字数 249 浏览 6 评论 0 原文

嘿,我正在尝试使用 Python 中的字典制作图表。我正在使用一个包含迷宫的 txt 文件(b 代表墙壁,a 代表路径),我正在尝试制作一个字典,列出在迷宫中采取的所有可能的动作(简单的步骤,而不是完整路径)。我应该从哪里开始有什么想法吗?我从来没有使用过字典。

非常感谢您的帮助,这让我有了一个良好的开端。还有一个问题,我从一栋有效的房子开始,检查所有可能的路径。之后我必须搬到另一所房子并检查那里的路径。我怎样才能确保我不会出现无限循环或重新检查已经检查过的房子?

Hey, I'm trying to make a graph using dictionaries in Python. I'm using a txt file containing a maze (b for walls a for paths) and I'm trying to make a dictionary that lists all the possible moves to take in a maze (simple steps, not full paths). Any ideas on where I should start? I never worked with dictionaries.

Thank you so much for you help, that got me off on a great start. Just one more question, I'm starting at one valid house and checking all possible paths. after that ill have to move to another house and check the paths on that. How can i make sure i dont get an infinite loop or recheck a house ive already checked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2024-10-19 15:21:52

假设您的迷宫看起来像一个网格,迷宫中的位置可以表示为元组 (row,col)。当您构建字典时,为迷宫中的每个位置创建一个条目,初始值是一个空列表。在迷宫中的每个有效位置 (r,c),计算是否可以到达 (r-1,c)、(r,c-1)、(r+1,c) 和 (r,c+) 1)。如果可以的话,将该元组添加到列表中。所以,假设我可以从 (r,c) 到达 (r-1,c) 和 (r,c+1),字典中的条目看起来像

  maze_dict[(r,c)] = [(r-1,c), (r,c+1)]

要创建一个空字典,您可以使用:

maze_dict = {}

您还应该查看 python 教程的 字典 部分

Assuming that your maze looks like a grid, a position in the maze could be represented as a tuple (row,col). When you construct your dictionary, create an entry for every position in the maze, the initial value is an empty list. At every valid position (r,c) in the maze, figure out if you can get to (r-1,c), (r,c-1), (r+1,c), and (r,c+1). If you can, then add that tuple to the list. So, let's say that I can get to (r-1,c) and (r,c+1) from (r,c), the entry in the dictionary would look like

  maze_dict[(r,c)] = [(r-1,c), (r,c+1)]

To create an empty dictionary, you'd use:

maze_dict = {}

You should also take a look at the dictionaries section of the python tutorial

↙温凉少女 2024-10-19 15:21:52

非常感谢您的帮助,这让我有了一个良好的开端。只是
还有一个问题,我从一所有效的房子开始并检查所有
可能的路径。之后我必须搬到另一所房子并检查
其上的路径。我怎样才能确保我不会出现无限循环或
重新检查我已经检查过的房子?

创建一个“House”类,及其网格坐标:

class House(object):
    def __init__(self, pos):
        self.pos = pos # the coordinates (position) on the grid, a tuple
        self.paths = [] # Empty array to hold paths

创建一些房屋:

houses = [House((1,3)), House((3,3)), House((4,3))] # a list of houses

现在,遍历每个房屋,并计算其路径

paths = {}
paths[(1,3)] = [(2,3), (4,3) ... ] # possible paths to the point (1,3)

for i in houses:
    try:
       i.paths = paths[(i.pos)]
    except KeyError:
       print "I don't know how to get to ", i.pos

逐步浏览列表可确保您仅检查每个房屋一次。现在您可以找出无法到达的房屋:

for i in houses:
   if not i.paths:
      print "I did not find a way to reach the house at ",i.pos

Thank you so much for you help, that got me off on a great start. Just
one more question, I'm starting at one valid house and checking all
possible paths. after that ill have to move to another house and check
the paths on that. How can i make sure i dont get an infinite loop or
recheck a house ive already checked?

Create a "House" class, with its grid coordinates:

class House(object):
    def __init__(self, pos):
        self.pos = pos # the coordinates (position) on the grid, a tuple
        self.paths = [] # Empty array to hold paths

Create some houses:

houses = [House((1,3)), House((3,3)), House((4,3))] # a list of houses

Now, go through each house, and caculate its path(s)

paths = {}
paths[(1,3)] = [(2,3), (4,3) ... ] # possible paths to the point (1,3)

for i in houses:
    try:
       i.paths = paths[(i.pos)]
    except KeyError:
       print "I don't know how to get to ", i.pos

Stepping through the list ensures that you check each house only once. Now you can find out the houses that are unreachable:

for i in houses:
   if not i.paths:
      print "I did not find a way to reach the house at ",i.pos
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文