使用图解迷宫

发布于 2024-10-02 07:20:54 字数 479 浏览 10 评论 0原文

嘿,我参加了一场当地的编程比赛,他们问了我这个问题,但我做不到,所以请帮助我解决这个问题。

编写一个程序,从迷宫大小的文件中加载,然后加载迷宫本身。 为了对迷宫进行建模,我们使用字符“S”来指定起始单元格“.”。指定空闲单元,“#”是一堵墙,“F”是最后一个单元。 编写一个程序来找到从起始单元格到最终单元格的路径。 你可以认为迷宫中有一个服从命令的机器人,那么对于下面的迷宫,机器人应该接收以下命令:上,上,右,右,下,下。

迷宫 1 文本文件

5 5
#####
#...#
#.#.#
#S#T#
#####

迷宫 2 文本文件

4 5
#.#.#
#.#.#
#S#T#
#####

一般编写程序(迷宫最大输入最多为 200x200)。

非常感谢您的帮助。我只是一个即将升入大二的学生,所以如果你能给我提供代码,那么我就能理解它,他们会自己再做一次。

Hey i was in a local programming competition and they asked me this question which i could not do so please help me on this one.

Write a program that loads from a file the size of a maze and then the maze itself.
To model the maze we use the character "S" that specifies the start cell, "." that specifies free cell, "#" is a wall and "F" is the final cell.
Write a program that will find a path from the start cell to the final cell.
You can think that in the maze there is a robot that obeys commands, so for the following maze the robot should receive the following commands: up, up, right, right, down, down.

maze 1 text file

5 5
#####
#...#
#.#.#
#S#T#
#####

maze 2 text file

4 5
#.#.#
#.#.#
#S#T#
#####

Write your program in general (the maze maximum input can be at most 200x200).

Help would be much appreciated. I am just a rising sophmore so if you could provide me the code then i could understand it and they do it again bymyself.

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

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

发布评论

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

评论(3

时光与爱终年不遇 2024-10-09 07:20:54

找到路径的一种方法是:

  1. 有一个要检查的单元队列,以及每个单元从那里到目的地的步数。
  2. 将结束单元格的计数设置为 0,并将其添加到队列中。
  3. 当队列不为空时:
    1. 从队列中获取一个单元格。
    2. 对于每个空闲相邻单元格,将当前单元格的计数 + 1 与相邻单元格的计数进行比较。如果小于,或者如果相邻小区还没有计数,则将相邻小区的计数设置为当前小区的计数+1,然后将相邻小区添加到队列中。

一旦队列为空,迷宫中的每个空闲单元(可以从目的地到达)将具有到达目的地的最短路径中的步数。如果单元格没有计数,则没有从它到目的地的路径。

如果起始单元格有计数,

  1. 则获取起始单元格的计数。
  2. 检查每个相邻单元格的计数 (count - 1)。 将会有一个,这是路径的下一步。记录前往该单元格的方向,然后获取该单元格,如果不是目的地,则对该单元格重复步骤 2。

我将让您自行决定如何加载迷宫。这是这一切中最简单的部分。

One way to find a path:

  1. Have a queue of cells to check, and a count of steps for each cell from there to the destination.
  2. Set the ending cell's count to 0, and add it to the queue.
  3. While the queue is not empty:
    1. Get a cell from the queue.
    2. For each free neighbor cell, compare the current cell's count + 1 to the neighbor cell's count. If it's less, of if the neighbor cell doesn't have a count yet, set the neighbor cell's count to the current cell's count + 1, then add the neighbor cell to the queue.

Once the queue's empty, every free cell in the maze (that can be reached from the destination) will have the number of steps in the shortest path to the destination. If a cell doesn't have a count, there's no path from it to the destination.

If the start cell has a count,

  1. Get the start cell's count.
  2. Check each neighbor cell for a count of (count - 1). There will be one, and that's the next step in the path. Record the direction to that cell, and then get that cell, and if it's not the destination, repeat step 2 with that cell.

I'll leave it up to you to figure out how to load the maze. That's the easy part of all this.

绝情姑娘 2024-10-09 07:20:54

代码太多,无法在这里编写,但解决迷宫的最常见方法是朝一个方向出发,并且在每次可以右转时右转。

只要起点和出口位于四个周围墙壁之一,就可以保证这一点。对于没有沿着墙壁起点和出口的迷宫,这是一种递归练习。

以此为起点,看看您能想出什么代码!

哈特哈,
詹姆斯

The code is too much to write here, but the most common way of solving mazes is to set off in one direction, and at every right turn you can make, turn right.

This is guaranteed to work so long as the start and exit are in one of the four surrounding walls. For mazes that don't have their start and exit along the walls, it's an exercise in recursion.

See what you can come up with code-wise based off that as a starting point!

HTH,
James

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文