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("CHECK")) { boolean sorted = isSolution(puzzle); if (sorted) { System.out.println("SORTED"); } else { System.out.println("NOT SORTED"); } } else if (s.equals("SWAP")) { int target1 = lsc.nextInt(); int target2 = lsc.nextInt(); swapVals(puzzle, target1, target2); } else if (s.equals("MOVE")) { do { String step = lsc.next(); int neighbors[] = findNeighbors2(0); if (step.equals("up")) { if (neighbors[0] < 0) { System.out.println("FAILED"); return; } swapVals(puzzle, 0, neighbors[0]); } else if (step.equals("left")) { if (neighbors[1] < 0) { System.out.println("FAILED"); return; } swapVals(puzzle, 0, neighbors[1]); } else if (step.equals("right")) { if (neighbors[2] < 0) { System.out.println("FAILED"); return; } swapVals(puzzle, 0, neighbors[2]); } else if (step.equals("down")) { if (neighbors[3] < 0) { System.out.println("FAILED"); return; } swapVals(puzzle, 0, neighbors[3]); } else { System.out.println("Unknown MOVE = " + step); } } while (lsc.hasNext()); System.out.println("MOVED"); } 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[]) { // all values must be in sorted order for (int i = 0; i < (puzzleTiles) - 2; i++) { if (p[i] > p[i+1]) return false; } return true; } public static void solvePuzzle(int steps) { } public static void swapVals(int p[], int v1, int v2) { for (int i = 0; i < (puzzleTiles); i++) { if (p[i] == v1) p[i] = v2; else if (p[i] == v2) p[i] = v1; } } 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("findNeighbors2 ERROR, target 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 int[] findNeighbors2(int target) { int i; for (i = 0; i < (puzzleTiles); i++) { if (puzzle[i] == target) break; } if (i == puzzleTiles) { System.out.println("findNeighbors2 ERROR, target not found"); return null; } int row = i/puzzleSize; int col = i%puzzleSize; int neighbors[] = new int[4]; // top neighbor if ((row - 1) >= 0) { neighbors[0] = puzzle[i-puzzleSize]; } else neighbors[0] = -1; // left neighbor if ((col - 1) >= 0) { neighbors[1] = puzzle[i-1]; } else neighbors[1] = -1; // right neighbor if ((col + 1) < puzzleSize) { neighbors[2] = puzzle[i+1]; } else neighbors[2] = -1; // bottom neighbor if ((row + 1) < puzzleSize) { neighbors[3] = puzzle[i+puzzleSize]; } else neighbors[3] = -1; return neighbors; } public static void printPuzzle(int p[]) { for (int i = 0; i