반응형

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

 

17281번: ⚾

⚾는 9명으로 이루어진 두 팀이 공격과 수비를 번갈아 하는 게임이다. 하나의 이닝은 공격과 수비로 이루어져 있고, 총 N이닝 동안 게임을 진행해야 한다. 한 이닝에 3아웃이 발생하면 이닝이 종

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 순열을 통해 타자의 순서를 정합니다.

 

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
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
#include<iostream>
 
using namespace std;
 
int N;
int arr[51][10];
int order[10];
int visited[10];
int ans;
 
int getScore()        //야구 진행
{
    int cur = 1;
    int score = 0;
    for (int i = 1; i <= N; i++) { //이닝 번호
        int out = 0;
        int base[4= { 0 };
 
        while (out < 3) {    //out카운트가 3이 될 때까지
            int player = order[cur];
            base[0= 1;
            if (arr[i][player] == 0) {    //아웃
                out++;
            }
            else {    //진루
                for (int j = 3; j >= 0; j--) {
                    if (base[j] == 0) {
                        continue;
                    }
                    else {
                        if (j + arr[i][player] > 3) {
                            score++;                        }
                        else {
                            base[j + arr[i][player]] = 1;
                        }
                        base[j] = 0;
                    }
                }
            }
 
            cur++;
            if (cur > 9) cur = 1;
        }
    }
 
    return score;
}
 
void dfs(int level)
{
    if (level == 5 && order[4!= 1) {    //4번 타자가 1번 사람이 아니라면
        return;
    }
    if (level == 10) {
        ans = max(ans, getScore());
        return;
    }
 
    for (int i = 1; i <= 9; i++) {
        if (visited[i] == 0) {
            visited[i] = 1;
            order[level] = i;
            dfs(level + 1);
            visited[i] = 0;
        }
    }
}
 
int main()
{
    scanf("%d"&N);
 
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= 9; j++) {
            scanf("%d"&arr[i][j]);
        }
    }
 
    dfs(1);
 
    printf("%d", ans);
}
cs
반응형

+ Recent posts