白駿17281草



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

に答える


単純な実施問題なので、このような答えはありません.
順序で打順を構成し,構成された打順で野球を行い,得られた点数の中で最大値を探す全体探索問題である.

コード#コード#

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {
	static int[] order;
	static int[][] expect;
	static int n;
	static boolean[] visit;
	static int ans = 0;

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		order = new int[10];
		visit = new boolean[10];
		n = Integer.parseInt(br.readLine());
		expect = new int[n][10];

		// 4번 타자는 무조건 1번 선수
		order[4] = 1;

		String[] line;
		for (int i = 0; i < n; i++) {
			line = br.readLine().split(" ");
			for (int j = 0; j < 9; j++) {
				expect[i][j + 1] = Integer.parseInt(line[j]);
			}
		}

		makeOrder(1);

		bw.write(ans + "\n");
		bw.flush();
		bw.close();
		br.close();
	}

	private static void makeOrder(int cnt) {
		if (cnt == 10) {
			baseball();
			return;
		}
		for (int i = 2; i < 10; i++) {
			if (cnt == 4) {
				cnt++;
			}
			if (!visit[i]) {
				visit[i] = true;
				order[cnt] = i;
				makeOrder(cnt + 1);
				visit[i] = false;
			}
		}
	}

	private static void baseball() {
		int index = 1;
		int score = 0;
		for (int i = 0; i < n; i++) {
			int[] hit = expect[i];
			int outCount = 0;
			int[] base = new int[3];

			while (outCount != 3) {
				if(index == 10)
					index = 1;
				int hitter = order[index];
				// out
				if (hit[hitter] == 0) {
					outCount++;
				}
				// 안타
				else if (hit[hitter] == 1) {
					score += base[2];
					base[2] = base[1];
					base[1] = base[0];
					base[0] = 1;
				}
				// 2루타
				else if (hit[hitter] == 2) {
					score += base[2] + base[1];
					base[2] = base[0];
					base[1] = 1;
					base[0] = 0;
				}
				// 3루타
				else if (hit[hitter] == 3) {
					score += base[2] + base[1] + base[0];
					base[2] = 1;
					base[1] = 0;
					base[0] = 0;
				}
				// 홈런
				else if (hit[hitter] == 4) {
					score += base[2] + base[1] + base[0] + 1;
					base[2] = 0;
					base[1] = 0;
					base[0] = 0;
				}
				index++;
			}
		}
		ans = Math.max(ans, score);
	}
}