반응형

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

 

6987번: 월드컵

월드컵 조별 최종 예선에서는 6개국으로 구성된 각 조별로 동일한 조에 소속된 국가들과 한 번씩, 각 국가별로 총 5번의 경기를 치른다. 조별리그가 끝난 후, 기자가 보내온 각 나라의 승, 무승부

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 모든 경기의 수는 15 경기 이므로 시간 복잡도는 O(3^15)입니다.

 

2. 각 경기의 경우의 수를 vector p에 저장하고, 각 경기의 결과를 백트래킹을 통해 저장하고, 이 결과가 기자의 결과와 같다면 1을 출력하고, 아니라면 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
#include<iostream>
#include<vector>
 
using namespace std;
 
int arr[6][3];
int result[4];
int temp[6][3];
vector<pair<intint>> p;
bool res;
 
void dfs(int level)
{
    if (level == 15) {
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 3; j++) {
                if (arr[i][j] != temp[i][j]) {
                    return;
                }
            }
        }
        res = true;
        return;
    }
 
    const int a = p[level].first;
    const int b = p[level].second;
 
    //a 승
    temp[a][0]++;
    temp[b][2]++;
    dfs(level + 1);
    temp[a][0]--;
    temp[b][2]--;
 
    //무
    temp[a][1]++;
    temp[b][1]++;
    dfs(level + 1);
    temp[a][1]--;
    temp[b][1]--;
 
    //패
    temp[a][2]++;
    temp[b][0]++;
    dfs(level + 1);
    temp[a][2]--;
    temp[b][0]--;
}
 
int main()
{
    for (int i = 0; i < 5; i++) {
        for (int j = i + 1; j < 6; j++) {
            p.push_back({ i,j });
        }
    }
 
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 6; j++) {
            for (int k = 0; k < 3; k++) {
                scanf("%d"&arr[j][k]);
            }
        }
        res = false;
 
        dfs(0);
 
        if (res == true) {
            printf("1 ");
        }
        else {
            printf("0 ");
        }
    }
 
 
}
cs
반응형

+ Recent posts