Advertisement
KennasSticky

Magic Square

Aug 10th, 2020
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class magicsquare {
  5.  
  6.     private static int[] xCol, yCol, boardVal;
  7.     private static int[][] board;
  8.     private static ArrayList distinct;
  9.  
  10.     public static void main(String[] args) {
  11.         Scanner scan = new Scanner(System.in);
  12.         int size = scan.nextInt();
  13.         board = new int[size][size];
  14.  
  15.         // read user input into the board array
  16.         for (int i = 0; i < size;i++){
  17.             for (int j = 0; j < size; j++){
  18.                 board[i][j] = scan.nextInt();
  19.             }
  20.         }
  21.  
  22.         checkSquare();
  23.     }
  24.  
  25.     private static void checkSquare(){
  26.         xCol = new int[board.length];
  27.         yCol = new int[board.length];
  28.  
  29.         // calculate row and column totals
  30.         for (int i = 0; i < board.length; i++){
  31.             for (int j = 0; j < board.length; j++){
  32.                 xCol[i] += board[i][j];
  33.                 yCol[i] += board[j][i];
  34.             }
  35.         }
  36.         if (isMagic()){
  37.             System.out.println("MAGIC");
  38.         } else {
  39.             notMagic();
  40.         }
  41.     }
  42.  
  43.     private static boolean isMagic(){
  44.  
  45.         boardVal = combineArray();
  46.  
  47.         int first = boardVal[0];
  48.  
  49.         // check if each value in the array is the same
  50.         for (int i = 1; i < boardVal.length; i++) {
  51.             if (first != boardVal[i]) return false;
  52.         }
  53.  
  54.         return true;
  55.     }
  56.  
  57.     private static void notMagic(){
  58.  
  59.        distinct = new ArrayList<>();
  60.  
  61.        for (int i = 0; i < boardVal.length; i++){
  62.            if (!distinct.contains(boardVal[i])){
  63.                distinct.add(boardVal[i]);
  64.            }
  65.        }
  66.  
  67.        // more than 2 distinct values results in a NOT MAGIC SQUARE
  68.        if (distinct.size() - 1 == 1){
  69.            System.out.println("ALMOST MAGIC");
  70.            System.out.println(isAlmostSquare());
  71.        } else {
  72.            System.out.println("NOT MAGIC");
  73.        }
  74.     }
  75.  
  76.     private static int getIndex(int[] arr, int t){
  77.         for (int i = 0; i < arr.length; i++){
  78.             if (arr[i] == t){
  79.                 return i;
  80.             }
  81.         }
  82.         return -1;
  83.     }
  84.  
  85.     private static String isAlmostSquare(){
  86.  
  87.         int distCount1 = 0;
  88.         int distCount2 = 0;
  89.  
  90.         // calculate pos of odd value
  91.         int distVal1 = (int) distinct.get(0);
  92.         int distVal2 = (int) distinct.get(1);
  93.  
  94.         for (int i = 0; i < boardVal.length; i++){
  95.             if (boardVal[i] == distVal1){
  96.                 distCount1++;
  97.             } else if (boardVal[i] == distVal2){
  98.                 distCount2++;
  99.             }
  100.         }
  101.  
  102.         int almostVal = distCount1 > distCount2 ? distVal2 : distVal1, desiredVal = distCount1 < distCount2 ? distVal2 : distVal1;
  103.  
  104.         int r = getIndex(xCol, almostVal);
  105.         int c = getIndex(yCol, almostVal);
  106.         int v = desiredVal - (almostVal - board[r][c]);
  107.  
  108.         return (r + 1) + " " + (c + 1) + " " + v;
  109.     }
  110.  
  111.     private static int[] combineArray(){
  112.         int[] combinedArr = new int[xCol.length * 2];
  113.  
  114.         System.arraycopy(xCol, 0, combinedArr, 0, xCol.length);
  115.         System.arraycopy(yCol, 0, combinedArr, xCol.length, yCol.length);
  116.  
  117.         return combinedArr;
  118.     }
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement