Search and backtracking · 12 / 15 Exercise Medium

Paths in a grid

Objective

Create a Python program that counts in how many ways you can go from the top-left to the bottom-right corner of a grid moving only right or down, avoiding obstacles.

Instructions

  • Read an integer n (1 to 7) and then n rows of text, all with the same number of characters: . is a free cell and # an obstacle.
  • Write a recursive function count(grid, r, c) that returns 0 if it goes outside the grid or steps on an obstacle, 1 if it reaches the final cell, and the sum of going down and going right otherwise.
  • If the starting cell is an obstacle, the result is 0. Show Paths: t.
Input
3
...
.#.
...
Output
Paths: 2

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise