The routes past the roadworks
- O(n²) · Very hard
- Full plan
- Python
- JavaScript
- recursion
- grids
- dictionaries
Problem
Nora delivers parcels by bike around a neighborhood of straight streets. She sets off from the top left corner and delivers at the bottom right one, and every street is one way: from each corner she can only go on to the corner to the right or the one below. This month there are roadworks and some corners are closed, so she does not go through those. She got curious about how many different ways she can do the round.
Write a function that takes city, a list of strings, one per row of corners and all of them the same length. Each character is a corner: . if it is clear and # if it is closed. There are 1 to 17 rows and 1 to 17 corners in each row.
Return a whole number: how many different routes lead from the top left corner to the bottom right one going only through clear corners. If the corner she sets off from or the corner where she delivers is closed, there is none: return 0.
With ["...", ".#.", "..."] it returns 2: with the roadworks in the middle the only ones left are the route that goes all the way right and then all the way down, and the one that goes all the way down and then all the way right. If the neighborhood is a single corner and it is clear, Nora is already where she delivers, and that counts as one route: it returns 1.
Careful with the size: in a 17 by 17 neighborhood there can be hundreds of millions of routes, so walking them one by one does not fit in the time the engine gives you. Many routes share the same stretch, so it is not worth working out the same thing twice.
Examples
The example
["...", ".#.", "..."] → 2
No roadworks, two rows
["...", "..."] → 3
A single corner
["."] → 1
The corner where she delivers is closed
["..", ".#"] → 0
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def routes(city):
passJavaScript
function routes(city) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.