본문 바로가기

알고리즘/BOJ

[C++] 백준 2503번 - 숫자야구


0. 문제

 

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

1. 아이디어

 

1) 123부터 987까지의 모든 수들 중에서, 주어진 N개의 질문에 대해 같은 결과를 가지는 수를 카운트한다.

 

2. 소스코드

 

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
#include <iostream>
#include <string>
#include <vector>
struct entry
{
    std::string num;
    int strike;
    int ball;
};
int main()
{
    int N;
    std::cin >> N;
    std::vector<entry> entryV;
 
    for (int i = 0; i < N; i++)
    {
        entry input;
        std::cin >> input.num >> input.strike >> input.ball;
        entryV.push_back(input);
    }
 
    int result = 0;
 
    for (int i = 123; i <= 987; i++)
    {
        int temp = 0;
        std::string target = std::to_string(i);
        if (target[0== '0' || target[1== '0' || target[2== '0' || target[0== target[1|| target[1== target[2|| target[2== target[0])
            continue;
        for (int j = 0; j < N; j++)
        {
            int temps = 0, tempb = 0;
            for (int k = 0; k < 3; k++)
            {
                if (entryV[j].num[k] == target[k]) temps++;
                else if (target[k] == entryV[j].num[(k + 1) % 3|| target[k] == entryV[j].num[(k + 2) % 3])
                    tempb++;
            }
            if (temps == entryV[j].strike && tempb == entryV[j].ball) temp++;
        }
        if (temp == N) result++;
    }
    std::cout << result;
}
cs

 

3. 결과

 

4. 피드백

 

  • 혼자서 못 풀었다..
  • 브루트 포스는 결국 주어진 모든 조건들에 대해서 만족하는 경우의 수를 찾는 방법이구나.