Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.46 KB | None | 0 0
  1. import java.util.Arrays;
  2. public class lily{
  3.     private static int nextRow, nextCol;
  4.     private static int height = 20;
  5.     private static int width = 3;
  6.     private static int numused = 0;
  7.     private static int[][] board = new int[height][width];
  8.     private static final int numOfPieces = 12;
  9.     private static final boolean[] used = new boolean[numOfPieces+1];//we need 13 because we're not using the index 0 one. we will be using 1-12
  10.     private  static final int[][] pieces = {
  11.             { 1, 0,1,0,2,0,3,0,4 },  // Describes piece I in its horizontal orientation.
  12.             { 1, 1,0,2,0,3,0,4,0 },  // Describes piece I in its vertical orientation.
  13.             { 2, 1,-1,1,0,1,1,2,0 }, // Describes piece X in its only orientation.
  14.             { 3, 0,1,1,0,2,-1,2,0 },
  15.             { 3, 1,0,1,1,1,2,2,2 },
  16.             { 3, 0,1,1,1,2,1,2,2 },
  17.             { 3, 1,-2,1,-1,1,0,2,-2 },
  18.             { 4, 1,0,2,0,2,1,2,2 },
  19.             { 4, 0,1,0,2,1,0,2,0 },
  20.             { 4, 1,0,2,-2,2,-1,2,0 },
  21.             { 4, 0,1,0,2,1,2,2,2 },
  22.             { 5, 0,1,0,2,1,1,2,1 },
  23.             { 5, 1,-2,1,-1,1,0,2,0 },
  24.             { 5, 1,0,2,-1,2,0,2,1 },
  25.             { 5, 1,0,1,1,1,2,2,0 },
  26.             { 6, 1,0,1,1,2,1,2,2 },
  27.             { 6, 1,-1,1,0,2,-2,2,-1 },
  28.             { 6, 0,1,1,1,1,2,2,2 },
  29.             { 6, 0,1,1,-1,1,0,2,-1 },
  30.             { 7, 0,1,0,2,1,0,1,2 },
  31.             { 7, 0,1,1,1,2,0,2,1 },
  32.             { 7, 0,2,1,0,1,1,1,2 },
  33.             { 7, 0,1,1,0,2,0,2,1 },
  34.             { 8, 1,0,1,1,1,2,1,3 },
  35.             { 8, 1,0,2,0,3,-1,3,0 },
  36.             { 8, 0,1,0,2,0,3,1,3 },
  37.             { 8, 0,1,1,0,2,0,3,0 },
  38.             { 8, 0,1,1,1,2,1,3,1 },
  39.             { 8, 0,1,0,2,0,3,1,0 },
  40.             { 8, 1,0,2,0,3,0,3,1 },
  41.             { 8, 1,-3,1,-2,1,-1,1,0 },
  42.             { 9, 0,1,1,-2,1,-1,1,0 },
  43.             { 9, 1,0,1,1,2,1,3,1 },
  44.             { 9, 0,1,0,2,1,-1,1,0 },
  45.             { 9, 1,0,2,0,2,1,3,1 },
  46.             { 9, 0,1,1,1,1,2,1,3 },
  47.             { 9, 1,0,2,-1,2,0,3,-1 },
  48.             { 9, 0,1,0,2,1,2,1,3 },
  49.             { 9, 1,-1,1,0,2,-1,3,-1 },
  50.             { 10, 1,-2,1,-1,1,0,1,1 },
  51.             { 10, 1,-1,1,0,2,0,3,0 },
  52.             { 10, 0,1,0,2,0,3,1,1 },
  53.             { 10, 1,0,2,0,2,1,3,0 },
  54.             { 10, 0,1,0,2,0,3,1,2 },
  55.             { 10, 1,0,1,1,2,0,3,0 },
  56.             { 10, 1,-1,1,0,1,1,1,2 },
  57.             { 10, 1,0,2,-1,2,0,3,0 },
  58.             { 11, 1,-1,1,0,1,1,2,1 },
  59.             { 11, 0,1,1,-1,1,0,2,0 },
  60.             { 11, 1,0,1,1,1,2,2,1 },
  61.             { 11, 1,0,1,1,2,-1,2,0 },
  62.             { 11, 1,-2,1,-1,1,0,2,-1 },
  63.             { 11, 0,1,1,1,1,2,2,1 },
  64.             { 11, 1,-1,1,0,1,1,2,-1 },
  65.             { 11, 1,-1,1,0,2,0,2,1 },
  66.             { 12, 0,1,1,0,1,1,2,1 },
  67.             { 12, 0,1,0,2,1,0,1,1 },
  68.             { 12, 1,0,1,1,2,0,2,1 },
  69.             { 12, 0,1,1,-1,1,0,1,1 },
  70.             { 12, 0,1,1,0,1,1,1,2 },
  71.             { 12, 1,-1,1,0,2,-1,2,0 },
  72.             { 12, 0,1,0,2,1,1,1,2 },
  73.             { 12, 0,1,1,0,1,1,2,0 }
  74.     };
  75.     public static boolean putPiece(int p, int row, int col){
  76.  
  77.         for (int i = 1; i < 8; i += 2){//first check if all the coordinates are on the board to prevent ArraysOutOfBounds
  78.             //if it's on board we can then check the values
  79.             if((row + pieces[p][i])>=height || (col+pieces[p][i+1])>=width || col+pieces[p][i+1] < 0 || row+pieces[p][i] < 0)
  80.                 return false;
  81.             if (board[row + pieces[p][i]][col+pieces[p][i+1]] != 0) {// one of the block needed is filled already
  82.                 return false;
  83.             }
  84.         }
  85.         //now place the piece to the board
  86.         board[row][col] = pieces[p][0];//change the value of the first block
  87.  
  88.         for (int i = 1; i < 8; i += 2){
  89.             board[row + pieces[p][i]][col+pieces[p][i+1]] = pieces[p][0];  //we assign the piece value to the 4 other empty blocks
  90.         }
  91.         return true;
  92.  
  93.     }
  94.     public static void removePiece(int p, int row, int col) {
  95.         for (int i = 1; i < 8; i += 2){
  96.             board[row][col] = 0;
  97.             board[row + pieces[p][i]][col+pieces[p][i+1]] = 0;
  98.         }
  99.     }
  100.     public static void printBoard(){
  101.         for(int i=0; i<height; i++){
  102.             for(int j=0; j<width; j++){//convert the numbers to letters
  103.                 if(board[i][j]==1)System.out.print("I");
  104.                 if(board[i][j]==2)System.out.print("X");
  105.                 if(board[i][j]==3)System.out.print("Z");
  106.                 if(board[i][j]==4)System.out.print("V");
  107.                 if(board[i][j]==5)System.out.print("T");
  108.                 if(board[i][j]==6)System.out.print("W");
  109.                 if(board[i][j]==7)System.out.print("U");
  110.                 if(board[i][j]==8)System.out.print("L");
  111.                 if(board[i][j]==9)System.out.print("N");
  112.                 if(board[i][j]==10)System.out.print("Y");
  113.                 if(board[i][j]==11)System.out.print("F");
  114.                 if(board[i][j]==12)System.out.print("P");
  115.                 if(board[i][j]==0)System.out.print(0);
  116.  
  117.                 System.out.print(" ");
  118.             }
  119.             System.out.println();
  120.         }
  121.     }
  122.  
  123.     public static void solve(int row, int col) {   // recursive procedure that tries to solve the puzzle
  124.  
  125.         for (int p=0; p<63; p++){
  126.  
  127.             if ((used[pieces[p][0]] == false) && putPiece(p, row, col)){  // try piece p
  128.                 // a piece has been placed on the board.
  129.  
  130.                 used[pieces[p][0]] = true;
  131.                 numused++;
  132.  
  133.                 if (numused == numOfPieces) {  // puzzle is solved if used = pieces
  134.                     System.out.println("Solution found:");
  135.                     printBoard();
  136.                     System.out.println(Arrays.toString(used));
  137.                     System.exit(0);
  138.                 }
  139.                 else {//find the next empty block to fill
  140.                     for(int i=height-1; i>=0; i--){
  141.                         for(int j=width-1; j>=0; j--){
  142.                             if (board[i][j]==0){
  143.                                 nextRow = i;
  144.                                 nextCol = j;
  145.                             }
  146.                         }
  147.                     }
  148.                     solve(nextRow, nextCol);  // try to complete the solution
  149.                 }
  150.                 removePiece(p, row, col);  // backtrack
  151.                 numused--;
  152.                 used[pieces[p][0]] = false;
  153.             }
  154.         }
  155.     }
  156.  
  157.  
  158.  
  159.     public static void main(String[] args){
  160.  
  161.         solve(0,0);
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement