본문 바로가기

알고리즘/BOJ

2503번

2503번 - 숫자 야구


생각보다 굉장히 까다로웠던 문제였다..나에겐..


처음에는 어떤 방식으로 풀어야 좋을까 굉장히 생각했다...그런데 생각보다 쉽지 않았다..


그런데 숫자가 3자리까지 밖에 되지 않기 때문에, 완전탐색이 가능하다는 걸 뒤늦게 깨달았다.


그래서 완전탐색으로 문제를 풀었다..


근데 50퍼센트에서 계속 틀려서.. 질문 게시판을 봤다


봤더니.. Strike=0 ,Ball =0 일때를 생각하지 못하고 풀었다가 50퍼센트에서 계속 틀렸다..


그리고 분명 숫자 1~9를 쓴다고 했는데, 그걸 못보고 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<iostream>
#include<vector>
using namespace std;
bool check[1000];
 
void checkNum(int num, int strike, int ball)
{
 
    int first = num / 100;
    int second = (num % 100/ 10;
    int third = num % 10;
 
    for (int i = 100; i <= 999; i++)
    {
        if (!check[i])
        {
            int ns = 0, nb = 0;
            int f = i / 100;
            int s = (i % 100/ 10;
            int t = i % 10;
 
            if (f == 0 || s == 0 || t == 0)
            {
                check[i] = true;
                continue;
            }
 
            if (strike == 0 && ball == 0)
            {
                if (first == f || first == s || first == t || second == f || second == s || second == t || third == f || third == s || third == t)
                {
                    check[i] = true;
                    continue;
                }
            }
 
            if (first == f)
            {
                ns++;
            }
            if (second == s)
            {
                ns++;
            }
            if (third == t)
            {
                ns++;
            }
 
            if (first != f && (first == s || first == t))
            {
                nb++;
            }
 
            if (second != s && (second == f || second == t))
            {
                nb++;
            }
            if (third != t && (third == f || third == s))
            {
                nb++;
            }
 
            if (ns != strike || nb != ball)
            {
                check[i] = true;
            }
 
 
        }
    }
 
 
}
int main()
{
    //freopen("input.txt", "r", stdin);
    int n;
    scanf("%d"&n);
 
 
    for (int i = 100; i <= 999; i++)
    {
        int first = i / 100;
        int second = (i % 100/ 10;
        int third = i % 10;
 
        if (first == second || second == third || first == third)
        {
            check[i] = true;
        }
    }
    while (n--)
    {
        int num, s, b;
        scanf("%d %d %d"&num, &s, &b);
 
        checkNum(num, s, b);
    }
 
    int ans = 0;
    for (int i = 100; i <= 999; i++)
    {
        if (!check[i])
        {
            ans++;
        }
    }
 
    printf("%d\n", ans);
    return 0;
}
cs


반응형

'알고리즘 > BOJ' 카테고리의 다른 글

1038번  (2) 2018.01.10
1213번  (0) 2018.01.10
3085번  (0) 2018.01.09
14891번  (2) 2018.01.09
14888번  (0) 2018.01.09