ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Algorithm/Baekjoon

[Java] ๋ฐฑ์ค€ 2564๋ฒˆ - ๊ฒฝ๋น„์›

๊ฐœ๋ฐœ๊ฐœ๊ตด๐Ÿธ 2022. 6. 5. 20:42

https://www.acmicpc.net/problem/2564

 

2564๋ฒˆ: ๊ฒฝ๋น„์›

์ฒซ์งธ ์ค„์— ๋ธ”๋ก์˜ ๊ฐ€๋กœ์˜ ๊ธธ์ด์™€ ์„ธ๋กœ์˜ ๊ธธ์ด๊ฐ€ ์ฐจ๋ก€๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์— ์ƒ์ ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ธ”๋ก์˜ ๊ฐ€๋กœ์˜ ๊ธธ์ด์™€ ์„ธ๋กœ์˜ ๊ธธ์ด, ์ƒ์ ์˜ ๊ฐœ์ˆ˜๋Š” ๋ชจ๋‘ 100์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค. ์ด์–ด ํ•œ ์ค„

www.acmicpc.net

[๋ฌธ์ œ ํ’€์ด]

๊ฐ ๋ฐฉํ–ฅ๋ณ„๋กœ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ• ๋•Œ ๊ณ ๋ คํ•ด์•ผ๋  ์š”์†Œ๋“ค์ด ๋งŽ์•„์„œ ๋‹ค์†Œ ๋ณต์žกํ–ˆ๋˜ ๊ตฌํ˜„๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

๊ตฌํ˜„์„ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์™ผ์ชฝ๊ณผ ์œ„์ชฝ ๊ฒฝ๊ณ„๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ๊ฐ’์„  ๋ถ์€ ์™ผ์ชฝ, ๋™์€ ์œ„์ชฝ, ๋‚จ์€ ์˜ค๋ฅธ์ชฝ, ์„œ๋Š” ์•„๋ž˜์ชฝ์œผ๋กœ ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆผ์œผ๋กœ ์„ค๋ช…ํ•ด ๋ณด์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

shop๋ฐฐ์—ด์— ์ƒ์  ๊ฐœ์ˆ˜ ๋งŒํผ ๋ฐฉํ–ฅ๊ฐ’๊ณผ, ์œ„์˜ ๊ทธ๋ฆผ ๋ฐฉํ–ฅ ๊ธฐ์ค€์œผ๋กœ ๊ฑฐ๋ฆฌ๊ฐ’์„ ์ €์žฅํ•œ ํ›„

shop์˜ ๋งˆ์ง€๋ง‰ ๋ฐฐ์—ด์— ๋™๊ทผ์ด์˜ ๋ฐฉํ–ฅ๊ฐ’๊ณผ ๊ฑฐ๋ฆฌ๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ ๋‹ค์Œ, ๋™๊ทผ์ด๊ฐ€ ์žˆ๋Š” ๋ฐฉํ–ฅ์ด ๋ฐ‘๋ฉด์ด๋ผ๊ณ  ๊ฐ€์ •ํ–ˆ์„๋•Œ, ๊ฐ ์ƒ์ ๊ณผ์˜ ๊ฑฐ๋ฆฌ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

- ์™ผ์ชฝ ์ƒ์  : ์ƒ์  ๊ฑฐ๋ฆฌ + ๊ฐ€๋กœ๋ณ€ - ๋™๊ทผ์ด์˜ ๊ฑฐ๋ฆฌ

- ์˜ค๋ฅธ์ชฝ ์ƒ์  : ์„ธ๋กœ๋ณ€ - ์ƒ์  ๊ฑฐ๋ฆฌ + ๋™๊ทผ์ด์˜ ๊ฑฐ๋ฆฌ

- ์œ—์ชฝ ์ƒ์  : ์ƒ์ ๊ฑฐ๋ฆฌ + ๊ฐ€๋กœ๋ณ€ - ๋™๊ทผ์ด์˜ ๊ฑฐ๋ฆฌ + ์„ธ๋กœ๋ณ€

- ์•„๋žซ์ชฝ ์ƒ์  : ์ƒ์ ๊ฑฐ๋ฆฌ - ๋™๊ทผ์ด์˜ ๊ฑฐ๋ฆฌ์˜ ์ ˆ๋Œ“๊ฐ’

[์ •๋‹ต ์ฝ”๋“œ]

import javax.swing.text.Style;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

public class Main {
    public static int N;
    public static int M;
    public static int[][] shop;
    public static int count = 0;
    public static int direction = 0;
    public static int dist = 0;
    // ๊ธฐ์ค€์ ์˜ ๊ฐ€๋กœ
    public static int width = 0;
    // ๊ธฐ์ค€์ ์˜ ๋†’์ด
    public static int height = 0;
    public static HashMap<Integer, String> map = new HashMap<>();
    // ๋ฑกํ–ฅ ์ •๋ณด ์ €์žฅ
    // 1 ๋ถ , 2 ๋‚จ, 3 ์„œ, 4 ๋™
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        count = Integer.parseInt(br.readLine());
        // ๋งˆ์ง€๋ง‰์—๋Š” ๊ธฐ์ค€์  ์ €์žฅํ•จ
        shop = new int[count + 1][2];
        for(int i=0;i<count + 1;i++) {
            st = new StringTokenizer(br.readLine());
            shop[i][0] = Integer.parseInt(st.nextToken());
            shop[i][1] = Integer.parseInt(st.nextToken());
            if(shop[i][0] == 2) {
                shop[i][1] = N - shop[i][1];
            }
            if(shop[i][0] == 3) {
                shop[i][1] = M - shop[i][1];
            }
        }
        direction = shop[count][0];
        dist = shop[count][1];
        getDirection(direction);
        getTotalDist();
    }
    public static void getDirection(int index) {
        if(index == 1 || index == 2) {
            width = N;
            height = M;
        } else {
            width = M;
            height = N;
        }
        if(index == 1) {
            map.put(4,"left");
            map.put(3,"right");
            map.put(2,"top");
            map.put(1,"bottom");
            return;
        }
        if(index == 2) {
            map.put(3,"left");
            map.put(4,"right");
            map.put(1,"top");
            map.put(2,"bottom");
            return;
        }
        if(index == 3) {
            map.put(1,"left");
            map.put(2,"right");
            map.put(4,"top");
            map.put(3,"bottom");
            return;
        }
        if(index == 4) {
            map.put(2,"left");
            map.put(1,"right");
            map.put(3,"top");
            map.put(4,"bottom");
            return;
        }
    }
    public static void getTotalDist() {
        int result = 0;
        int total = 2*N + 2*M;
        // ๊ฐ๊ฐ์˜ shop์„ ๋„ฃ๊ณ  ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ
        for(int i=0;i<count;i++) {
            String dir = map.get(shop[i][0]);
            int d = 0;
            if(dir.equals("left")) {
                d = shop[i][1] + width - dist;
            }
            else if(dir.equals("right")) {
                d = height - shop[i][1] + dist;
            }
            else if(dir.equals("top")) {
                d = shop[i][1] + width - dist + height;
            } else if(dir.equals("bottom")) {
                d = Math.abs(shop[i][1] - dist);
            }
            result += Math.min(d, total - d);
        }
        System.out.println(result);
    }
}

/*
 1
3 4
 2
 */