반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
05-19 00:03
관리 메뉴

ImJay

[SWEA/Java] 8382. 방향 전환 본문

SW Expert Academy/D4

[SWEA/Java] 8382. 방향 전환

ImJay 2024. 1. 20. 18:31
반응형

[SWEA/Java] 8382. 방향 전환

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com


해설

 

풀이

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        // 테스트 케이스의 개수를 입력받음
        int testCase = Integer.parseInt(br.readLine());

        // 각 테스트 케이스에 대한 반복
        for (int t = 1; t <= testCase; t++) {
            // 시작점과 끝점의 좌표를 입력받음
            String input = br.readLine();
            StringTokenizer st = new StringTokenizer(input);

            int startX = Integer.parseInt(st.nextToken());
            int startY = Integer.parseInt(st.nextToken());
            int endX = Integer.parseInt(st.nextToken());
            int endY = Integer.parseInt(st.nextToken());

            // x축과 y축 각각의 거리를 계산
            int distanceX = Math.abs(startX - endX);
            int distanceY = Math.abs(startY - endY);

            int ans = 0;

            // x축 거리가 y축 거리보다 큰 경우
            if (distanceX > distanceY) {
                ans = distanceX * 2;

                // 거리가 짝수일 때, y축 거리가 홀수면 하나 감소
                if (distanceX % 2 == 0) {
                    if (distanceY % 2 == 1) ans--;
                }
                // 거리가 홀수일 때, y축 거리가 짝수면 하나 감소
                else {
                    if (distanceY % 2 == 0) ans--;
                }
            } 
            // y축 거리가 x축 거리보다 큰 경우
            else {
                ans = distanceY * 2;

                // 거리가 짝수일 때, x축 거리가 홀수면 하나 감소
                if(distanceY % 2 == 0) {
                    if (distanceX % 2 == 1) ans--;
                }
                // 거리가 홀수일 때, x축 거리가 짝수면 하나 감소
                else {
                    if (distanceX % 2 == 0) ans--;
                }
            }

            // 결과를 StringBuilder에 추가
            sb.append("#" + t + " " + ans + "\n");
        }

        // 최종 결과 출력
        System.out.print(sb);
    }
}
반응형
Comments