최장 맨해튼 거리

han.user();·2023년 4월 9일
0

구름

목록 보기
4/20
post-thumbnail

import java.io.*;

class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        int[] nums = new int[4];

        for (int i = 0; i < input.length; i++) {
            nums[i] = Integer.parseInt(input[i]);
        }

        int[] distances = new int[4];
        // 가능한 모든 좌표 쌍에 대해 맨해튼 거리를 계산하여 배열에 추가합니다.
        distances[0] = Math.abs(nums[0] - nums[2]) + Math.abs(nums[1] - nums[3]);
        distances[1] = Math.abs(nums[0] - nums[3]) + Math.abs(nums[1] - nums[2]);
        distances[2] = Math.abs(nums[0] - nums[2]) + Math.abs(nums[1] - nums[3]);
        distances[3] = Math.abs(nums[0] - nums[3]) + Math.abs(nums[1] - nums[2]);

        // 계산한 거리 중 가장 큰 값을 출력합니다.
        int maxDistance = distances[0];
        for (int i = 1; i < distances.length; i++) {
            if (distances[i] > maxDistance) {
                maxDistance = distances[i];
            }
        }
        System.out.println(maxDistance);
    }
}
profile
I'm still hungry.

0개의 댓글