ALGORITHM
백준 1085 직사각형에서 탈출 (Java)
공부하는_다온
2023. 1. 22. 20:33
1. 문제 링크
1085번: 직사각형에서 탈출
한수는 지금 (x, y)에 있다. 직사각형은 각 변이 좌표축에 평행하고, 왼쪽 아래 꼭짓점은 (0, 0), 오른쪽 위 꼭짓점은 (w, h)에 있다. 직사각형의 경계선까지 가는 거리의 최솟값을 구하는 프로그램
www.acmicpc.net
2. 문제 및 입출력예제
3. 문제 풀이
직사각형의 경계선까지의 거리를 구하는 문제이다.
그래서 주어진 입력과 꼭짓점의 x좌표 y좌표 사이와의 거리를 각각 구하면 된다.
4. 코드
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int w = sc.nextInt();
int h = sc.nextInt();
int min = x;
if(min > y) min = y;
int temp = Math.abs(x-w);
if(min > temp) min = temp;
temp = Math.abs(y-h);
if(min > temp) min = temp;
System.out.println(min);
}
}