2503숫자 야구

LJM·2023년 1월 13일
0

백준풀기

목록 보기
30/259

https://www.acmicpc.net/problem/2503

어떻게 구현해야 할지 방향이 안잡혀서 다른 풀이를 보고 한참 생각하다 이해가 되어서 풀었다

민혁이가 시도한 숫자와 스트라이크, 볼 개수 를 완전탐색으로 생성한 숫자와 비교하면 된다
비교해서 스트라이크, 볼 개수가 모두 일치하는 (생성된)숫자의 개수를 카운트하면 답이 나온다.

숫자 생성은 완전탐색 방식으로 중복된 숫자없이 생성하면 된다 111 121 131 이런건 안됨
123
124
125
126
127
128
129
132
133
134
~~
이런식으로 만들었고 저 숫자들을 민혁이가 시도한 숫자와 비교해서 스트라이크, 볼 개수를 카운트하고 민혁이가 시도한 숫자의 스트라이크, 볼 개수와 일치하는지 확인하면 된다

예를 들어 완전탐색 방식으로 142를 만들었다고 해보자 이것을 다음의 그림과 같이 민혁이가 시도한 숫자인 123과 비교해서 S(스트라이크) 와 B(볼) 을 카운트하면 1S, 1B가 나온다
이것은 123 의 1S, 1B와 일치하므로 다음 숫자인 356 과 비교한다.
142 와 356 은 0S, 0B 이므로 일치하지 않으므로 142는 정답후보에서 제외하고 다음 숫자를 시도한다.

import java.io.*;
import java.util.*;

public class Main {

    static class Hint
    {
        int[] hintNum;
        int stCnt;
        int baCnt;
        Hint()
        {
            hintNum = new int[3];
        }
    }

    static int tryCnt;
    static Hint[] hint;

    static int[] tryNum = new int[3];
    static boolean[] checked = new boolean[9];

    static int answer = 0;

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

        tryCnt = Integer.parseInt(br.readLine());
        hint = new Hint[tryCnt];

        String[] input;
        for(int i = 0; i < tryCnt; ++i)
        {
            input = br.readLine().split(" ");

            hint[i] = new Hint();

            for(int j = 0; j < 3; ++j)
            {
                hint[i].hintNum[j] = input[0].charAt(j) - '0';
            }

            hint[i].stCnt = Integer.parseInt(input[1]);
            hint[i].baCnt = Integer.parseInt(input[2]);
        }

        search(0);

        System.out.println(answer);
    }

    static void search(int level)
    {
        if(level == 3)
        {
            if(tryMethod(tryNum))
                answer++;
            return;
        }

        for(int i = 0; i < 9; ++i)
        {
            if(checked[i]==false)
            {
                checked[i] = true;
                tryNum[level] = i+1;
                search(level+1);
                checked[i] = false;
            }
        }
    }

    static boolean tryMethod(int[] tryNum)
    {
        boolean ret = true;

        int strikeCnt = 0;
        int ballCnt = 0;
        for(int i = 0; i < tryCnt; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                for (int k = 0; k < 3; ++k)
                {
                    if (hint[i].hintNum[j] == tryNum[k])
                    {
                        if (j == k)
                            strikeCnt++;
                        else
                            ballCnt++;
                    }
                }
            }
            if (hint[i].stCnt == strikeCnt && hint[i].baCnt == ballCnt)
            {
                //No operation
            }
            else
            {
                ret = false;
                break;
            }
            strikeCnt = 0;
            ballCnt = 0;
        }
        return ret;
    }

#### }
profile
게임개발자 백엔드개발자

0개의 댓글