[백준][BOJ][C++][17281번] 야구
본문 바로가기

알고리즘 문제풀기/백준

[백준][BOJ][C++][17281번] 야구

반응형

solution

p_order[8]배열에 2번부터 9번선수의 번호를 저장시켜 next_permutation을 이용하여 모든 경우의 수를 고려하여 주었습니다.next_permutation이 실행 될때마다 정답을 갱신시켜 주었습니다.

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
113
114
115
116
117
118
119
120
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
 
int N, ans, ans_temp;
int player[51][10]; //1회부터 n회까지 타자들의 성적을 기록
int player_temp[10];
int p_order[9]; //next_permutation을 이용하여 모든 경우의 수를 고려하기위한 배열
int s[4]; //1루부터 홈까지 사람이 있는지 판단
int out_cnt;//아웃카운트
bool nex; //3아웃일 경우 중단시켜 주기위한 bool
 
void game_go() {
    int start = 1; ans = 0;
    for (int n = 1; n <= N; n++) {
        out_cnt = 0; nex = false;
        while (1) {
            for (int i = start; i <= 9; i++) { 
                int ss = player[n][player_temp[i]];
                if (ss == 0//아웃이라면
                    out_cnt++;
                if (ss == 1) {
                    if (s[3== 1) {
                        s[3= 0;
                        ans++;
                    }
                    for (int ss = 3; ss > 1; ss--) {
                        if (s[ss - 1!= 0) {//선수가 위치에 있다면
                            s[ss] = 1;
                            s[ss - 1= 0;
                        }
                    }
                    s[1= 1;
                }//1루타
                if (ss == 2) {//2루타라면
                    if (s[3== 1) {
                        s[3= 0;
                        ans++;
                    }
                    if (s[2== 1) {
                        s[2= 0;
                        ans++;
                    }
                    if (s[1== 1) {
                        s[1= 0;
                        s[3= 1;
                    }
                    s[2= 1;
                }//2루타
                if (ss == 3) {//3루타라면
                    for (int j = 1; j <= 3; j++) {
                        if (s[j] == 1) {
                            s[j] = 0;
                            ans++;
                        }
                    }
                    s[3= 1;
                }//3루타
                if (ss == 4) {//홈런이라면
                    for (int j = 1; j <= 3; j++) {
                        if (s[j] == 1) {
                            s[j] = 0;
                            ans++;
                        }
                    }
                    ans++;
                }//홈런
                if (out_cnt == 3) { //3아웃이라면
                    if (i + 1 == 10) {
                        start = 1;
                    }
                    else
                        start = i + 1;
                    for (int j = 1; j <= 3; j++) {
                        s[j] = 0;
                    }
                    nex = true;
                    break;
                }
            }
            if (nex) break;
            start = 1;
        }//while
    }
}
 
void scoree() {
    do {
        int cnt = 1;
        for (int i = 0; i < 8; i++) {
            if (cnt != 4)
                player_temp[cnt] = p_order[i];
            else {
                cnt++;
                player_temp[cnt] = p_order[i];
            }
            cnt++;
        }
        game_go();
        ans_temp = max(ans, ans_temp);
    } while (next_permutation(p_order, p_order + 8));
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    for (int i = 0; i < 8; i++//2번타자부터 9번타자까지의 번호를 저장(위에서next_permutation을 써주기 위해)
        p_order[i] = i + 2;
    
    scanf("%d"&N);
    for (int Y = 1; Y <= N; Y++) {
        for (int X = 1; X <= 9; X++) {
            scanf("%d"&player[Y][X]);
        }
    }
    ans_temp = 0;
    player_temp[4= 1;
    scoree();
    cout << ans_temp;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

반응형