백준 17281 풀이

남기용·2021년 8월 28일
0

백준 풀이

목록 보기
99/109

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);
	}
}
profile
개인용 공부한 것을 정리하는 블로그입니다.

0개의 댓글