What’s the Output?
The following program presents a more sophisticated method for calculating the solvability of a starting configuration for the Fifteen Puzzle of Section 7.10. Trace through the program by hand, and count how many times the statements length+ + and numSwaps + = length execute. What is the output?
public class SolvableGame{ // counts number of swaps necessary to restore the values in a[1]..a[15] to 1,2,...,15 public static void main(String[ ] args) { int numSwaps = 0; // the board is stored in a[1]..a[15]; a[0] is not considered, -1 is a dummy value int[ ] a = {−1, 5, 6, 3, 12, 2, 1, 7, 4, 9, 8, 15, 13, 10, 11, 14}; for (int i = 1; iRepeat the problem again after changing the line:
int[ ] a = {−1, 5, 6, 3, 12, 2, 1, 7, 4, 9, 8, 15, 13, 10, 11, 14} to:
i. int[ ] a = {−1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
ii. int[ ] a = {−1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 14}
Using the data of (i) and (ii), compare the number of steps taken by this method with the more intuitive countInversions(...) method used in the case study of Section 7.10. The number of steps required by countInversions(...) is the number of times the if statement on lines 43–44 executes plus the number of times line 45 executes.
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.