본문 바로가기

코딩 테스트/백준

[백준 1085번 문제, JAVA] 직사각형에서 탈출

728x90
반응형

문제


코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        int x = Integer.parseInt(input[0]);
        int y = Integer.parseInt(input[1]);
        int w = Integer.parseInt(input[2]);
        int h = Integer.parseInt(input[3]);

        int top = Math.abs(h - y);
        int right = Math.abs(w - x);

        List<Integer> list = Arrays.asList(top, right, x, y);
        int answer= Collections.min(list);
        System.out.println(answer);
    }
}

해결

x, y로부터 0, 0 ~ w, h 까지의 직사각형에서 가장 짧은 거리가 답이다


참고

링크

 

 

 

728x90
반응형