백준 [17281] ⚾(야구)
문제보기
소스코드
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int N, ans;
static int[][] inning;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
ans = 0;
inning = new int[N][9];
for (int i = 0; i < N; i++) {
for (int j = 0; j < 9; j++) {
inning[i][j] = sc.nextInt();
}
}
boolean[] v = new boolean[9];
v[0] = true;
setOrder(new int[9], 0, v);
System.out.println(ans);
}
public static void setOrder(int[] order, int idx, boolean[] v) {
if (idx == 3) {
setOrder(order, idx + 1, v);
return;
}
if (idx == order.length) {
getScore(order, 0, 0, 0);
return;
}
for (int i = 0; i < 9; i++) {
if (!v[i]) {
v[i] = true;
order[idx] = i;
setOrder(order, idx + 1, v);
v[i] = false;
}
}
}
public static void getScore(int[] order, int score, int idx, int n) {
if (n == N) {
ans = Math.max(ans, score);
return;
}
int out = 0;
int index = -1;
int[] loo = new int[3]; // 1루 2루 3루 홈
while (out < 3) {
for (int i = idx; i < 9; i++) {
index = i + 1;
int result = inning[n][order[i]];
if (result == 0) { // 아웃
out++;
if (out >= 3)
break;
} else if (result == 1) { // 안타
if (loo[2] != 0) {
score++;
loo[2] = 0;
}
loo[2] = loo[1];
loo[1] = loo[0];
loo[0] = 1;
} else if (result == 2) { // 2루타
for (int k = 1; k < 3; k++) {
if (loo[k] != 0) {
score++;
loo[k] = 0;
}
}
loo[2] = loo[0];
loo[0] = 0;
loo[1] = 1;
} else if (result == 3) { // 3루타
for (int k = 0; k < 3; k++) {
if (loo[k] != 0) {
score++;
loo[k] = 0;
}
}
loo[2] = 1;
} else if (result == 4) { // 홈런
score++; // 현재타자
for (int k = 0; k < 3; k++) {
if (loo[k] != 0) {
score++;
loo[k] = 0;
}
}
}
}
idx = 0;
}
getScore(order, score, index, n + 1);
}
}