Representing the maze in the Dictionary in Python

53 views Asked by At

I am a new computer science student, and now we know the Python language. It is beautiful, but I have many problems with it. Sorry, I can solve this question. enter image description here

I tried to solve it using the Dictionary, but I did not know how to represent it well. I only know how to represent simple things.

1

There are 1 answers

3
Samwise On

When you're thinking about how to represent a problem in the form of a data structure, you should usually start by thinking about what you will need to do with the structure -- what questions do you want it to be able to answer? For the case of a maze, I'd probably start with "given a cell in the maze, I want to know what other cells I can get to".

With a dictionary, you could make each cell a key, with the values being a list of the other reachable cells. Hence:

maze = {
    'A1': ['A2'],
    'A2': ['A1', 'B2'],
    ...
}

Given that structure it'd be pretty easy to write a BFS that would tell you the shortest path between any two cells.