Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Views: 30
Image: ubuntu2004
Kernel: Python 3 (system-wide)

Count the number of distinct paths from the start to the end of the 5x5 maze.

maze = [[0, 0, 0, 0, 0], [0, -1, -1, -1, 0], [0, 0, -1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
def maze_solver(maze): rows = len(maze) columns = len(maze[0]) M = [[0 for i in range(columns)] for j in range(rows)] M[0][0] = 1 for j in range(1,columns): if maze[0][j] == -1: M[0][j] = 0 else: M[0][j] = M[0][j-1] for i in range(1,rows): if maze[i][0] == -1: M[i][0] = 0 else: M[i][0] = M[i-1][0] for i in range(1,rows): for j in range(1,columns): if maze[i][j] == -1: M[i][j] = 0 else: M[i][j] = M[i-1][j] + M[i][j-1] return M[-1][-1]
maze_solver(maze)
10

Thus, we have 10 distinct paths to the end of the maze.