Gamified challenge · Backtracking
The board is a triangle with rows rows (1 to 5, so at most 15 holes). Holes are numbered row by row from the top: row 0 is hole 0, row 1 is holes 1 2, row 2 is 3 4 5, and so on. Every hole has a peg except the ones listed in empty.
A jump moves a peg over an adjacent peg, in a straight line along a row or either diagonal, into the empty hole just beyond; the jumped peg is removed. Write jumpPegs(rows, empty) that returns jumps as [from, to] pairs that leave exactly one peg. Any winning sequence passes. If the board can't be won, or already has a single peg, return an empty list.
Example: on 4 rows with empty = [1], the first jump could be [6, 1] (hole 6 over hole 3 into hole 1).
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google