import java.io.*; import java.util.*; import java.awt.*; import java.text.*; import java.math.*; /** * Solution to Block Game. * * It's just a Breadth-First Search, keeping track of previously seen states. */ class blockgame { public Scanner sc; public PrintStream ps; public String toString() { return "blockgame"; } // The change in i and j for each of the four directions. // d=0 is up, d=1 is right, d=2 is down, d=3 is left. public static final int di[] = { -1, 0, 1, 0 }; public static final int dj[] = { 0, 1, 0, -1 }; // The (fixed) board size. public int n=6, m=6; /** * This is a State in the Breadth-First Search. * A State consists of the current configuration of the board, and the move number. */ public class State { // The board public char board[][]; // The move number public int move; /** * Build a state. * * @param b The Board * @param mm The Move Number */ public State( char b[][], int mm ) { // Remember move number move = mm; // Build a new board board = new char[n][]; for( int i=0; i=0 && j>=0 && i queue = new LinkedList(); // This will record states we've seen HashSet seen = new HashSet(); // Put the first state on the queue, // and say that we've seen it State state = new State( board, 0 ); queue.add( state ); seen.add( state.getID() ); // This will hold the final answer int solution = -1; // Keep going until the queue is empty, // or until a solution is found while( queue.size()>0 && solution<0 ) { // Get the state at the top of the queue, // and get its board state = queue.removeFirst(); char b[][] = state.getBoard(); // Look to see if the special piece has // been moved to the last row. If so, it can be // moved off, and the board is solved. for( int i=0; i