[BOJ] 2503 - 숫자 야구

suhyun·2022년 10월 24일
0

백준/프로그래머스

목록 보기
28/81

문제 링크

2503-숫자 야구


입력

첫째 줄에는 민혁이가 영수에게 몇 번이나 질문을 했는지를 나타내는 1 이상 100 이하의 자연수 N이 주어진다. 이어지는 N개의 줄에는 각 줄마다 민혁이가 질문한 세 자리 수와 영수가 답한 스트라이크 개수를 나타내는 정수와 볼의 개수를 나타내는 정수, 이렇게 총 세 개의 정수가 빈칸을 사이에 두고 주어진다.


출력

첫 줄에 영수가 생각하고 있을 가능성이 있는 답의 총 개수를 출력한다.


문제 풀이

브루트포스 알고리즘으로 풀 수 있는 문제

서로 다른 숫자 세개로 구성된 숫자라고 했으니깐
boolean배열은 0부터 확인안하고 123부터 987까지만 확인하면 됨.

package Algorithm.Silver3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Code2503 {

    static int num, strike, ball, result = 0;
    static boolean[] nums = new boolean[988];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int n = Integer.parseInt(st.nextToken());

        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            int num = Integer.parseInt(st.nextToken());
            int strike = Integer.parseInt(st.nextToken());
            int ball = Integer.parseInt(st.nextToken());

            checkSB(num, strike, ball);
        }

        int cnt = 0;
        for (int i = 123; i < 988; i++) {
            if(!nums[i]) cnt++;
        }
        System.out.println(cnt);
    }

    static void checkSB (int num, int strike, int ball) {
        int a = num / 100;
        int b = (num % 100) / 10;
        int c = num % 10;

        for (int i = 123; i < 988; i++) {
            if(nums[i]) continue;
            int strike2 = 0, ball2 = 0;
            int a2 = i / 100;
            int b2 = (i % 100) / 10;
            int c2 = i % 10;

            if (a2 == 0 || b2 == 0 || c2 == 0 || a2 == b2 || a2 == c2 || b2 == c2) {
                nums[i] = true;
                continue;
            }

            if(a == a2) strike2++;
            if(b == b2) strike2++;
            if(c == c2) strike2++;

            if(a == b2 || a == c2) ball2++;
            if(b == a2 || b == c2) ball2++;
            if(c == b2 || c == a2) ball2++;

            if (strike != strike2 || ball != ball2) {
                nums[i] = true;
            }
        }


    }
}
profile
꾸준히 하려고 노력하는 편 💻

0개의 댓글