import java.io.*; import java.util.*; import java.awt.geom.*; /** * Solution to Word Ladder * * We'll use Best-First Search, also known as Dijkstra's algorithm, * to find the shortest path from the start word to the target word. * * @author vanb */ public class ladder_vanb { public Scanner sc; public PrintStream ps; /** Distance between words (distance = # letters different) */ public int dists[][]; /** The words themselves, stored as char arrays */ public char words[][]; /** * A state in a Best-First Search * * @author vanb */ public class State implements Comparable { /** The distance to get to this state (distance = # syteps ) */ public int distance; /** Index of the word here */ public int word; /** The word we added to get here (or "0" if no word added) */ public String add; /** The state we came from. This isn't necessary, but it's useful for debugging. */ public State from; /** * Create a State. * * @param distance # of steps to get to this state * @param word Index of work * @param from State we came from * @param add Word we added (or "0") */ public State( int distance, int word, State from, String add ) { this.distance = distance; this.word = word; this.add = add; this.from = from; } @Override /** * Compare this State to another, first by distance, then by added word. * * @param s Another State * @return Standard for compareTo */ public int compareTo( State s ) { int diff = distance - s.distance; if( diff==0 ) diff = add.compareTo( s.add ); return diff; } /** * A Pretty String for debugging. * * @return A pretty String */ public String toString() { return "[" + distance + " " + new String(words[word]) + " <" + add + ">]"; } } /** * Driver. * @throws Exception */ public void doit() throws Exception { sc = new Scanner( System.in ); ps = System.out; // Read in n, allocate arrays int n = sc.nextInt(); dists = new int[n][n]; words = new char[n][]; // Read in the words in the dictionary for( int i=0; i pq = new PriorityQueue(); // The starting state - word 0, distance 0, no added word. pq.add( new State( 0, 0, null, "0" ) ); // This is the default solution if we can't get there from here. State solution = new State( -1, 1, null, "0" ); // BestFS! (Also known as Dijkstra's algorithm) while( !pq.isEmpty() ) { State s = pq.poll(); // Have we used our prerogative to add a word yet? int used = s.add.charAt( 0 ) == '0' ? 0 : 1; // If we've already visited this word, go to the next state. if( visited[used][s.word] ) continue; // Mark it as visited visited[used][s.word]= true; // Have we gotten to the target word? // Are we done? if( s.word == 1 ) { solution = s; break; } // Go through all the words to see where we can go next for( int i=0; iwords[s.word][k] ) { newword[k] = words[s.word][k]; usei = true; } } // Add the new state! pq.add( new State( s.distance+2, i, s, new String(newword)) ); } } } // Print the solution! ps.println( solution.add ); ps.println( solution.distance ); } /** * @param args */ public static void main( String[] args ) throws Exception { new ladder_vanb().doit(); } }