

간단한 문제다. x축, y축방향으로밖에 이동못하는 경우 최단거리를 구하는 문제.
x, y, w-x, h-y 중 가장 작은 값이 최단거리다. 4개의 배열을 만들어주고, 정렬해서 제일작은값(=오름차순으로 제일앞에 나오는값)을 출력해주면 끝.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
StringTokenizer st = new StringTokenizer(str, " ");
int[] ans = new int[4];
ans[0] = Integer.parseInt(st.nextToken()); // x값
ans[1] = Integer.parseInt(st.nextToken()); // y값
int w = Integer.parseInt(st.nextToken());
int h = Integer.parseInt(st.nextToken());
ans[2] = w-ans[0];
ans[3] = h-ans[1];
Arrays.sort(ans);
System.out.println(ans[0]);
}
}'백준 문제풀이' 카테고리의 다른 글
| 4153. 직각삼각형 (자바, Java) (0) | 2022.10.10 |
|---|---|
| 3009. 네 번째 점 (0) | 2022.10.10 |
| 11478. 서로 다른 부분 문자열의 개수 (자바, Java) (0) | 2022.10.10 |
| 1269. 대칭차집합 (자바, Java) (0) | 2022.10.10 |
| 1764. 듣보잡 (자바, Java) (1) | 2022.10.03 |