본문 바로가기

ALGORITHM

백준 17281 ⚾ 야구 (Java)

1. 문제 링크

 

17281번: ⚾

⚾는 9명으로 이루어진 두 팀이 공격과 수비를 번갈아 하는 게임이다. 하나의 이닝은 공격과 수비로 이루어져 있고, 총 N이닝 동안 게임을 진행해야 한다. 한 이닝에 3아웃이 발생하면 이닝이 종

www.acmicpc.net

 

2. 문제 및 입출력예제

 

3. 문제 풀이

next 변수는 다음 타자를 저장하기 위해 사용했다.

flag는 3아웃 발생 시 다음 이닝으로 넘기기 위한 값이다.

순열로 타자 순서를 짠다. 4번 타자가 1번으로 던지는 경우에만 해당 코드를 반복한다.

 

이닝이 끝날 때 까지 해당 타자가 공을 친 결과를 확인한다. 

3루부터 홈까지 루 마다 이동시키고 스코어를 더한다.

해당 값에서 최댓값이 나왔으면 최댓값으로 바꾸고 출력한다.

 

코드를 더 깔끔하고 효율적으로 짤 수 있었을 것 같아서 아쉬움이 남는다.

 

4. 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {
	static ArrayList<String[]> list;
	static boolean[] visit;
	static int N, numbers[], max, next, outCnt, one, two, three;
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine());
		
		list = new ArrayList<>();
		for(int i=0;i<N;i++) {
			list.add(br.readLine().split(" "));
		}
		
		visit = new boolean[9];
		numbers = new int[9];
		
		permutation(0);
		
		System.out.println(max);
	}
	private static void permutation(int cnt) {
		if(cnt == 9) {
			if(numbers[3]==0) {
				int score = 0;
				for(int i=0;i<N;i++) {
					boolean flag = true;
					while(flag) { 
						for(int j=next;j<9;j++) {
							next = j+1; //다음 이닝에 연속해서 순서 진행
							if(next == 9) {
								next = 0;
							}
							int ball = Integer.parseInt(list.get(i)[numbers[j]]);
							if(ball == 0) {
								outCnt++;
								if(outCnt == 3) {
									flag = false;
									one = 0;
									two = 0;
									three = 0;
									outCnt = 0;
									break;
								}
							}
							if(ball == 1) {
								if(three == 1) {
									score++;
									three = 0;
								}
								if(two == 1) {
									three = 1;
									two = 0; 
								}
								if(one == 1) {
									two = 1;
									one = 0;
								}
								one = 1;						
							}
							if(ball == 2) {
								if(three == 1) {
									score++;
									three = 0;
								}
								if(two == 1) {
									score++;
									two = 0; 
								}
								if(one == 1) {
									three = 1;
									one = 0;
								}
								two = 1;						
							}
							if(ball == 3) {
								if(three == 1) {
									score++;
									three = 0;
								}
								if(two == 1) {
									score++;
									two = 0; 
								}
								if(one == 1) {
									score++;
									one = 0;
								}
								three = 1;						
							}
							if(ball == 4) {
								score++;
								if(three == 1) {
									score++;
									three = 0;
								}
								if(two == 1) {
									score++;
									two = 0; 
								}
								if(one == 1) {
									score++;
									one = 0;
								}
							}
						}
					}
				} 
				next = 0;
				max = Math.max(max, score);	
				return;
			}
		}
		
		for(int i=0;i<9;i++) {
			if(!visit[i]) {
				numbers[cnt] = i;
				visit[i] = true;
				permutation(cnt+1);
				visit[i] = false;
			}
		}
	}
}

 

'ALGORITHM' 카테고리의 다른 글

백준 10026 적록색약 (Java)  (0) 2023.03.20
백준 4153 직각 삼각형(Java)  (0) 2023.03.20
백준 13023 ABCDE (Java)  (0) 2023.03.19
백준 11050 이항 계수 1 (Java)  (0) 2023.03.18
백준 1541 잃어버린 괄호 (Java)  (0) 2023.03.18