[Algorithm] - 백준 1085 직사각형에서 탈출 - Java


/*문제
한수는 지금 (x, y)에 있다. 직사각형의 왼쪽 아래 꼭지점은 (0, 0)에 있고, 오른쪽 위 꼭지점은 (w, h)에 있다. 직사각형의 경계선까지 가는 거리의 최솟값을 구하는 프로그램을 작성하시오.

입력
첫째 줄에 x y w h가 주어진다. w와 h는 1,000보다 작거나 같은 자연수이고, x는 1보다 크거나 같고, w-1보다 작거나 같은 자연수이고, y는 1보다 크거나 같고, h-1보다 작거나 같은 자연수이다.

출력
첫째 줄에 문제의 정답을 출력한다.*/

/*6 2 10 3*/

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class back1085 {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input[] = br.readLine().split(" ");
int result = 0;
int x = Integer.parseInt(input[0]);
int y = Integer.parseInt(input[1]);
int w = Integer.parseInt(input[2]);
int h = Integer.parseInt(input[3]);
//중간지점도 체크해서 비교해줘야 함.
if(x <= (w/2)){
result = x;
}else{
result = w-x;
}
if(y <= (h/2)){
if(result > y) result = y;
}else{
if(result > h-y) result = h-y;
}
System.out.println(result);
}
}

댓글