Problem
We define a magic square to be an matrix of distinct positive integers from to where the sum of any row, column, or diagonal of length is always equal to the same number: the magic constant.
You will be given a matrix of integers in the inclusive range . We can convert any digit to any other digit in the range at cost of . Given , convert it into a magic square at minimal cost. Print this cost on a new line.
Note: The resulting magic square must contain distinct integers in the inclusive range .
For example, we start with the following matrix :
5 3 4
1 5 8
6 4 2
We can convert it to the following magic square:
8 3 4
1 5 9
6 7 2
This took three replacements at a cost of .
Function Description
Complete the formingMagicSquare function in the editor below. It should return an integer that represents the minimal total cost of converting the input square to a magic square.
formingMagicSquare has the following parameter(s):
- s: a array of integers
Input Format
Each of the lines contains three space-separated integers of row .
Constraints
Output Format
Print an integer denoting the minimum cost of turning matrix into a magic square.
Sample Input 0
4 9 2
3 5 7
8 1 5
Sample Output 0
1
Explanation 0
If we change the bottom right value, , from to at a cost of , becomes a magic square at the minimum possible cost.
Sample Input 1
4 8 2
4 5 7
6 1 6
Sample Output 1
4
Explanation 1
Using 0-based indexing, if we make
- -> at a cost of
- -> at a cost of
- -> at a cost of ,
then the total cost will be .
How I solved the problem
# 조금 어려워서 찾아보니
# 위에서는 3 x 3 으로 제한해놨기 때문에 총 8개의 가능성이 있음!
# 각각의 경우의 수와 받은 변수 사이에 차이를 구한 다음 min 값을 찾아야함
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { // Complete the formingMagicSquare function below. static int formingMagicSquare(int[][] s) { // 3 x 3 magic square //possible magicsquare is 8 int[][] magic0 = {{8,1,6}, {3,5,7}, {4,9,2}}; int[][] magic1 = {{6,1,8}, {7,5,3}, {2,9,4}}; int[][] magic2 = {{4,9,2}, {3,5,7}, {8,1,6}}; int[][] magic3 = {{2,9,4}, {7,5,3}, {6,1,8}}; int[][] magic4 = {{8,3,4}, {1,5,9}, {6,7,2}}; int[][] magic5 = {{4,3,8}, {9,5,1}, {2,7,6}}; int[][] magic6 = {{6,7,2}, {1,5,9}, {8,3,4}}; int[][] magic7 = {{2,7,6}, {9,5,1}, {4,3,8}}; int[] sum = new int[8]; //compare given parameter and possible magicsquare for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { sum[0] += Math.abs(s[i][j] - magic0[i][j]); sum[1] += Math.abs(s[i][j] - magic1[i][j]); sum[2] += Math.abs(s[i][j] - magic2[i][j]); sum[3] += Math.abs(s[i][j] - magic3[i][j]); sum[4] += Math.abs(s[i][j] - magic4[i][j]); sum[5] += Math.abs(s[i][j] - magic5[i][j]); sum[6] += Math.abs(s[i][j] - magic6[i][j]); sum[7] += Math.abs(s[i][j] - magic7[i][j]); } } int min = sum[0]; for (int i = 0; i < sum.length; i++) { if (min > sum[i]) { min = sum[i]; } } return min; } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); int[][] s = new int[3][3]; for (int i = 0; i < 3; i++) { String[] sRowItems = scanner.nextLine().split(" "); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); for (int j = 0; j < 3; j++) { int sItem = Integer.parseInt(sRowItems[j]); s[i][j] = sItem; } } int result = formingMagicSquare(s); bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); bufferedWriter.close(); scanner.close(); } } | cs |
[출처 : https://www.hackerrank.com ]
'1 Day 1 Algorithms' 카테고리의 다른 글
[2019.02.25] Climbing the Leaderboard (0) | 2019.02.25 |
---|---|
[2019.02.22] Picking Numbers (0) | 2019.02.22 |
[2019.02.19] Cats and a Mouse (0) | 2019.02.19 |
[2019.02.18] Electronics Shop (0) | 2019.02.18 |
[2019.02.15] Drawing Book (0) | 2019.02.15 |
댓글