[HackerRank] Forming a Magic Square

아르당·2024년 6월 26일
0

HackerRank

목록 보기
108/109
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

마방진이란 1부터 n^2까지의 서로다른 양의 정수로 구성된 n n 행렬로서, 길이가 n인 어떤 행, 열 또는 대각선의 합이 항상 같은 수인 마법 상수와 같아지는 것을 말한다.
당신은 범위가 1부터 9까지의 정수로 구성된 3
3 행렬 s를 받게 될 것이다. 우리는 범위가 1부터 9까지의 어떤 숫자 a를 다른 숫자 b로 변환할 수 있고, 그 비용은 |a - b|이다. 주어진 s를 최소 비용으로 마방진으로 변환해라. 최소 비용을 출력해라.
NOTE: 결과로 나오는 마방진은 범위가 1부터 9까지 서로 다른 정수들로 구성되어야 한다.

Example

s = [[5, 3, 4], [1, 5, 8], [6, 4, 2]]

이 행렬은 아래와 같다.

5 3 4
1 5 8
6 4 2

우리는 마방진을 다음과 같이 변환할 수 있다.

8 3 4
1 5 9
6 7 2

3개를 변환했고, 비용은 |5 - 8| + |8 - 9| + |4 - 7| = 7이 된다.

Function Description

formingMagicSquare 함수를 완성해라.
formingMagicSquare 함수는 아래와 같은 매개변수를 가지고 있다.

  • int s[3][3]: 정수로 된 3 * 3 배열

Return

  • int: 마방진으로 변환할 최소 비용의 합

Constraints

  • s[i][j] ∈ [1, 9]

Solved

마방진이 되는 경우를 먼저 찾아준다.

List<int[][]> squareList = new ArrayList<>();
int[][] a1 = {{8, 1, 6}, {3, 5, 7}, {4, 9, 2}};
int[][] a2 = {{6, 1, 8}, {7, 5, 3}, {2, 9, 4}};
int[][] a3 = {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}};
int[][] a4 = {{2, 9, 4}, {7, 5, 3}, {6, 1, 8}};
int[][] a5 = {{8, 3, 4}, {1, 5, 9}, {6, 7, 2}};
int[][] a6 = {{4, 3, 8}, {9, 5, 1}, {2, 7, 6}};
int[][] a7 = {{6, 7, 2}, {1, 5, 9}, {8, 3, 4}};
int[][] a8 = {{2, 7, 6}, {9, 5, 1}, {4, 3, 8}};
squareList.add(a1);
squareList.add(a2);
squareList.add(a3);
squareList.add(a4);
squareList.add(a5);
squareList.add(a6);
squareList.add(a7);
squareList.add(a8);

리스트로 주어진 리스트를 정수형 배열로 형변환한다.

int[][] s1 = new int[s.size()][];

for (int i = 0; i < s.size(); i++) {
	List<Integer> rowList = s.get(i);
	s1[i] = new int[rowList.size()];
    
	for (int j = 0; j < rowList.size(); j++) {
		s1[i][j] = rowList.get(j);
	}
}

형변환한 배열을 마방진 경우와 비교하며 최소 비용을 찾아서 반환한다.

int minCost = Integer.MAX_VALUE;
for (int[][] a : squareList) {
	int cost = 0;
	for (int i = 0; i < 3 ; i++) {
		for (int j = 0 ; j < 3; j++) {
			cost += Math.abs(s1[i][j] - a[i][j]);
		}
	}

	if (cost < minCost) {
		minCost = cost;
	}
}

return minCost;

All Code

public static int formingMagicSquare(List<List<Integer>> s) {
	List<int[][]> squareList = new ArrayList<>();
	int[][] a1 = {{8, 1, 6}, {3, 5, 7}, {4, 9, 2}};
	int[][] a2 = {{6, 1, 8}, {7, 5, 3}, {2, 9, 4}};
	int[][] a3 = {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}};
	int[][] a4 = {{2, 9, 4}, {7, 5, 3}, {6, 1, 8}};
	int[][] a5 = {{8, 3, 4}, {1, 5, 9}, {6, 7, 2}};
	int[][] a6 = {{4, 3, 8}, {9, 5, 1}, {2, 7, 6}};
	int[][] a7 = {{6, 7, 2}, {1, 5, 9}, {8, 3, 4}};
	int[][] a8 = {{2, 7, 6}, {9, 5, 1}, {4, 3, 8}};
	squareList.add(a1);
    squareList.add(a2);
    squareList.add(a3);
    squareList.add(a4);
    squareList.add(a5);
    squareList.add(a6);
    squareList.add(a7);
    squareList.add(a8);

	int[][] s1 = new int[s.size()][];
	for (int i = 0; i < s.size(); i++) {
		List<Integer> rowList = s.get(i);
		s1[i] = new int[rowList.size()];

		for (int j = 0; j < rowList.size(); j++) {
			s1[i][j] = rowList.get(j);
		}
	}

	int minCost = Integer.MAX_VALUE;
	for (int[][] a : squareList) {
		int cost = 0;
		for (int i = 0; i < 3 ; i++) {
			for (int j = 0 ; j < 3; j++) {
				cost += Math.abs(s1[i][j] - a[i][j]);
			}
		}

		if (cost < minCost) {
			minCost = cost;
		}
	}

	return minCost;
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글