简体   繁体   中英

Representing a maze

How would I represent this maze, so that I can run dijkstras algorithm on it?

Maze

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?

2) What should my edges be?

Because it will be a race, the maze is not known before hand.

3) How do I update my matrix?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM