백준

백준 브루트포스 - 18111번 : 마인크래프트

맴썰 2022. 3. 3. 20:43

 

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

 

18111번: 마인크래프트

팀 레드시프트는 대회 준비를 하다가 지루해져서 샌드박스 게임인 ‘마인크래프트’를 켰다. 마인크래프트는 1 × 1 × 1(세로, 가로, 높이) 크기의 블록들로 이루어진 3차원 세계에서 자유롭게

www.acmicpc.net


import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		int b =  Integer.parseInt(st.nextToken());
		int[][] map = new int[n][m];
		for(int i=0; i<n; i++) {
			st = new StringTokenizer(br.readLine());
			for(int j=0; j<m; j++) {
				map[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		int ans = Integer.MAX_VALUE;
		int h = -1;
		for(int k = 0; k<=256; k++) {
			int target = k;
			int block = b;
			int time = 0;
			int[][] copy = new int[n][m];
			for(int i=0; i<n; i++) {
				System.arraycopy(map[i], 0, copy[i], 0, map[0].length);
			}
			for(int i=0; i<n; i++) {
				for(int j=0; j<m; j++) {
					if(copy[i][j]<target) {
							block -=(target-copy[i][j]);
							time+=(target-copy[i][j]);
							copy[i][j] = target;
					}
					if(copy[i][j]>target) {
						block+=(copy[i][j]-target);
						time+=(copy[i][j]-target)*2;
						copy[i][j] = target;
					}
					else continue;
				}
			}
			if(block>=0) {
				if(time<ans) {
					ans = time;
					h = target;
				}
				else if(time==ans) {
					if(target>h) {
						h = target;
					}
				}
			}
		}
		bw.write(ans + " "+ h);
		bw.close();
	}
	static boolean check(int[][] map) {
		int target = map[0][0];
		for(int i=0; i<map.length; i++) {
			for(int j=0; j<map[0].length; j++) {
				if(map[i][j]!=target) return false;
			}
		}
		return true;
	}
}