문제 풀이 및 개발 공간

[백준] 18111번 마인크래프트 (silver 2 본문

백준공부/java

[백준] 18111번 마인크래프트 (silver 2

gomduri43 2024. 2. 14. 15:35

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

class Point{
    int h;
    int t;
    public Point(int h, int t){
        this.h=h;
        this.t=t;
    }
}
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.valueOf(st.nextToken());
        int m=Integer.valueOf(st.nextToken());
        int b=Integer.valueOf(st.nextToken());

        HashMap<Integer,Integer> dict=new HashMap<>();

        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.valueOf(st.nextToken());
            }
        }

        ArrayList<Point> arr=new ArrayList<>();


        for(int test=0; test<=256; test++) {
            int th =test;
            int tb = b;
            int time = 0;
            boolean possible = true;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (map[i][j] < th) {
                        time += (th - map[i][j]);
                        tb -= (th - map[i][j]);
                    }
                    else if(map[i][j]>th){
                        time+=2*(map[i][j]-th);
                        tb+=(map[i][j]-th);
                    }
                }

            }
            if(tb<0){
                possible=false;
                break;
            }
            if (possible) {
                arr.add(new Point(th, time));
            }
        }
        Collections.sort(arr, new Comparator<Point>(){
            public int compare(Point o1, Point o2){
                if(o1.t==o2.t){
                    return -(o1.h-o2.h);
                }
                else{
                    return o1.t-o2.t;
                }
            }
        });
        bw.write(arr.get(0).t+" "+arr.get(0).h);
        bw.flush();
    }
}



//블럭을 어느정도 높이에 맞추어서 세팅해야 최소인지 직관적으로 판단 불가.
//결국 0에서 256범위에서 브루트포스를 진행해야한다.
//이때 생각을 해야할게 이 범위안에서 다른블럭을 인벤에 저장하면 그걸 또 다른 곳에 사용이 가능하다.
//이부분을 생각못해서 시간이 오래걸림.. 문제어디에서도 제한사항이 없었고, 유동적으로 사용이 가능함.
//순서대로 채워야 하는 것이 아니므로, 결국 다른 곳의 잉여는 어느 곳에서든지 자유롭게 이용이 가능함.