๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV597vbqAH0DFAVl 

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com


1. ๋ฌธ์ œ ์š”์•ฝ

1. ๊ฐ€์žฅ์ž๋ฆฌ ์…€ ๋„์ฐฉ ์‹œ

  -> ๊ตฐ์ง‘ ๋‚ด ๋ฏธ์ƒ๋ฌผ ์ ˆ๋ฐ˜์œผ๋กœ ๊ฐ์†Œ, ์ ˆ๋ฐ˜์ด 1๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ ์‚ฌ๋ผ์ง

  -> ์ด๋™๋ฐฉํ–ฅ ๋ฐ˜๋Œ€

2. ์—ฌ๋Ÿฌ ๊ตฐ์ง‘์ด ํ•œ๊ตฐ๋ฐ ๋ชจ์ผ ๊ฒฝ์šฐ

  -> ๋ฏธ์ƒ๋ฌผ ์ˆ˜ ํ•ฉ์นจ

  -> ์ด๋™๋ฐฉํ–ฅ์€ ๋ฏธ์ƒ๋ฌผ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ๊ตฐ์ง‘์˜ ๋ฐฉํ–ฅ์„ ๋”ฐ๋ผ๊ฐ

 

2. ํ’€์ด

์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ์ด๋‹ค. ์•„๊ธฐ์ƒ์–ด, ๋‚š์‹œ์™•, ๋ฏธ์ƒ๋ฌผ ๊ฒฉ๋ฆฌ๋Š” ์„œ๋กœ ๋น„์Šทํ•œ ๋ฌธ์ œ๋“ค์ด๋‹ค.

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค.

์ •๋ ฌ์กฐ๊ฑด์€ ๋ฏธ์ƒ๋ฌผ ์ˆ˜ ๋งŽ์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ฃผ์—ˆ๋‹ค.

์ด๋™ํ•˜๊ธฐ ์ „, pq์—์„œ ์ขŒํ‘œ๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์–ด ๋ชฉํ‘œ ์ขŒํ‘œ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์งˆ ๊ฐ’๋“ค์„  ์ด๋™์‹œํ‚ฌ ํ move_q์— ๋„ฃ์–ด์ค€๋‹ค.

์ด๋™ํ•˜๊ธฐ ์œ„ํ•ด move_q์˜ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ map์— ์„ธํŒ…ํ•ด์ค€๋‹ค.

๋‹ค์‹œ map์„ ๋ณด๋ฉด์„œ null์ด ์•„๋‹Œ ์ขŒํ‘œ๋Š” pq์— ๋„ฃ์–ด์ค€๋‹ค.

 

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ 2๊ฐœ ์จ์„œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋‚จ๋“ค๋ณด๋‹ค 2๋ฐฐ๋Š” ๋” ๋งŽ์ด ๋‚˜์™”๋‹ค.

๋Œ€๋ถ€๋ถ„ list๋กœ ๋งŽ์ด ํ’€๋”๋ผ. ๋‚˜๋„ list๋กœ ๋‹ค์‹œ ํ’€์–ด๋ด์•ผ ๊ฒ ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด๊ฑฐ ํ’€๋ฉด์„œ ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ์ ์ด ์žˆ๋‹ค.

์šฐ์„ ์ˆœ์œ„ํ(PriorityQueue) ์“ฐ๋Š”๋ฐ ์ •๋ ฌ ์กฐ๊ฑด ์•ˆ์ฃผ๋ฉด ๋Ÿฐํƒ€์ž„์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค!

 

3. ์ฝ”๋“œ

๋ฆฌ์ŠคํŠธ ์‚ฌ์šฉ

 

์šฐ์„ ์ˆœ์œ„ ํ ์‚ฌ์šฉ

๋”๋ณด๊ธฐ

import java.io.*;
import java.util.*;
 
public class SW_2382_๋ฏธ์ƒ๋ฌผ๊ฒฉ๋ฆฌ {
 
    static int N, M, K, ans;
    static Pos[][] map;
    static PriorityQueue<Pos> pq; //๋ฏธ์ƒ๋ฌผ ์ˆ˜ ๋งŽ์€ ์ˆœ์œผ๋กœ ์ •๋ ฌ
    static PriorityQueue<Pos> move_q;
     
    static int[] dy = {-1,1,0,0};
    static int[] dx = {0,0,-1,1};
     
    public static void main(String[] args) throws Exception {
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = null;
        int T = Integer.parseInt(br.readLine());
         
        for (int tc=1; tc<=T; tc++) {
            st = new StringTokenizer(br.readLine());
            N = Integer.parseInt(st.nextToken());
            M = Integer.parseInt(st.nextToken());
            K = Integer.parseInt(st.nextToken());
            map = new Pos[N][N];
            pq = new PriorityQueue<> (); 
            for (int i=0; i<K; i++) {
                st = new StringTokenizer(br.readLine());
                int y = Integer.parseInt(st.nextToken());
                int x = Integer.parseInt(st.nextToken());
                int cnt = Integer.parseInt(st.nextToken());
                int dir = Integer.parseInt(st.nextToken())-1;
                pq.offer(new Pos(y, x, cnt, dir));
                map[y][x] = new Pos(y, x, cnt, dir);
            } //์ž…๋ ฅ ๋
             
            ans = 0;
            solution();
            System.out.println("#"+tc+" "+ans);
        }
    }
     
    static void solution() {
        for (int time=0; time<M; time++) {
            if (time>0) getCell(); //null์ด ์•„๋‹ˆ๋ฉด pq์— ๋„ฃ์–ด์คŒ
            move(); //์ด๋™
        }
         
        for (int i=0; i<N; i++) {
            for (int j=0; j<N; j++) {
                if (map[i][j] ==null) continue;
                ans += map[i][j].cnt;
            }
        }
    }
     
    static void move() {
        move_q = new PriorityQueue<> ();
        while (!pq.isEmpty()) {
            Pos cur = pq.poll();
            int y = cur.y;
            int x = cur.x;
            int cnt = cur.cnt;
            int d = cur.dir;
            map[y][x] = null;
             
            int ny = cur.y+dy[d];
            int nx = cur.x+dx[d];
            if (ny==0 || ny==N-1 || nx==0 || nx==N-1) { //๊ฐ€์žฅ์ž๋ฆฌ์ผ ๋•Œ
                if (cnt/2 >= 1) {
                    int nd=0;
                    switch (d) {
                    case 0: nd=1; break;
                    case 1: nd=0; break;
                    case 2: nd=3; break;
                    case 3: nd=2; break;
                    }
                    move_q.offer(new Pos(ny, nx, cnt/2, nd));
                }
            } else {
                move_q.offer(new Pos(ny, nx, cnt, d));
            }
        }
         
        while (!move_q.isEmpty()) {
            Pos cur = move_q.poll();
            int y = cur.y;
            int x = cur.x;
            int cnt = cur.cnt;
            int d = cur.dir;
             
            if (map[y][x] != null) {
                map[y][x].cnt += cnt; //๋ฏธ์ƒ๋ฌผ ์ˆ˜๋งŒ ๋”ํ•ด์คŒ
                 
            } else {
                map[y][x] = cur;
            }
        }
    }
     
    static void getCell() {
        for (int i=0; i<N; i++) {
            for (int j=0; j<N; j++) {
                if (map[i][j] ==null) continue;
                pq.offer(map[i][j]);
            }
        }
    }
     
     
    static class Pos implements Comparable<Pos>{
        int y, x, cnt, dir;
        Pos(int y, int x, int cnt, int dir) {
            this.y = y;
            this.x = x;
            this.cnt = cnt;
            this.dir = dir;
        }
        @Override
        public int compareTo(Pos o) {
            if (o.cnt - this.cnt == 0) {
                if (this.y - o.y == 0) {
                    return this.x - o.x;
                }
                else return this.y - o.y;
            }
            else return o.cnt - this.cnt;
        }
    }
}

4. ์‚ฌ๋‹ด