import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.PrintStream; import java.util.Random; import java.util.Scanner; /** * Sliding Puzzle * * For the UMD High School Programming Contest, 2009. * @author Chau-Wen Tseng */ public class Puzzle { static final int maxPuzzle = 1000; // size of puzzle static int puzzleSize = 1; // size of puzzle static int puzzleTiles; // number of tiles & 1 space in puzzle static int puzzle[]; public static void main(String[] args) throws Exception { long time; int rowNum = 0; int row[] = new int [maxPuzzle]; int idx; time = System.currentTimeMillis(); time = System.currentTimeMillis() - time; // System.out.println("Solve Time = "+(time/1000)); // read size of puzzle Scanner sc = new Scanner( System.in ); String line = sc.nextLine(); Scanner lsc = new Scanner( line ); puzzleSize = lsc.nextInt(); puzzleTiles = puzzleSize*puzzleSize; puzzle = new int[puzzleTiles]; // read contents of puzzle line = sc.nextLine(); lsc = new Scanner( line ); for (int i = 0; i< puzzleTiles; i++) { puzzle[i] = lsc.nextInt(); } // perform commands while (sc.hasNextLine()) { line = sc.nextLine(); lsc = new Scanner( line ); // skip past empty lines if (!lsc.hasNext()) continue; String s = lsc.next(); if (s.equals("SHOW")) { printPuzzle(puzzle); } else if (s.equals("NEIGHBORS")) { int target = lsc.nextInt(); int neighbors[] = findNeighbors(target); for (int i = 0; i< neighbors.length; i++) { System.out.print(neighbors[i] + " "); } System.out.println(); } else if (s.equals("SOLUTION")) { boolean solved = isSolution(puzzle); System.out.println(solved); } else if (s.equals("DIRECTIONS")) { int target = lsc.nextInt(); takeSteps(target); } else if (s.equals("SOLVE")) { int maxSteps = lsc.nextInt(); solvePuzzle(maxSteps); } else { System.out.println("Illegal input " + s); return; } } } public static boolean isSolution(int p[]) { // values must be in sorted order, except for last position for (int i = 0; i < (puzzleTiles) - 2; i++) { if (p[i] > p[i+1]) return false; } // last position in puzzle must be 0 return (p[puzzleTiles-1] == 0); } public static int row_col_to_pos(int size, int r, int c) { return (r*size)+c; } public static int pos_to_row(int size, int pos) { return pos/size; } public static int pos_to_col(int size, int pos) { return pos % size; } public static int[] findNeighbors(int target) { int i; for (i = 0; i < (puzzleTiles); i++) { if (puzzle[i] == target) break; } if (i == puzzleTiles) { System.out.println("Not found"); return null; } int row = i/puzzleSize; int col = i%puzzleSize; int numNeighbors = 0; int neighbors[] = new int[4]; // top neighbor if ((row - 1) >= 0) { neighbors[numNeighbors] = puzzle[i-puzzleSize]; numNeighbors++; } // left neighbor if ((col - 1) >= 0) { neighbors[numNeighbors] = puzzle[i-1]; numNeighbors++; } // right neighbor if ((col + 1) < puzzleSize) { neighbors[numNeighbors] = puzzle[i+1]; numNeighbors++; } // bottom neighbor if ((row + 1) < puzzleSize) { neighbors[numNeighbors] = puzzle[i+puzzleSize]; numNeighbors++; } int retVal[] = new int[numNeighbors]; for (int k = 0; k < numNeighbors; k++) { retVal[k] = neighbors[k]; } return retVal; } public static void takeSteps(int target) { } public static void solvePuzzle(int maxSteps) { } public static void printPuzzle(int p[]) { for (int i = 0; i