개발자 톡

연습문제 톡 진정한 효도

테케 4, 8, 10, 11 에서 틀렸습니다가 나오는데 이유를 모르겠어요. 도움부탁드립니다.

등록일
2024-02-13 00:37:37
조회수
327
작성자
castela0119
import java.io.*;
import java.util.*;


public class Main {


	public static void main(String[] args) throws IOException {


		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int[][] arrs = new int[3][3];


		for (int i = 0; i < 3; i++) {


			StringTokenizer st = new StringTokenizer(br.readLine());


			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			int c = Integer.parseInt(st.nextToken());


			arrs[i][0] = a;
			arrs[i][1] = b;
			arrs[i][2] = c;
		}
		
		// System.out.println(Arrays.deepToString(arrs));


		int minCost = 100;


		// 가로 탐색
		for (int i=0; i<3; i++) {


			int oneCnt = 0;
			int twoCnt = 0;
			int threeCnt = 0;


			int cost = 10;


			for(int j=0; j<3; j++) {
				if (arrs[i][j] == 1) {
					oneCnt += 1;
				} else if (arrs[i][j] == 2) {
					twoCnt += 1;
				} else if (arrs[i][j] == 3) {
					threeCnt += 1;
				}
			}


			if (oneCnt == twoCnt && twoCnt == threeCnt) {
				// System.out.println("0");
				minCost = 0;
			} else {
				if (oneCnt == 2) {
					if (twoCnt == 1) {
						cost = 1;
					} else if (threeCnt == 1) {
						cost = 2;
					}
				} else if (twoCnt == 2) {
					if (oneCnt == 1) {
						cost = 1;
					} else if (threeCnt == 1) {
						cost = 1;
					}
				} else if (threeCnt == 2) {
					if (oneCnt == 1) {
						cost = 2;
					} else if (twoCnt == 1) {
						cost = 1;
					}
				}


				if (minCost >= cost) {
					minCost = cost;
				}
			}
		}
		
		// 세로 탐색
		for (int j=0; j<3; j++) {


			int oneCnt = 0;
			int twoCnt = 0;
			int threeCnt = 0;


			int cost2 = 10;


			for(int i=0; i<3; i++) {
				if (arrs[i][j] == 1) {
					oneCnt += 1;
				} else if (arrs[i][j] == 2) {
					twoCnt += 1;
				} else if (arrs[i][j] == 3) {
					threeCnt += 1;
				}
			}
			


			if (oneCnt == twoCnt && twoCnt == threeCnt) {
				// System.out.println("0");
				minCost = 0;
			} else {
				if (oneCnt == 2) {
					if (twoCnt == 1) {
						cost2 = 1;
					} else if (threeCnt == 1) {
						cost2 = 2;
					}
				} else if (twoCnt == 2) {
					if (oneCnt == 1) {
						cost2 = 1;
					} else if (threeCnt == 1) {
						cost2 = 1;
					}
				} else if (threeCnt == 2) {
					if (oneCnt == 1) {
						cost2 = 2;
					} else if (twoCnt == 1) {
						cost2 = 1;
					}
				}


				if (minCost >= cost2) {
					minCost = cost2;
				}
			}
		}
		System.out.println(minCost);
	}
}
#진정한_효도

이 카테고리의 톡 더보기