import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; /** * Solution to Haybales * * @author vanb */ public class haybales_vanb { private static BufferedReader br; private static PrintStream ps; /** This is bigger than any number of jumps we'll see */ private static final int INFINITY = 10000; /** * This records a position on the field and how many jumps it took to get there. * For rows, it will record the column number, and for columns, it will record the row number. * * @author vanb */ private class From implements Comparable { /** The number of jumps it took to get here */ public int jumps; /** Where we are - the row number or column number */ public int where; /** * Create one of these. * * @param jumps The number of jumps it took to get here * @param where Where we are - the row number or column number */ public From( int jumps, int where ) { this.jumps = jumps; this.where = where; } @Override /** * Compare this From with another. * * @param other Another of these * @return >0 if this one is 'better' than the other, <0 if it's 'worse', 0 if they'e the same. */ public int compareTo( From other ) { // Smaller number of jumps is obviously better. int diff = jumps - other.jumps; // Same number of jumps? The the larger position is better. // That will be closer to the spot we're trying to move to. if( diff==0 ) diff = other.where - where; return diff; } } /** * Do it! * * @throws Exception */ private void doit() throws Exception { String tokens[] = br.readLine().trim().split( "\\s+" ); int n = Integer.parseInt( tokens[0] ); int k = Integer.parseInt( tokens[1] ); //System.err.println( n + " " + k ); // Read the field char field[][] = new char[n][]; for( int i=0; i columns[] = new PriorityQueue[n]; for( int i=0; i(k); PriorityQueue row = new PriorityQueue(k); // We start at square 0,0, so we know how many jumps it takes (zero). // So, no need to compute it. field[0][0] = 'X'; int best = INFINITY; // For each row... for( int i=0; i