Saturday, 25 February 2017

Chapter 29 Exercise 4, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

29.4 (Modify weight in the nine tails problem) In the text, we assign the number of the flips as the weight for each move. Assuming that the weight is three times
of the number of flips, revise the program.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Exercise04 {

public static void main(String[] args) {
// Prompt the user to enter nine coins' Hs and Ts
System.out.print("Enter an initial nine coins' Hs and Ts: ");
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
String s = input.nextLine();
char[] initialNode = s.toCharArray();

WeightedNineTailModel model = new WeightedNineTailModel();
java.util.List<Integer> path = model.getShortestPath(NineTailModel
.getIndex(initialNode));

System.out.println("The steps to flip the coins are ");
for (int i = 0; i < path.size(); i++)
NineTailModel.printNode(NineTailModel.getNode(path.get(i).intValue()));

System.out.println(