A Dynamic Programing Based
Solution to the Two-Dimensional
Jump-It Problem
Jamil Saquer and Razib Iqbal
Computer Science Department
Missouri State University
Springfield, MO
Introducing the problem
• 2D board with a cost to visit each cell
• A player starts at the top-left cell and wants to
reach the bottom-right (exit) cell with the
lowest cost
5
7
2
4
8
• Allowed moves:
1
6
8
1
3
– one cell to the right
9
4
– one cell below
– jump over one cell to the right
– Jump over one cell below
1
6
5
8
1
7
9
3
2
Possible Paths
5
7
2
4
8
5
7
2
4
8
1
6
8
1
3
1
6
8
1
3
9
1
6
5
8
9
1
6
5
8
4
1
7
9
3
4
1
7
9
3
cost = 5 + 7 + 1 + 5 + 8 + 3 = 29
cost = 5 + 1 + 4 + 7 + 3 = 20
5
7
2
4
8
1
6
8
1
3
9
1
6
5
8
4
1
7
9
3
cost = 5 + 2 + 4 + 1 + 3 + 3 = 18
3
Goal
• Find the cheapest cost of playing the game
• Find path that leads to playing the game with
the cheapest cost
4
Review – 1D Jump-It
5
15
75
7
43
11
5
Review – 1D Jump-It
start
5
15
75
7
43
11
• solution:
• if board length == 1, visit that cell
6
Review – 1D Jump-It
start
5
15
75
7
43
11
• solution:
• if board length == 1, visit that cell
• if board length == 2, visit both cells
7
Review – 1D Jump-It
start
5
•
•
•
•
15
75
7
43
11
solution:
if board length == 1, visit that cell
if board length == 2, visit both cells
if board length == 3, cheaper to jump over
8
Review – 1D Jump-It
start
5
•
•
•
•
•
15
75
7
43
11
solution:
if board length == 1, visit that cell
if board length == 2, visit both cells
if board length == 3, cheaper to jump over
else
min_cost = board[i] +
min{jumpIt(i+1),
jumpIt(i+2)}
9
2D Jump-It
Finding the Cheapest Cost
• Let jumpIt(r, c) be the cheapest cost of playing
the game starting at any cell (r, c)
• Many cases
5
7
2
4
8
1
6
8
1
3
9
1
6
5
8
4
1
7
9
3
10
Finding the Cheapest Cost
5
7
2
4
8
1
6
8
1
3
9
1
6
5
8
4
1
7
9
3
11
Finding the Cheapest Cost
5
7
2
4
8
1
6
8
1
3
9
1
6
5
8
4
1
7
9
3
12
Dynamic Programming Solution
• 2D jump-It is a good candidate for DP
– Overlapping sub-problems
– Optimal sub-structure
13
Top-Down DP Solution
• board[i][j] contains cost of visiting cell (i, j)
• costs – a cache table for storing solutions
– costs[i][j] minimum cost starting game at cell (i, j)
• fill cache table starting with basic cases
0
0
0
0
0
5
7
2
4
8
0
0
0
0
0
1
6
8
1
3
0
0
0
0
0
9
1
6
5
8
0
0
0
0
0
4
1
7
9
3
costs
board
14
Top-Down DP Solution
1.
2.
3.
4.
5.
fill last row starting
fill in last column
fill the row before last
fill the column before last
fill rest of cache table
18
18
13
11
14
21
13
12
14
16
14
11
10
costs
5
7
2
4
8
7
14
6
1
6
8
1
3
16
11
9
1
6
5
8
12
3
4
1
7
9
3
board
15
Finding Optimal path
1. Use another cache table, path, to remember moves
2. path[i][j] - coordinates of cell visited after cell (i ,j)
0
2
3
4
1
5
7
2
4
8
18
18
13
11
1
6
8
1
3
13
1
6
5
8
12
14
16
7
9
14
21
14
6
16
11
2
4
1
7
9
3
14
11
10
12
3
3
board
(0, 2)
(0, 3)
(0, 3)
(1, 3)
(1, 4)
1
(1,1)
(1, 3)
(1, 4)
(1, 4)
(3, 4)
2
(2, 1)
(3, 1)
(3, 2)
(2, 4)
(3, 4)
(3, 2)
(3,4)
(3, 4)
(-1,-1)
1
2
3
4
(3, 2)
0
path
0
costs
0
3
1
16
2D Jump-It in The CS Curriculum
• Good problem to use when teaching DP
17
Questions
18
© Copyright 2026 Paperzz