BOJ_16439_치킨치킨치킨

2021. 12. 7. 16:11백준 알고리즘(BOJ)

1

구분: 부르트포스

언어: Java
전략: 

  • 치킨 3가지 종류를 선택한다(조합)
  • 각 사람별로 선택된 치킨 중 가장 선호도가 큰 것을 고른다
  • 선택된 각 선호도를 더한 값을 계속 매 조합마다 최대값으로 갱신한다

2. 코드

import java.util.Scanner;
public class Main {
	static int m, n; //치킨 종류 m, 사람 수 n
	static int[][] chicken; //사람마다 치킨의 선호도들을 담을 배열
	static int max;
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		n = scanner.nextInt();
		m = scanner.nextInt();
		chicken = new int[n][m];
		for(int i = 0; i<n; i++) {
			for(int j = 0; j<m; j++) {
				chicken[i][j] = scanner.nextInt();
			}
		}
		max = Integer.MIN_VALUE;
		set = new int[3];
		comb(0,0);
		System.out.println(max);
	}//main
	
	static int[] set;//치킨 3가지 종류를 선택
	//조합으로 3가지 선택
	private static void comb(int cnt, int start) {
		if(cnt >=3) {
			int sum = 0;
			//set에있는 번호들과 chicken값을 비교
			for(int i = 0; i<n; i++) {
				sum+=getMax(i);
			}
			//매 조합마다 최대값을 갱신
			max = Math.max(sum, max);
			return;
		}
		
		for(int i = start; i<m; i++) {
			set[cnt] = i;
			comb(cnt+1, i+1);
		}
	}
	
	//각 사람별로 set에 선택된 3가지 중 가장 큰 선호도 get 
	private static int getMax(int num) {
		int getmax = -1;
		for(int i = 0; i<3; i++) {
			getmax = Math.max(getmax, chicken[num][set[i]]);
		}
		return getmax;
	}
	
	
}

3

  • 문제 이름이 귀엽다 
  • 부르트포스의 기본 문제 
  • max라는 전역변수를 사용했는데, 지역변수와 조금 겹치는 부분이 있어서 그 부분에서 문제가 있었지만 금방 해결하였다 

'백준 알고리즘(BOJ)' 카테고리의 다른 글

BOJ_1012_유기농 배추  (0) 2021.12.15
BOJ_14620_꽃길  (0) 2021.12.10
BOJ_15721_번데기  (0) 2021.12.07
BOJ_11725_트리의 부모 찾기  (0) 2021.11.19
BOJ_16938_캠프 준비  (0) 2021.11.12