Represent Maze in Python

93 Views Asked by At

How could I represent the following maze in Python?

I would like to make a program that navigates it so that it goes from Start to End, while also going to every Gate before it reaches End.

I am not very familiar with data structures, so all help is appreciated!

Maze

2

There are 2 best solutions below

0
Bart McEndree On BEST ANSWER

I think you want a 2d array of nodes. Some would suggest using Numpy. A node object could have a type property (normal,start,end,gate) and some way to represent exits (North,South,East,West). For discovering all gates you would need to ensure all nodes were checked. Maybe this strategy would help.

0
C L On

This could be represented using arrays in python. For example if each node is a list with a state then a north east south west values (0,0) might look like this.

['blank', 0, 1, 1, 0]

Where the 0s have no connection, 1s are a connection, and 'blank' is if it is a gate or what not. Then the first row

[['blank', 0, 1, 1, 0],[],[],[]]

Then all the rows

maze = [[[],[],[],[]],
        [[],[],[],[]],
        [[],[],[],[]],
        [[],[],[],[]]]

It would then be possible to read each list and know where you are and what is around you. So in this case if you wanted to print the information about the start

print(maze(3,1)) would output a list ['start',0,1,0,1]