Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. package com.example.hellojni;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class JavaSolver {
  7.     private final static int base = 12;
  8.     private final static int[][] moves = {{1, -2}, {2, -1}, {2, 1}, {1, 2}, {-1, 2},
  9.             {-2, 1}, {-2, -1}, {-1, -2}};
  10.     private static int[][] board;
  11.     private static int total;
  12.  
  13.     private static int[][] formatGrid() {
  14.         int[][] newGrid = new int[8][8];
  15.  
  16.         for(int i = 0; i < 8; i++) {
  17.             for(int j = 0; j < 8; j++) {
  18.                 newGrid[i][j] = board[i+2][j+2];
  19.             }
  20.         }
  21.         return newGrid;
  22.     }
  23.  
  24.     public static int[][] createJavaSolution(int startRow, int startCol) {
  25.         // Create board and populate it, boardsize is set to 2 wider than the actual board to
  26.         // not have to do collision detection
  27.         board = new int[base][base];
  28.         total = (base - 4) * (base - 4);
  29.  
  30.         for (int r = 0; r < base; r++)
  31.             for (int c = 0; c < base; c++)
  32.                 if (r < 2 || r > base - 3 || c < 2 || c > base - 3)
  33.                     board[r][c] = -1;
  34.  
  35.         int row = 2 + startRow;
  36.         int col = 2 + startCol;
  37.  
  38.         board[row][col] = 1;
  39.  
  40.         solve(row, col, 2);
  41.         return formatGrid();
  42.  
  43.     }
  44.  
  45.     private static boolean solve(int row, int col, int turn) {
  46.         if (turn > total)
  47.             return true;
  48.  
  49.         int[][] numbers = neighborsArray(row,col);
  50.  
  51.         int[] bestMove = new int[] {0,0,9};
  52.         for (int i = 0; i < 8; i++) {
  53.             if (numbers[i][2] < bestMove[2]) {
  54.                 bestMove[0] = numbers[i][0];
  55.                 bestMove[1] = numbers[i][1];
  56.             }
  57.         }
  58.         row = bestMove[0];
  59.         col = bestMove[1];
  60.         board[row][col] = turn;
  61.         if (solve(row, col, turn + 1))
  62.             return true;
  63.         board[row][col] = 0;
  64.  
  65.         return false;
  66.     }
  67.  
  68.     private static List<int[]> neighbors(int row, int col) {
  69.         List<int[]> numbers = new ArrayList<>();
  70.  
  71.         for (int i = 0; i < 8; i++) {
  72.             int x = moves[i][0];
  73.             int y = moves[i][1];
  74.             if (board[row + y][col + x] == 0) {
  75.                 int num = countNeighbors(row + y, col + x);
  76.                 numbers.add(new int[]{row + y, col + x, num});
  77.             }
  78.         }
  79.         return numbers;
  80.     }
  81.     private static int[][] neighborsArray(int row, int col) {
  82.         int[][] numbers = new int[8][3];
  83.  
  84.         for (int i = 0; i < 8; i++) {
  85.             int x = moves[i][0];
  86.             int y = moves[i][1];
  87.             if (board[row + y][col + x] == 0) {
  88.                 int num = countNeighbors(row + y, col + x);
  89.                 numbers[i][0] = row;
  90.                 numbers[i][1] = col;
  91.                 numbers[i][2] = num;
  92.             }
  93.             else {
  94.                 numbers[i][0] = row;
  95.                 numbers[i][1] = col;
  96.                 numbers[i][2] = 9;
  97.             }
  98.         }
  99.         return numbers;
  100.     }
  101.  
  102.     private static int countNeighbors(int row, int col) {
  103.         int num = 0;
  104.         for (int i = 0; i < 8; i++)
  105.             if (board[row + moves[i][1]][col + moves[i][0]] == 0)
  106.                 num++;
  107.         return num;
  108.     }
  109. }
  110.  
  111. // http://pastebin.com/LSqEHPu0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement