Guest User

Untitled

a guest
Apr 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. /**
  2. * @(#)Knight.java
  3. *
  4. *
  5. * @author
  6. * @version 1.00 2009/12/10
  7. */
  8. import java.util.ArrayList;
  9. public class Knight {
  10.  
  11. /**
  12. * Creates a new instance of <code>Knight</code>.
  13. */
  14. static int g_sqSize = 5;
  15. static int[][] g_board;
  16.  
  17. public static boolean InRangeAndEmpty(int y, int x){
  18. return (y>=0 && x >=0 && y < g_sqSize && x < g_sqSize && g_board[y][x] == 0);
  19. }
  20.  
  21. public static void PrintBoard(){
  22. //int scale = ((String)(g_sqSize*g_sqSize)).length;
  23. System.out.println("________________");
  24. for (int[] line : g_board){
  25. for (int element : line){
  26. System.out.print(element + " ");
  27. }
  28. System.out.println();
  29. }
  30. }
  31.  
  32. public static void Fill(int y, int x, int counter){
  33. //assume g_board[y][x] == 1;
  34. g_board[y][x] = counter; //Fill initial
  35. if (counter == g_sqSize*g_sqSize){ //Check if this is the last square
  36. PrintBoard();
  37. return;
  38. }
  39.  
  40. ArrayList<ArrayList> emptyNeighbors = new ArrayList<ArrayList>();; //Calculate empty neighbors
  41. int[][] jumps = {
  42. {-2, 1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}
  43. };
  44. for (int[] jump : jumps){
  45. int _y = y+jump[0];
  46. int _x = x+jump[1];
  47. int[] __xy = {_y, _x};
  48. ArrayList<Integer> _xy = new ArrayList<Integer>();
  49. for (int _elem : __xy){
  50. _xy.add(_elem);
  51. }
  52. if (InRangeAndEmpty(_y, _x)){
  53. emptyNeighbors.add(_xy);
  54. }
  55.  
  56. }
  57. PrintBoard();
  58. // Recurse with each neightbor, heuristically weigh those with least # of free neighbors
  59. // 1 for neigh
  60. for (ArrayList<Integer> neighbor : emptyNeighbors){
  61. Fill(neighbor.get(0), neighbor.get(1), counter+1);
  62. }
  63.  
  64. g_board[y][x] = 0;
  65. }
  66.  
  67.  
  68.  
  69. public static void main(String[] args) {
  70. // TODO code application logic here
  71. //Fill with 0
  72. g_board = new int[g_sqSize][g_sqSize];
  73. for (int i=0;i < g_sqSize;i++){
  74. for(int r = 0; r < g_sqSize; r++){
  75. g_board[i][r] = 0;
  76. }
  77. }
  78. Fill(0,0,1);
  79. }
  80.  
  81. }
Add Comment
Please, Sign In to add comment