PATH-P7

Across an endless grid

Practice 7 of 7Shortest paths10 tests

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

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

10 tests, not run yet
Test results
#TestResultReads · time
1The exampleNot run
2The start is the goalNot run
3No wallsNot run
4Into a pocketNot run
5A gap in a long wallNot run
6A guess must never be too highNot run
73,000 squares in a straight lineNot run
8Across a diagonalNot run
9A wall of 301 squaresNot run
10Far below zeroNot run