Question: Linear Disk Movement In this section, you will investigate the movement of disks on a linear grid. The starting configuration of this puzzle is a

Linear Disk Movement

In this section, you will investigate the movement of disks on a linear grid.

The starting configuration of this puzzle is a row ofLcells, with disks located on cells0throughn - 1. The goal is to move the disks to the end of the row using a constrained set of actions. At each step, a disk can only be moved to an adjacent empty cell, or to an empty cell two spaces away, provided another disk is located on the intervening square. Given these restrictions, it can be seen that in many cases, no movements will be possible for the majority of the disks. For example, from the starting position, the only two options are to move the last disk from celln - 1to celln, or to move the second-to-last disk from celln - 2to celln.

Write a function solve_identical_disks(length, n) that returns an optimal solution to the above problem as a list of moves, where length is the number of cells in the row and n is the number of disks. Each move in the solution should be a two-element tuple of the form (from, to) indicating a disk movement from the first cell to the second. As suggested by its name, this function should treat all disks as being identical.

Your solver for this problem should be implemented using a breadth-first graph search. The exact solution produced is not important, as long as it is of minimal length.

Unlike in the previous two sections, no requirement is made with regards to the manner in which puzzle configurations are represented. Before you begin, think carefully about which data structures might be best suited for the problem, as this choice may affect the efficiency of your search.

>>> solve_identical_disks(4, 2)

[(0, 2), (1, 3)]

>>> solve_identical_disks(5, 2)

[(0, 2), (1, 3), (2, 4)]

>>> solve_identical_disks(4, 3)

[(1, 3), (0, 1)]

>>> solve_identical_disks(5, 3)

[(1, 3), (0, 1), (2, 4), (1, 2)]

Write a function solve_distinct_disks(length, n) that returns an optimal solution to the same problem with a small modification: in addition to moving the disks to the end of the row, their final order must be the reverse of their initial order. More concretely, if we abbreviate length asL, then a desired solution moves the first disk from cell0to cellL - 1, the second disk from cell1to cellL - 2,\\cdots, and the last disk from celln - 1to cellL - n.

Your solver for this problem should again be implemented using a breadth-first graph search. As before, the exact solution produced is not important, as long as it is of minimal length.

>>> solve_distinct_disks(4, 2)

[(0, 2), (2, 3), (1, 2)]

>>> solve_distinct_disks(5, 2)

[(0, 2), (1, 3), (2, 4)]

>>> solve_distinct_disks(4, 3)

[(1, 3), (0, 1), (2, 0), (3, 2), (1, 3),

(0, 1)]

>>> solve_distinct_disks(5, 3)

[(1, 3), (2, 1), (0, 2), (2, 4), (1, 2)]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!