슈콩

[BOJ] 백준 17281 ⚾ 본문

Algorithms/Baekjoon

[BOJ] 백준 17281 ⚾

shukong 2025. 9. 26. 14:49

[문제]

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

 

 

[소스 코드]

import java.io.*;
import java.util.*;

public class Main {
	static int n,result = Integer.MIN_VALUE;
	static int[][] inning;
	static boolean[] visit; 
	static int[] order;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		n = Integer.parseInt(br.readLine());
		inning = new int[n][9];
		visit = new boolean[9];
		order = new int[9];
		order[3] = 0;
		for(int i=0;i<n;i++) {
			st = new StringTokenizer(br.readLine());
			for(int j=0;j<9;j++) {
				inning[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		perm(0);
		System.out.println(result);
	}
	private static void perm(int cnt) {
		if(cnt==9) {
			game();
			return;
		}
		for(int i=1;i<9;i++) {
			if(!visit[i]) {
				visit[i] = true;
				order[cnt] = i;
				if(cnt==2) perm(4);
				else perm(cnt+1);
				visit[i] = false;
			}
		}
	}
	private static void game() {
		int idx = 0;
		int score = 0;
		for(int t=0;t<n;t++) {
			int[] base = new int[4];
			int cnt = 0;
			while(cnt<3) {
				int res = inning[t][order[idx]];
				if(res==0) {
					cnt++;
				}
				else if(res==1) {
					score += base[3];
					base[3] = base[2];
					base[2] = base[1];
					base[1] = 1;
				}
				else if(res==2) {
					score += base[3] + base[2];
					base[3] = base[1];
					base[2] = 1;
					base[1] = 0;
				}
				else if(res==3) {
					score += base[3] + base[2] + base[1];
					base[3] = 1;
					base[2] = base[1] = 0;
				}
				else {
					score += base[3] + base[2] + base[1] + 1;
					base[3] = base[2] = base[1] = 0;
				}
				idx =  (idx+1) % 9;
			}
		}
		result = Math.max(result, score);
	}
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 백준 1260 DFS와 BFS  (0) 2025.09.29
[BOJ] 백준 23289 온풍기 안녕!  (2) 2025.09.27
[BOJ] 백준 17144 미세먼지 안녕!  (2) 2025.09.26
[BOJ] 백준 17143 낚시왕  (2) 2025.09.25
[BOJ] 백준 17142 연구소 3  (0) 2025.09.25