使用图解迷宫
嘿,我参加了一场当地的编程比赛,他们问了我这个问题,但我做不到,所以请帮助我解决这个问题。
编写一个程序,从迷宫大小的文件中加载,然后加载迷宫本身。 为了对迷宫进行建模,我们使用字符“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不知道要搜索什么:
http://en.wikipedia.org/wiki/Pathfinding#Sample_algorithm
这包含更多信息:
http://www.astrolog.org/labyrnth/algrithm.htm
In case you don't know what to search for:
http://en.wikipedia.org/wiki/Pathfinding#Sample_algorithm
and this contains a LOT more info:
http://www.astrolog.org/labyrnth/algrithm.htm
找到路径的一种方法是:
一旦队列为空,迷宫中的每个空闲单元(可以从目的地到达)将具有到达目的地的最短路径中的步数。如果单元格没有计数,则没有从它到目的地的路径。
如果起始单元格有计数,
我将让您自行决定如何加载迷宫。这是这一切中最简单的部分。
One way to find a path:
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,
I'll leave it up to you to figure out how to load the maze. That's the easy part of all this.
代码太多,无法在这里编写,但解决迷宫的最常见方法是朝一个方向出发,并且在每次可以右转时右转。
只要起点和出口位于四个周围墙壁之一,就可以保证这一点。对于没有沿着墙壁起点和出口的迷宫,这是一种递归练习。
以此为起点,看看您能想出什么代码!
哈特哈,
詹姆斯
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