알고리즘 단련장/소프티어
[소프티어] 진정한 효도 레벨2 자바 풀이
dcho
2024. 10. 28. 21:39
SMALL
https://softeer.ai/practice/7374
처음 이 문제를 접했을때 어떻게 풀어야지 하는 막연함이 존재했다.
하지만 조금씩 생각을 하면서 어떻게 하면 좋을지 하다가 결국 가로, 세로 각 3개씩 생각을 하니 단순해 보였다.
하지만 모든 케이스를 다 뒤지면서 어거지로 풀긴 했으나 너무 비효율적으로 먼저 풀었다.
import java.io.*;
import java.util.StringTokenizer;
public class Main {
// 풀이 1 (내 풀이)
public static int dap = 3;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int[][] arr = new int[3][3];
for (int i = 0; i < 3; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int j = 0; j < 3; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
// 3 개가 모두 같을때 쓰는 flag
int flag = 0;
// 가로 축 확인
for (int i = 0; i < 3; i++) {
// 3개가 모두 같은 경우
if (arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2]) {
flag = 1;
break;
}
// 같지 않은 경우
else {
// 1 ~ 3 숫자 반복
for (int j = 1; j < 4; j++) {
int total = 0;
// 반복을 돌면서 1,2,3에 모두 맞춰 보고 차이를 기록
for (int k = 0; k < 3; k++) {
int temp = j - arr[i][k];
if (temp < 0) {
temp *= -1;
}
total += temp;
}
if (total < dap) {
dap = total;
}
}
}
}
// 세로 축 확인
for (int i = 0; i < 3; i++) {
if (arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i]) {
flag = 1;
break;
}
else {
for (int j = 1; j < 4; j++) {
int total = 0;
for (int k = 0; k < 3; k++) {
int temp = j - arr[i][k];
if (temp < 0) {
temp *= -1;
}
total += temp;
}
if (total < dap) {
dap = total;
}
}
}
}
// 모두 같은 경우가 있는 경우 분기 처리
if (flag == 1) {
bw.write(0 + "\n");
}
else {
bw.write(dap + "\n");
}
bw.flush();
bw.close();
}
}
친구가 풀었던 풀이를 보고 어떻게 문제를 접근하는지 생각하게 되었고 어떻게 문제를 풀면 좋은지 조언도 같이 얻었다. (동희야 고맙다)
https://snapcode.tistory.com/52
전체적으로 어떻게 하면 좋을지 크게 생각해보고 케이스를 나눠서 보니 반복을 많이 돌지 않아도 해결이 되었다.
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static int arr[][];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
arr = new int[3][3];
// 111 > 0
// 121 > 1개를 나머지 2개에 맞추기
// 123 > 무조건 비용 2
for (int i = 0; i < 3; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < 3; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
// 가로
int rowMin = 2;
for (int i = 0; i < 3; i++) {
int tempMin = -1;
int r1 = arr[i][0];
int r2 = arr[i][1];
int r3 = arr[i][2];
if (r1 == r2 && r2 == r3) tempMin = 0;
else if (r1 == r2 || r1 == r3 || r2 == r3) {
if (r1 == r2) tempMin = Math.abs(r1-r3);
else if (r1 == r3) tempMin = Math.abs(r1-r2);
else tempMin = Math.abs(r1-r3);
}
else tempMin = 2;
if (rowMin > tempMin) rowMin = tempMin;
}
// 세로
int colMin = 2;
for (int i = 0; i < 3; i++) {
int tempMin = -1;
int c1 = arr[0][i];
int c2 = arr[1][i];
int c3 = arr[2][i];
if (c1 == c2 && c1 == c3) tempMin = 0;
else if (c1 == c2 || c1 == c3 || c2 == c3) {
if (c1 == c2) tempMin = Math.abs(c1-c3);
else if (c1 == c3) tempMin = Math.abs(c1-c2);
else tempMin = Math.abs(c1-c3);
}
else tempMin = 2;
if (colMin > tempMin) colMin = tempMin;
}
bw.write(Math.min(rowMin, colMin) + "\n");
bw.flush();
bw.close();
}
}
직접 구현하지 않고도 잘 가져다 쓸 수 있는 Math 클래스도 잘 활용해야겠다.