Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. public class MagicSquare {
  2. final int square[][];
  3. final boolean used[];
  4. final int n;
  5. final int magicSum;
  6. int total = 0;
  7.  
  8. public MagicSquare(int n) {
  9. square = new int[n][n];
  10. this.n = n;
  11. used = new boolean[n*n+1];
  12. magicSum = n*(n*n+1)/2;
  13. }
  14.  
  15. // handles only rows and columns
  16. boolean validUpTo(int step) {
  17. for (int r = 0; r < n; r++) {
  18. if (step == (r+1)*n-1) {
  19. int sum = 0;
  20. for (int c = 0; c < n; c++) { sum += square[r][c]; }
  21. return (sum == magicSum);
  22. }
  23. }
  24.  
  25. for (int c = 0; c < n; c++) {
  26. if (step == n*(n-1)+c) {
  27. int sum = 0;
  28. for (int r = 0; r < n; r++) { sum += square[r][c]; }
  29. return (sum == magicSum);
  30. }
  31. }
  32.  
  33. return true;
  34. }
  35.  
  36. boolean isValid() {
  37. int sumD1 = 0;
  38. int sumD2 = 0;
  39. for (int i = 0; i < n; i++) {
  40. sumD1 += square[i][i];
  41. sumD2 += square[i][n-i-1];
  42. }
  43.  
  44. return (sumD1 == magicSum && sumD2 == magicSum );
  45. }
  46.  
  47. boolean solve(int step) {
  48. if (step == n*n) {
  49. return isValid();
  50. }
  51.  
  52. for (int val = 1; val <= n*n; val++) {
  53. if (used[val]) { continue; }
  54.  
  55. used[val] = true;
  56. square[step/n][step%n] = val;
  57. if (validUpTo(step) && solve(step+1)) {
  58. return true;
  59. }
  60. square[step/n][step%n] = 0;
  61. used[val] = false;
  62. }
  63.  
  64. return false;
  65. }
  66.  
  67.  
  68.  
  69. public void outputSolution () {
  70. for (int r = 0; r < n; r++) {
  71. for (int c = 0; c < n; c++) {
  72. System.out.print(square[r][c]);
  73. System.out.print(' ');
  74. }
  75. System.out.println();
  76. }
  77. System.out.println();
  78. }
  79.  
  80. void count(int step) {
  81. if (step == n*n) {
  82. if (isValid()) {
  83. total++;
  84. outputSolution();
  85. }
  86. return;
  87. }
  88.  
  89. for (int val = 1; val <= n*n; val++) {
  90. if (used[val]) { continue; }
  91.  
  92. used[val] = true;
  93. square[step/n][step%n] = val;
  94. if (validUpTo(step)) {
  95. count (step+1);
  96. }
  97. square[step/n][step%n] = 0;
  98. used[val] = false;
  99. }
  100. }
  101. }
  102.  
  103. //MAIN
  104.  
  105.  
  106.  
  107. public class Main {
  108. public static void main(String[] args) {
  109. long startTime = System.currentTimeMillis();
  110. MagicSquare m = new MagicSquare(4);
  111. m.solve(0);
  112. m.outputSolution();
  113.  
  114. m = new MagicSquare(4);
  115. m.count(0);
  116. System.out.println("There are " + m.total + " possible squares.");
  117.  
  118. long stopTime = System.currentTimeMillis();
  119. long elapsedTime = stopTime - startTime;
  120. System.out.println(elapsedTime);
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement