10. Peg Solitaire Robert Snapp [email protected] Department of Computer Science University of Vermont Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 1 / 19 The Puzzle Five cylindrical holes in a row. Two red pegs. Two blue pegs. The Initial State The Goal State Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 2 / 19 Four possible moves 1. A red peg can step one position to the right into the empty space. Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 3 / 19 Four possible moves 2. A red peg can jump two positions to the right into the empty space. Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 4 / 19 Four possible moves 3. A blue peg can step one position to the left into the empty space. Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 5 / 19 Four possible moves 4. A blue peg can jump two positions to the left into the empty space. Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 6 / 19 Questions 1 How many states (i.e. peg configurations) exist? 2 Which peg configurations can be obtained using a sequence of legal moves? 3 Is the goal configuration accessible? (Is it possible to solve this puzzle?) 4 If the puzzle can be solved, how many solutions exist? 5 If the puzzle can be solved, what is the shortest solution? 6 Can the puzzle be generalized? 7 Can you think of any other questions? Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 7 / 19 Data Representation We can use a list to represent each state of the puzzle. A red R will represent a red peg. A blue B will represent a blue peg. An black E will represent the empty space. A list of five symbols will represent the state. The initial state is thus (R R E B B). The four successors are (E R R B B), (R E R B B), (R R B E B), (R R B B E). The goal state is thus (B B E R R). Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 8 / 19 Enumerating the states of the four-peg puzzle For every unique state of the four-peg puzzle, there exists a unique list containing two Rs, two Bs, and one E. Thus there exists a one-to-one correspondence between the set of states of the puzzle, and the set of lists (containing two Rs, two Bs, and one E); and thus, the number of state equals the number five-letter anagrams that can be formed using two Rs, two Bs, and one E: 5Š D 30: 2Š 2Š 1Š It is not clear at this point, however, if all 30 states can be reached from the initial state by following a sequence of legal operations. Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 9 / 19 The Four Operations Each of the four legal operations can be expressed symbolically: Assuming we begin in the initial state: 1 Red Step (R R E B B) ! (R E R B B): 2 Red Jump (R R E B B) ! (E R R B B): 3 Blue Step (R R E B B) ! (R R B E B): 4 Blue Jump (R R E B B) ! (R R B B E): Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 10 / 19 The Four Operations: Generalization Let the symbol ? denote a single stationary peg of either color, and let the symbol * denote any number of stationary pegs. 1 2 3 4 Red Step (* R E *) ! (* E R *): (RS) (* R ? E *) ! (* E ? R *): (RJ) (* E B *) ! (* B E *): (BS) (* E ? B *) ! (* B ? E *): (BJ) Red Jump Blue Step Blue Jump Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 11 / 19 State Transistion Graph: A directed graph (R R E B B) (R R B E B) (R E R B B) (R E B R B) (R B R E B) (E R R B B) (R R B B E) (R B E R B) (R B R B E) (R B E B R) (E B R R B) (R B B R E) (E B R B R) (E R B R B) (R B B E R) (B E R R B) (B E R B R) (B R E R B) (B R B R E) (B R B E R) (B E B R R) (B B R E R) (B B E R R) This is an example of a directed graph: edges can only be traversed in the direction of the arrows, as each peg can only move in one direction. Solid edges denote steps; dashed edges denote jumps. Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 12 / 19 How many solutions are there? Two solutions of 8 moves (4 jumps + 4 steps) exist for the 4-peg puzzle. How many moves are required to solve an eight-peg puzzle? How many moves are required to solve a 2n-peg puzzle? These questions can be answered either by constructing the graph of the state space (as we did above), or by delving a little deeper into the structure of the puzzle. Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 13 / 19 Algebraic method for computing the solution length Imagine that we have a special odometer that measures the total distance D that all the pegs have traveled as we work through the four-peg puzzle. Thus for the initial state the odometer reads D D 00. With every red or blue step, one unit is added to the reading. With every red or blue jump, two units are added. What does is odometer reading once the goal state has been reached? Proceeding sequentially along either solution you should be able to determine that the final reading is D D S C 2J D 12, where S D 4 (the number of steps) and J D 4 (the number of jumps. However, we can also infer this value simply by adding up the net displacement require to move each peg into its final position. Note that each of the four pegs must move three spaces to reach their final destinations. Hence D D 4 3 D 12. Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 14 / 19 Algebraic method for computing the solution length Now note that a change in the order between a red peg and a blue peg requires a single jump. Likewise, to change the order of each red peg with two blue pegs requires two jumps. Therefore, the total number of jumps requires is J D 2 2 D 4: To complete the calculation, we note that each jump moves a peg by two spaces, and each step by one space. Since D D S C 2J , it follows that S D D 2J D 12 2.4/ D 4. Thus, the total number of moves required is M D J C S D 4 C 4 D 8. As an exercise, repeat this calculation for the eight-peg puzzle, which has four red pegs and four blue pegs, separated by a single empty space. Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 15 / 19 A 2n-peg puzzle More generally, consider the puzzle that consists of n red pegs, and n blue pegs, placed a horizontal row of 2n C 1 spaces, which leaves one empty space between each group. The initial state of this 2n-peg puzzle is represented by the list, .R1 R2 Rn E B1 B2 Bn /: Applying our algebraic analysis, each peg must traverse n C 1 spaces to reach its final position. Since there are 2n pegs, the total distance is D.n/ D 2n.n C 1/: (Note that we have introduced mathematical function notation, D.n/, in place of D , to emphasize that value of the distance depends on the number of pegs.) Likewise, a little thought should convince you that number of jumps required is J.n/ D n2 ; as each of the n red peg must some how cross over (or under) the n blue pegs. From our distance formula, D.n/ D 2J.n/ C S.n/ we obtain S.n/ D D.n/ 2J.n/ D 2n.n C 1/ 2n2 D 2n: Thus, the total number of moves required to complete the 2n-peg puzzle is M.n/ D J.n/ C S.n/ D n2 C 2n: Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 16 / 19 A 2n-peg puzzle (cont.) Here we tabulate some values of S.n/ D 2n, J.n/ D n2 , and M.n/ D J.n/ C S.n/, vs. n: n S.n/ J.n/ M.n/ n S.n/ J.n/ M.n/ 1 2 3 4 5 6 7 8 2 4 6 8 10 12 14 16 1 4 9 16 25 36 49 64 3 8 15 24 35 48 63 80 9 10 20 30 40 50 100 1000 18 20 40 60 80 100 200 2000 81 100 400 900 1600 2500 10000 1000000 99 120 440 960 1680 2600 10200 1002000 Notice that for sufficiently large n, M.n/ J.n/ D n2 . Looking at the behavior of a quantity as one or more of its parameters steadily increases is called asymptotic analysis. In this case we would say that both quantities M and J increase quadratically with respect to n, as both grow like k n2 for some suitable choice of the multiplicative constant k . (In this case, k D 1.) The particular value of k is usually much less important than the factor of n2 . Thus it is convenient to ignore it, and summarize the asymptotic dependence by writing M.n/ O.n2 /. Likewise, one could state that S.n/ O.n/, as S increases linearly with respect to n. Fine Print: By convention, the notation M.n/ O.n2 / usually means that M.n/ has an asymptotic quadratic upper bound, i.e., for sufficiently large n, there exists a constant k such that M.n/ k n2 . Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 17 / 19 Two-dimensional peg solitaire There are several popular versions of two-dimensional peg solitaire. A very simple one is an extension of our previous puzzle. Consider the following top-view of a puzzle with 8 red pegs, 8 blue pegs, placed in a board with 17 spaces, leaving exactly one empty space in the middle: As before, the goal of the puzzle is to interchange the colors, with the restriction that red pegs can only move to the right or down, and the blue pegs can only move to the left, or up. Likewise pegs can either step one space into the empty space, or can jump over a single peg of either color into the empty space. Can you find a solution? Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 18 / 19 Study Problems 1 2 3 4 How many different ways can n red pegs and n blue pegs be placed on a board that contains 2n C 1 spaces? Write a scheme function of one integer argument, called initial-state that will generate the initial state for an n peg puzzle. Consequently, the expression (initial-state 1) should evaluate to (R E B), (initial-state 2) to (R R E B B), etc. Write a scheme function of a single argument, called legal? that returns #t if the argument represents a legal state of a 2n peg puzzle, for some value of n; and #f, otherwise. Thus, (legal? ’(R E B R B)) and (legal? ’(B E R)) should both return #t, but (legal? ’(E R)), (legal? ’(R R B B)), (legal? ’(R E R E B B)), and (legal? ’(R R E B R) return #f. Note that a legal state is not necessarily accessible. Consider the two-dimensional puzzle with 8 red pegs and 8 blue pegs. (a) How many total states are there? (Include both accessible and inaccessible states.) Please express your answer as an integer. (b) This puzzle is known to have a 48 move solution. How many moves in this solution are jumps? (c ) Can you find a 48 move solution? Can you find a shorter solution? 5 We define a two-dimensional jigsaw puzzle to be regular if and only if the matches are unique; that is two pieces fit together if and only if the are neighboring pieces in the solved puzzle. Note that most picture jigsaw puzzles have this property. Letting n denote the number of pieces in the puzzle, show that the amount of time required to solve a regular jigsaw puzzle scales as O.n2 /. Robert R. Snapp © 2012 10. Peg Solitaire CS 32, Fall 2012 19 / 19
© Copyright 2026 Paperzz