Algorithm/백준
[BOJ] - 17281. 야구
benguin
2019. 8. 13. 15:38
[URL]
https://www.acmicpc.net/problem/17281
17281번: ⚾
⚾는 9명으로 이루어진 두 팀이 공격과 수비를 번갈아 하는 게임이다. 하나의 이닝은 공격과 수비로 이루어져 있고, 총 N이닝동안 게임을 진행해야 한다. 한 이닝에 3아웃이 발생하면 이닝이 종료되고, 두 팀이 공격과 수비를 서로 바꾼다. 두 팀은 경기가 시작하기 전까지 타순(타자가 타석에 서는 순서)을 정해야 하고, 경기 중에는 타순을 변경할 수 없다. 9번 타자까지 공을 쳤는데 3아웃이 발생하지 않은 상태면 이닝은 끝나지 않고, 1번 타자가 다시 타석에
www.acmicpc.net
[풀이 과정]
* 순열(완전 탐색) + 시뮬레이션
1. 가능한 모든 후보 탐색
4번째 타순은 1번 타자로 고정
따라서, 전체 경우의 수: (9-1)!
2. 각 경우의 수 , 각 이닝에 맞는 타순 선택: batter[]
3. 아웃 카운트, 점수 계산
4. 최종 점수 중 최대값 출력
[소스 코드]
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
#include <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;
int Inning;
int map[51][10];
int tmp[8] = { 1, 2, 3, 5, 6, 7, 8 ,9 };
int cand[9] = { 4, };
int maxScore = -1;
void input()
{
scanf("%d", &Inning);
for (int i = 1; i <= Inning; i++)
{
for (int j = 1; j <= 9; j++)
{
scanf("%d", &map[i][j]);
}
}
}
void go() {
do
{
for (int i = 0; i < 8; i++)
{
cand[i + 1] = tmp[i];
}
int batterIndex = 0;
int score = 0;
for (int i = 1; i <= Inning; i++)
{
// 아웃카운트 계산
int outCnt = 0;
// 점수 계산
queue <int> q;
q.push(0);
q.push(0);
q.push(0);
// 배트 계산 + 인덱스 까다롭다 ㅠㅠ
int batter[9];
for (int k = 0; k < 9; k++)
{
batter[cand[k] - 1] = map[i][k + 1];
}
while (outCnt != 3)
{
// 안타
if (batter[batterIndex] == 1)
{
int tmpScore;
tmpScore = q.front();
score += tmpScore;
q.pop();
q.push(1); // 한명 진출
batterIndex++;
if (batterIndex == 9) { batterIndex = 0; }
}
// 2루타
else if (batter[batterIndex] == 2)
{
int tmpScore;
tmpScore = q.front();
score += tmpScore;
q.pop();
q.push(1); // 한명 진출
tmpScore = q.front();
score += tmpScore;
q.pop();
q.push(0); // 2루타 계산
batterIndex++;
if (batterIndex == 9)
{
batterIndex = 0;
}
}
// 3루타
else if (batter[batterIndex] == 3)
{
int tmpScore;
tmpScore = q.front();
score += tmpScore;
q.pop();
q.push(1); // 한명 진출
tmpScore = q.front();
score += tmpScore;
q.pop();
q.push(0);
tmpScore = q.front();
score += tmpScore;
q.pop();
q.push(0); // 3루타 계산
batterIndex++;
if (batterIndex == 9) { batterIndex = 0; }
}
// 홈런
else if (batter[batterIndex] == 4)
{
int tmpScore;
tmpScore = q.front();
score += tmpScore;
q.pop();
q.push(1); // 한명 진출
tmpScore = q.front();
score += tmpScore;
q.pop();
q.push(0);
tmpScore = q.front();
score += tmpScore;
q.pop();
q.push(0);
tmpScore = q.front();
score += tmpScore;
q.pop();
q.push(0); // 홈런 계산
batterIndex++;
if (batterIndex == 9) { batterIndex = 0; }
}
// 아웃
else if (batter[batterIndex] == 0)
{
outCnt++;
batterIndex++;
if (batterIndex == 9) { batterIndex = 0; }
}
}
}
if (score > maxScore)
{
maxScore = score;
}
} while (next_permutation(tmp, tmp + 8));
}
int main() {
input();
go();
printf("%d\n", maxScore);
return 0;
}
|
cs |