코테/백준

[백준/JAVA] 2503번: 숫자 야구

imname1am 2024. 2. 6. 23:55
반응형

📖 문제

 

2503번: 숫자 야구

첫째 줄에는 민혁이가 영수에게 몇 번이나 질문을 했는지를 나타내는 1 이상 100 이하의 자연수 N이 주어진다. 이어지는 N개의 줄에는 각 줄마다 민혁이가 질문한 세 자리 수와 영수가 답한 스트

www.acmicpc.net

 

 

💡  풀이 방식

• 브루트포스

- 123 이상 987 이하의 숫자들 중에 대해 중복되는 경우와 0이 들어간 경우를 제외하고 반복문을 돌려 조건을 만족하는 숫자를 찾는다.

 

 

💥 유의사항

제한사항 : 중복숫자가 들어있거나, 0이 들어있는 경우

 

 

🔺 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.*;
import java.io.*;
 
public class Main {
    static int N;
    static Node[] arr;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        
        N = Integer.parseInt(br.readLine());
        
        arr = new Node[N];
        for(int i = 0 ; i < N ; i++) {
            st = new StringTokenizer(br.readLine(), " ");
            
            int n = Integer.parseInt(st.nextToken());
            int s = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            
            arr[i] = new Node(n, s, b);
        }
        
        System.out.println(calBaseball());
    }
    
    private static int calBaseball() {
        int result = 0;
        
        for(int i = 123 ; i <= 987 ; i++) {
            if(!isSame(i))  continue;   // 중복숫자 처리
            
            int allTestPass = 0;
            
            for(int j = 0 ; j < N ; j++) {
                int sCnt = 0;
                int bCnt = 0;
                
                Node now = arr[j];
                String curStr = Integer.toString(now.num);
                String myDataStr = Integer.toString(i);
                
                // 스트라이크 확인
                for(int idx = 0 ; idx < 3 ; idx++) {
                    if(curStr.charAt(idx) == myDataStr.charAt(idx))
                        sCnt++;
                }
                
                // 볼 확인
                for(int b = 0 ; b < 3 ; b++) {
                    for(int c = 0 ; c < 3 ; c++) {
                        if(b == c) continue;
                        if(curStr.charAt(c) == myDataStr.charAt(b)) {
                            bCnt++;
                        }
                    }
                }
                
                if(now.strike == sCnt && now.ball == bCnt) {
                    allTestPass++;
                }
            }
            
            if(allTestPass == N) {
                result++;
            }
        }
        
        return result;
    }
    
    private static boolean isSame(int number) {
        String numStr = Integer.toString(number);
        
        if(numStr.charAt(0== numStr.charAt(1))    return false;
        if(numStr.charAt(1== numStr.charAt(2))    return false;
        if(numStr.charAt(0== numStr.charAt(2))    return false;
        if(numStr.charAt(0== '0' || numStr.charAt(1== '0' || numStr.charAt(2== '0')    return false;
        
        return true;
    }
}
 
class Node {
    int num, strike, ball;
    
    public Node(int num, int strike, int ball) {
        this.num = num;
        this.strike = strike;
        this.ball = ball;
    }
}
 
cs

 

 

 

➕ 다른 풀이 방식

숫자 야구에 나올 수 있는 숫자를 dfs로 만들어 푸셨다.

https://tussle.tistory.com/912

 

[백준] 알고리즘 분류(브루트 포스,JAVA)2503번, 숫자 야구

문제 링크 2503번: 숫자 야구 첫째 줄에는 민혁이가 영수에게 몇 번이나 질문을 했는지를 나타내는 1 이상 100 이하의 자연수 N이 주어진다. 이어지는 N개의 줄에는 각 줄마다 민혁이가 질문한 세

tussle.tistory.com


💦 어려웠던 점

그냥 완전탐색으로 123~999 사이에 있는 숫자들 중에서 스트라이크랑 볼 갯수 세서 만족하는지 확인하면 되는 문제였다.....

첨에 방문배열 만들어서 안 쓰는 숫자 고려하고 이렇게 풀려고 했는데,,,

이걸 이렇게 오래 고민하다니...

머리를 다시 굴려줘야겠다..

 

 

1회독 2회독 3회독 4회독 5회독
V        

(참고)

 

[알고리즘] 백준 2503 숫자야구 - JAVA

www.acmicpc.net/problem/2503 2503번: 숫자 야구 첫째 줄에는 민혁이가 영수에게 몇 번이나 질문을 했는지를 나타내는 1 이상 100 이하의 자연수 N이 주어진다. 이어지는 N개의 줄에는 각 줄마다 민혁이가

easybrother0103.tistory.com

 

반응형