백준 13413 java : 구현

magicdrill·2025년 7월 23일
0

백준 문제풀이

목록 보기
639/654

백준 13413 java : 구현

initdest를 한 글자씩 비교하며 둘이 차이 나는 경우, 흑돌과 백돌 수를 따로 저장한다. 이후 흑돌과 백돌이 서로 상쇄하고 남은 개수를 통해 연산을 하려했는데, 저장한 값 중 더 큰 값이 정답과 같은 규칙을 발견했다.
처음 구상한 (백돌과 흑돌이 서로 상쇄하는 개수) + (남은 돌 개수)은 어차피 둘 중 더 큰 값과 동일하기 때문에 연산을 줄일 수 있다.

import java.util.Scanner;

public class BJ13413 {
    static Scanner sc = new Scanner(System.in);

    static class Osello{
        int tokenNum;
        String init;
        String dest;

        Osello (int tokenNum, String init, String dest){
            this.tokenNum = tokenNum;
            this.init = init;
            this.dest = dest;
        }
    }

    static Osello [] testCase;

    public static void main(String[] args) {
        inputData();
        findAnswer();
    }

    public static void inputData(){
        int i;

        testCase = new Osello[sc.nextInt()];
        for(i = 0; i < testCase.length; i++){
            Osello osello = new Osello(sc.nextInt(), sc.next(), sc.next());
            testCase[i] = osello;
        }

        sc.close();
    }

    public static void findAnswer(){
        int i, j;

        for(i = 0; i < testCase.length; i++){
            int answer = 0, BCount = 0, WCount = 0;

            //init과 dest를 비교
            //init에서 BCount와 WCount를 각각 계산
            //BCount와 WCount 차이만큼?

            for(j = 0; j < testCase[i].init.length(); j++){
                if(testCase[i].init.charAt(j) != testCase[i].dest.charAt(j)){
                    if(testCase[i].init.charAt(j) == 'B'){
                        BCount++;
                    }
                    else{
                        WCount++;
                    }
                }
            }

            System.out.println("BCount : " + BCount + " / WCount : " + WCount);
            System.out.println(Math.max(BCount, WCount));
        }
    }
}

0개의 댓글