The problem
The grid has no edges: x and y can be any whole numbers, including negative ones. Some squares are walls. Each move goes one square up, down, left or right, into a square that is not a wall. Return the fewest moves from start to goal. A route always exists, and the start is never a wall.
moves_needed((0, 0), (4, 0), [(2, -1), (2, 0), (2, 1)]) # 8
moves_needed((-2, 7), (3, 1), []) # 11
Limits
- Up to 3,000 moves apart, and a few hundred walls.
- A breadth-first search looks at every square closer than the goal: millions of squares. Use A* with a good guess, and think about which entry should come first when two have the same f. The big tests have 3 seconds each.
Builds on PATH-7 A* search.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
10 tests, not run yet
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | The example | Not run | |
| 2 | The start is the goal | Not run | |
| 3 | No walls | Not run | |
| 4 | Into a pocket | Not run | |
| 5 | A gap in a long wall | Not run | |
| 6 | A guess must never be too high | Not run | |
| 7 | 3,000 squares in a straight line | Not run | |
| 8 | Across a diagonal | Not run | |
| 9 | A wall of 301 squares | Not run | |
| 10 | Far below zero | Not run |