import java.util.*; /** * Homer's Broken Remote Control * * This program generates an optimal sequence to get from one channel * to another, assuming a remote control with some broken digit keys. * * For the UMD High School Programming Contest, 2008. * @author Dave Mount */ public class BrokenRemote { static final int MAX_POSSIBLE = 9999; // maximum possible channel static int maxChannel; // maximum channel number static int nWorkingDigits; // number of working digit keys static boolean[] isWorking; // true if i-th digit key works static int startChannel; // starting channel number static int targetChannel; // desired target channel number /** Main program. * * Reads inputs, invokes the algorithm, and outputs the answer. */ public static void main(String[] args) throws Exception { Scanner scanner = new Scanner( System.in ); // input remote state maxChannel = scanner.nextInt(); if (maxChannel > MAX_POSSIBLE) { System.out.println("Error: Maximum cannot exceed " + MAX_POSSIBLE); System.exit(0); } nWorkingDigits = scanner.nextInt(); isWorking = new boolean[10]; for (int i = 0; i < nWorkingDigits; i++) { int thisDigit = scanner.nextInt(); if (thisDigit < 0 || thisDigit > 9) { System.out.println("Error: Digit out of range"); System.exit(0); } isWorking[thisDigit] = true; } // echo state System.out.print("Max Channel: " + maxChannel); System.out.print(" Working Digits: "); for (int i = 0; i < 10; i++) { if (isWorking[i]) { System.out.print(i + " "); } } System.out.println(); // input start/target pairs while (scanner.hasNextInt()) { startChannel = scanner.nextInt(); targetChannel = scanner.nextInt(); if (startChannel < 1 || startChannel > maxChannel || targetChannel < 1 || targetChannel > maxChannel) { System.out.println("Error: Either start or target out of range"); System.exit(0); } // generate the optimal sequence String sequence = generateSequence(); // output final sequence System.out.println("Start: " + startChannel + " Target: " + targetChannel + " Sequence: " + makePrintable(sequence)); } } /** Convert sequence into printable form * * Sequences of '+' or '-' that are longer than 5 characters * printed as [99+] or [99-], where 99 is the repetition factor. * * As a side effect, we check that the sequence contains only valid * characters, and produce an error message if not. */ private static String makePrintable(String sequence) { final int MAX_REPEAT = 5; StringBuffer outputString = new StringBuffer(""); int n = sequence.length(); boolean isValid = true; int i = 0; while (i < n) { char ch = sequence.charAt(i); if (!Character.isDigit(ch) && ch != 'E' && ch != '+' && ch != '-') { isValid = false; } if (ch == '+' || ch == '-') { // check for repeats of + or - int j = i+1; while (j < n && sequence.charAt(j) == ch) j++; if (j-i > MAX_REPEAT) { outputString.append("[" + (j-i) + ch + "]"); } else { outputString.append(sequence.substring(i, j)); } i = j; } else { outputString.append(sequence.charAt(i)); i++; } } if (!isValid) { System.out.println("Error: Final sequence can only contain digits, 'E', '+', and '-'"); } return new String(outputString); } /** Generates the optimal remote control sequence * * This uses the global quantities maxChannel, startChannel, * targetChannel, nWorkingDigits, and isWorking[], defined above. * * The result is stored as a string, containing the digits '0'-'9' * along with 'E' (for Enter), '+' (up channel), or '-' (down * channel). */ private static String generateSequence() { String result = ""; /* ------------------- INSERT CODE HERE ---------------------*/ /* -------------------- END OF INSERTION --------------------*/ return result; } }