[英]Representing a maze
How would I represent this maze, so that I can run dijkstras algorithm on it? 我将如何表示这个迷宫,以便可以在其上运行dijkstras算法?
I've been looking around, and the most common representations seem to be the adjacency matrix and adjacency list. 我一直在环顾四周,最常见的表示形式似乎是邻接矩阵和邻接列表。
So: 所以:
1) What should my vertices be? 1)我的顶点应该是什么?
2) What should my edges be? 2)我的边缘应该是什么?
Because it will be a race, the maze is not known before hand. 因为这将是一场比赛,所以迷宫是未知的。
3) How do I update my matrix? 3)如何更新矩阵?
Note: We are given a chance to explore the maze, so I'll be using a wall follower along with a mapper that calculates the distance the robot is from the start, but not sure how this would all translate to be of any use when building the matrix. 注意:我们有机会探索迷宫,所以我将使用墙随动件和一个映射器,该映射器可计算机器人从一开始的距离,但不确定如何将其转化为任何用途建立矩阵。
For example, you can declaes a room structure as: 例如,您可以将房间结构装饰为:
struct tRoom {
enum {
NORTH,
EAST,
SOUTH,
WEST
};
int walls[4]; /* status: 0 -- The wall is open
1 -- The wall is close
*/
...
}
/* A room in the game looks like:
____ The wall is close and cannot walk through
|
|
v
+-----+
| |
|
| |
+- -+
^
|
|____ The wall is open and can walk through
*/
/* A maze looks like :
+-----+-----+-----+
| | | |
| * |
| | | |
+- -+-----+- -+
| | | |
| | | |
| | | |
+-----+-----+-----+
*/
And declare your maze as: 并声明您的迷宫为:
struct tRoom[WIDTH][HEIGHT];
Initially the robot is put to the starting room (which has a star). 最初,机器人被放置在起跑室(有一颗星星)中。
Then the player can cotrol movement of the robot by keypress or other devices. 然后,玩家可以通过按键或其他设备控制机器人的运动。
The game ends when the robot reaches the goal room. 当机器人到达目标房间时,游戏结束。
Surely you can find a path by some algorithm and then guide the robot to the goal. 当然,您可以通过某种算法找到一条路径,然后将机器人引导至目标。
I would define a struct that represents each junction of the maze as a node, with each corridor branching out as a vertex in the form of a pointer to another node. 我将定义一个结构,该结构将迷宫的每个交点表示为一个节点,每个走廊以指向另一个节点的指针的形式分支为一个顶点。 Something along the following ought to do it: 应该采取以下措施:
#define MAX_VERTEXES_PER_NODE 4
struct junction;
struct corridor {
int weight;
struct junction *other_end;
};
struct junction {
struct corridor junctions[MAX_VERTEXES_PER_NODE];
};
It is straightforward to apply Dijkstra (or any other graph theory algorithm) to this representation. 将Dijkstra(或任何其他图论算法)应用于此表示很简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.