import java.io.*; import java.util.*; import java.awt.geom.*; /** * Solution to You Win! * * We'll solve this by keeping a bitmap of which letters we've entered so far, and which we haven't. * We'll go backwards, so we'll start with (binary) 111..., and end up with 000... * * @author vanb */ public class youwin_vanb { public Scanner sc; public PrintStream ps; // Memoization array to keep track of the best we can do to get to // a certain bitmap, also knowing the position last letter entered. public int memo[][] = new int[20][1<<20]; // The target Name public char name[]; /** * What's the best number of moves to get to a certain state, * where a state is represented by a bitmap showing which letters have been entered, * and an integer showing the position of the last letter entered? * * @param bits Bitmap showing which letters have been entered * @param last The position of the last letter entered * @return The least number of moves needed to get to this state. */ public int moves( int bits, int last ) { // This will hold a count of the bits // from position 0 to here. // That's the same as the number of letters entered so far. int counts[] = new int[20]; // No need to do this if we've already computed it. if( memo[last][bits]<0 ) { // Count up the bits counts[0] = bits&1; for( int i=1; i0?1:0); } int best = Integer.MAX_VALUE; // If 'last' was the position of the last letter entered, // then this bitmap represents the letters remaining to be // accounted for. int remaining = bits^(1<13 ) cost = 26-cost; // Add 1 to account for pressing 'FIRE' // to enter the letter best = cost+1; } else { // Go through the positions of the Name for( int i=0; i13 ) diff = 26-diff; // Add in the diff, plus one to account for 'FIRE' cost += diff+1; // Add in the cost to get to the remaining state cost += moves( remaining, i ); // And voila if( cost