ENع
→Grid paths
الدرس 43 · Dynamic programming and backtracking

Grid paths and counting

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةAdvanced
عن هذا الدرس

A robot crosses a grid, moving only right or down. How many routes are there? Listing them is hopeless on big grids: a 16 by 16 grid has 155,117,520. Instead, fill a table where each square holds the routes that reach it, the sum of the square above and the square to its left. Watch the 3 by 4 table fill to 10, then refill to 4 around a rock. The Software lesson "Dynamic programming" covers memoisation and tables, and "Defining states and transitions" covers how to name a state.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 43 · تفاعلي
01 / 10

How many routes?

A robot starts on the top-left square of a 3 by 4 grid and must reach the bottom-right square. Each move goes one square right or one square down. How many different routes can it take?

grid: 3 rows × 4 columns, squares (row, column)
start (0, 0), goal (2, 3)
moves: right (column + 1) or down (row + 1)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. How many routes?

    A robot starts on the top-left square of a 3 by 4 grid and must reach the bottom-right square. Each move goes one square right or one square down. How many different routes can it take?

    grid: 3 rows × 4 columns, squares (row, column)
    start (0, 0), goal (2, 3)
    moves: right (column + 1) or down (row + 1)
  2. Listing every route

    Every route is 5 moves: 3 right and 2 down, in some order. Listing them works for this small grid, but the list explodes: a 16 by 16 grid has 155,117,520 routes. We need to count without listing.

    route 1: R R R D D
    route 2: D D R R R
    every route: 3 × R and 2 × D
    16 × 16 grid: 15 R + 15 D = 30 moves, 155,117,520 routes
  3. Look at the last move

    Any route into a square arrives from the square above it or from the square to its left. So ways(r, c) = ways(above) + ways(left). That is the state and the transition: each count needs only two smaller counts.

    state:      ways(r, c) = routes from the start to (r, c)
    transition: ways(r, c) = ways(r-1, c) + ways(r, c-1)
    example:    ways(1, 2) = ways(0, 2) + ways(1, 1)
    code lines 11 and 13
  4. The start and the edges

    The start holds 1: the route that has not moved yet. A top-row square can only be reached from the left, and a left-column square only from above, so each of them holds 1 too.

    base case: ways(0, 0) = 1       (line 9)
    top row:   ways(0, c) = ways(0, c-1) = 1
    left col:  ways(r, 0) = ways(r-1, 0) = 1
    off the grid counts as 0
  5. Fill row 1

    Go left to right. (1, 1) = 1 above + 1 left = 2. Then (1, 2) = 1 + 2 = 3, and (1, 3) = 1 + 3 = 4. Both numbers a square needs are always filled already.

    ways(1, 1) = 1 + 1 = 2
    ways(1, 2) = 1 + 2 = 3
    ways(1, 3) = 1 + 3 = 4
    row by row, left to right: a topological order
  6. Fill row 2

    Same again: 2 + 1 = 3, then 3 + 3 = 6, then 4 + 6 = 10. The goal holds 10: there are 10 routes. We did one small sum per square, not one walk per route.

    ways(2, 1) = 2 + 1 = 3
    ways(2, 2) = 3 + 3 = 6
    ways(2, 3) = 4 + 6 = 10
    answer: ways(2, 3) = 10
  7. Check: 5 choose 2

    Check another way. A route is 5 moves, and picking which 2 of them go down fixes the route: 5 choose 2 = 10. But the formula needs an empty grid.

    route R D R R D: down at moves 2 and 5
    C(5, 2) = 5 × 4 / 2 = 10
    empty R × C grid: C(R + C - 2, R - 1)
    Python: math.comb(5, 2) == 10
  8. Drop a rock

    Now a rock blocks square (1, 1). No route may pass through it, so it holds 0. The formula no longer fits, but the table still works: line 6 skips the rock and leaves its count at 0.

    grid = ["....",
            ".#..",
            "...."]
    ways(1, 1) = 0   (line 6: continue)
    the other squares must be filled again
  9. Refill around the rock

    Same order, same sums. Row 1: 1 + 0 = 1, 1 + 1 = 2. Row 2: 0 + 1 = 1, 1 + 1 = 2, and the goal 2 + 2 = 4. Only 4 of the 10 routes dodge the rock.

    ways(1, 2) = 1 + 0 = 1
    ways(1, 3) = 1 + 1 = 2
    ways(2, 1) = 0 + 1 = 1
    ways(2, 2) = 1 + 1 = 2
    ways(2, 3) = 2 + 2 = 4
    count_paths(grid) returns 4
  10. Rows times columns

    The loops visit each square once and do at most two additions there, so an R by C grid costs O(R × C) time. A 16 by 16 grid is 256 squares, against 155,117,520 routes to list.

    subproblems: R × C squares
    work per square: O(1), at most 2 additions
    time: O(R × C), memory: O(R × C) for the table
    16 × 16: 256 squares vs 155,117,520 routes
    keep one row only: memory O(C)

اختبار يتاح بعد الخطوة الأخيرة.