Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package queenscombinations;
  7.  
  8. /**
  9. *
  10. * @author Demid
  11. */
  12. public class QueensCombinations {
  13.  
  14. final static int N = 8;
  15.  
  16. static int combinationsCount;
  17.  
  18. /**
  19. * Sets value to the position only if it's current value equals currentValue
  20. */
  21. static void setIf(int[][] board, int x, int y, int value, int currentValue) {
  22. if (board[x][y] == currentValue) {
  23. board[x][y] = value;
  24. }
  25. }
  26.  
  27. /**
  28. * Puts/removes the Queen on the position. Marks/unmarks all position on
  29. * rows below that are under attack by this Queen
  30. */
  31. static void toggleQueen(int[][] board, int x, int y) {
  32. int value, opposite;
  33. if (board[x][y] == 0) {
  34. value = x + 1;
  35. opposite = 0;
  36. } else {
  37. value = 0;
  38. opposite = x + 1;
  39. }
  40.  
  41. for (int i = x; i < N; i++) {
  42. setIf(board, i, y, value, opposite);
  43. }
  44.  
  45. //main diagonal
  46. int difference = Math.abs(x - y);
  47. int length = N - difference;
  48. if (x <= y) {
  49. for (int i = x + 1; i < length; i++) {
  50. setIf(board, i, difference + i, value, opposite);
  51. }
  52. } else {
  53. for (int j = y + 1; j < length; j++) {
  54. setIf(board, difference + j, j, value, opposite);
  55. }
  56. }
  57. //secondary diagonal
  58. int sum = x + y;
  59. if (sum < N) {
  60. for (int j = 0; j <= y - 1; j++) {
  61. setIf(board, sum - j, j, value, opposite);
  62. }
  63. } else {
  64. for (int i = N - 1; i >= x + 1; i--) {
  65. setIf(board, i, sum - i, value, opposite);
  66. }
  67. }
  68. }
  69.  
  70. /**
  71. * Recursion with backtracking. Puts the Queen on each position in a row and
  72. * calls itself on below row.
  73. */
  74. static int processRow(int[][] board, int row) {
  75. for (int i = 0; i < N; i++) {
  76. if (board[row][i] == 0) {
  77. if (row == N - 1) {
  78. //print(board);
  79. combinationsCount++;
  80. } else {
  81. toggleQueen(board, row, i);
  82. processRow(board, row + 1);
  83. toggleQueen(board, row, i);
  84. }
  85. }
  86. }
  87. return combinationsCount;
  88. }
  89.  
  90. /**
  91. * main method
  92. *
  93. * @param args the command line arguments
  94. */
  95. public static void main(String[] args) {
  96. int[][] board = new int[N][N];
  97. System.out.println(processRow(board, 0));
  98. }
  99.  
  100. public static void print(int[][] a) {
  101. System.out.println();
  102. for (int i = 0; i < N; i++) {
  103. System.out.println();
  104. for (int j = 0; j < N; j++) {
  105. System.out.print(Math.abs(a[i][j]) + " ");
  106. }
  107. }
  108.  
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement