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

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

 

21610๋ฒˆ: ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด์™€ ๋น„๋ฐ”๋ผ๊ธฐ

๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด๋Š” ํŒŒ์ด์–ด๋ณผ, ํ† ๋„ค์ด๋„, ํŒŒ์ด์–ด์Šคํ†ฐ, ๋ฌผ๋ณต์‚ฌ๋ฒ„๊ทธ ๋งˆ๋ฒ•์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ค๋Š˜ ์ƒˆ๋กœ ๋ฐฐ์šด ๋งˆ๋ฒ•์€ ๋น„๋ฐ”๋ผ๊ธฐ์ด๋‹ค. ๋น„๋ฐ”๋ผ๊ธฐ๋ฅผ ์‹œ์ „ํ•˜๋ฉด ํ•˜๋Š˜์— ๋น„๊ตฌ๋ฆ„์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์˜ค๋Š˜์€ ๋น„๋ฐ”๋ผ๊ธฐ

www.acmicpc.net


1. ๋ฌธ์ œ

NxN ํฌ๊ธฐ์˜ map์ด ์ฃผ์–ด์ง€๊ณ  ๋น„๊ตฌ๋ฆ„์˜ ์‹œ์ž‘์œ„์น˜๋Š” (N, 1), (N, 2), (N-1, 1), (N-1, 2)์ด๋‹ค.

์ด ๋น„๊ตฌ๋ฆ„์˜ ์ด๋™ ์ •๋ณด๋Š” M๋ฒˆ ์ฃผ์–ด์ง€๊ณ  ๋ฐฉํ–ฅ, ํšŸ์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค.

๋ฐฉํ–ฅ์€ 1๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ←, โ†–, ↑, โ†—, →, โ†˜, ↓, โ†™ ์ด๋‹ค.

1. ๋ชจ๋“  ๊ตฌ๋ฆ„์ด d๋ฐฉํ–ฅ์œผ๋กœ s์นธ ์ด๋™

2. ๊ตฌ๋ฆ„์—์„œ ๋น„๊ฐ€ ๋‚ด๋ ค map์— +1

3. ๋Œ€๊ฐ์„  ์นธ์— ๋ฌผ์ด ์žˆ์œผ๋ฉด ๋ฌผ์ด ์žˆ๋Š” ์นธ ์ˆ˜๋งŒํผ +

4. ๊ตฌ๋ฆ„ ๋‹ค์‹œ ์„ธํŒ… -> ์ด์ „ ๊ตฌ๋ฆ„ ์œ„์น˜ ์ œ์™ธํ•˜๊ณ  ๋ฌผ ์–‘์ด 2 ์ด์ƒ์ด๋ฉด ๊ตฌ๋ฆ„

2. ํ’€์ด

์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ์ด๋‹ค.

๋‚ด๊ฐ€ ๋†“์นœ ๋ถ€๋ถ„์€ 2๊ฐ€์ง€์˜€๋‹ค.

1. ๊ตฌ๋ฆ„ ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์ฒ˜์Œ์— ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ removeํ•  ๋•Œ ์ธ๋ฑ์Šค ๋•Œ๋ฌธ์— ํ—ท๊ฐˆ๋ ธ๋‹ค.

๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ ํ๋กœ ๋ณ€๊ฒฝํ•˜์˜€๋”๋‹ˆ ๊ธˆ๋ฐฉ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

2. ๋น„ ๋‚ด๋ฆฌ๊ณ  ๋น„๋ฐ”๋ผ๊ธฐ๋ฅผ ์‹œ์ „ํ•  ๋•Œ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜์—ฌ ๋Œ€๊ฐ์„  ์ฒดํฌ ์‹œ ์ž˜๋ชป๋œ ๊ฐ’์œผ๋กœ ์ฒดํฌ๋˜์—ˆ๋‹ค.

๋ชจ๋“  ๊ตฌ๋ฆ„ ์ขŒํ‘œ์— +1์„ ๋จผ์ € ํ•œ ๋‹ค์Œ, ๋Œ€๊ฐ์„  ์ฒดํฌ๋ฅผ ํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ

๋‚˜๋Š” ์ขŒํ‘œ๋งˆ๋‹ค +1 ํ›„ ๋Œ€๊ฐ์„  ์ฒดํฌ๋ฅผ ํ•ด์คฌ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์‹ค์ƒ ๋Œ€๊ฐ์„  ์ขŒํ‘œ๊ฐ€ 0์ด ์•„๋‹Œ๋ฐ๋„ 0์œผ๋กœ ์ฒดํฌํ•˜๊ณ  ๋”ํ•˜์ง€ ์•Š๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜์˜€๋‹ค.

 

3. ์ฝ”๋“œ

import java.io.*;
import java.util.*;

public class ๋งˆ๋ฒ•์‚ฌ์ƒ์–ด์™€๋น„๋ฐ”๋ผ๊ธฐ {

    static int N, M, map[][], ans;
    static Queue<int[]> cloudQ = new ArrayDeque<> ();
    static List<int[]> lastCloudList;
    static List<int[]> moveInfoList = new ArrayList<> ();

    static int[] dy = { 0,-1,-1,-1, 0, 1, 1, 1};
    static int[] dx = {-1,-1, 0, 1, 1, 1, 0,-1};

    static int[] cy = {-1,-1, 1, 1};
    static int[] cx = {-1, 1, 1,-1};

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());

        map = new int[N+1][N+1]; // 0 dummy
        for (int i = 1; i<=N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 1; j<=N; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        for (int i=0; i<M; i++) {
            st = new StringTokenizer(br.readLine());
            int dir = Integer.parseInt(st.nextToken());
            int cnt = Integer.parseInt(st.nextToken());
            moveInfoList.add(new int[] {dir, cnt});
        }
        // ๊ตฌ๋ฆ„ ์ขŒํ‘œ ์ €์žฅ
        cloudQ.offer(new int[] {N-1,1});
        cloudQ.offer(new int[] {N-1,2});
        cloudQ.offer(new int[] {N,1});
        cloudQ.offer(new int[] {N,2});
        // ์ž…๋ ฅ ๋

        solution();
        System.out.println(ans);
    }

    static void solution() {
        for (int i=0; i<M; i++) {
            move(moveInfoList.get(i)[0]-1, moveInfoList.get(i)[1]);
            rain();
            setCloud();
        }
        for (int i = 1; i<=N; i++) {
            for (int j = 1; j<=N; j++) {
                ans += map[i][j];
            }
        }
    }

    static void move(int dir, int cnt) {
        int len = cloudQ.size();
        for (int i=0; i<len; i++) {
            int[] lastLoc = cloudQ.poll();
            int y = lastLoc[0];
            int x = lastLoc[1];
            int ny = y;
            int nx = x;
            for (int c = 0; c<cnt ;c++) {
                if (ny + dy[dir] <= 0) ny = N;
                else if (ny + dy[dir] > N) ny = 1;
                else ny += dy[dir];

                if (nx + dx[dir] <= 0) nx = N;
                else if (nx + dx[dir] > N) nx = 1;
                else nx += dx[dir];
            }
            cloudQ.offer(new int[] {ny, nx});
        }
    }

    static void rain() {
        // ๋น„
        for (int[] loc : cloudQ) {
            map[loc[0]][loc[1]]++;
        }
        //๋น„๋ฐ”๋ผ๊ธฐ
        for (int[] loc : cloudQ) {
            int y = loc[0];
            int x = loc[1];
            int cnt = 0;
            for (int crossD = 0; crossD < 4; crossD++) {
                int ny = y+cy[crossD];
                int nx = x+cx[crossD];
                if (ny<=0 || ny>N || nx<=0 || nx>N) continue;
                if (map[ny][nx] > 0) cnt++;
            }
            map[y][x] += cnt;
        }
    }

    static void setCloud() {
        lastCloudList = new ArrayList<> ();
        while (!cloudQ.isEmpty()) {
            lastCloudList.add(cloudQ.poll());
        }
        for (int i=1; i<=N; i++) {
            for (int j=1; j<=N; j++) {
                if (map[i][j] >= 2) {
                    if (lastCloudCheck(i, j)) continue;
                    map[i][j] -= 2;
                    cloudQ.offer(new int[] {i, j});
                }
            }
        }
    }

    static boolean lastCloudCheck(int i, int j) {
        for (int[] loc : lastCloudList) {
            if (loc[0] == i && loc[1] == j) {
                return true;
            }
        }
        return false;
    }
}

4. ์‚ฌ๋‹ด

์˜ค๋žœ๋งŒ์— ํ‘ธ๋‹ˆ๊นŒ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ ธ์ง€๋งŒ ์ •๋ง ์žฌ๋ฐŒ์—ˆ๋‹ค.