문제 풀이 및 개발 공간

[백준] 17144번 미세먼지 안녕! (gold 4 본문

백준공부/java

[백준] 17144번 미세먼지 안녕! (gold 4

gomduri43 2024. 3. 7. 22:40

import com.sun.security.jgss.GSSUtil;

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


public class Main {
    static int[][] map;
    static int[] numx = {-1, 1, 0, 0};
    static int[] numy = {0, 0, -1, 1};
    static int r;
    static int c;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());

        r = Integer.valueOf(st.nextToken());
        c = Integer.valueOf(st.nextToken());
        int t = Integer.valueOf(st.nextToken());
        int frog = 0;

        //공기청정기.
        ArrayList<int[]> a = new ArrayList<>();
        map = new int[r + 1][c + 1];

        //map초기화.
        for (int i = 1; i <= r; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 1; j <= c; j++) {
                frog = Integer.valueOf(st.nextToken());
                if (frog == -1) {
                    a.add(new int[]{j, i});

                }
                map[i][j]=frog;
            }
        }

        //시간의 흐름.
        for (int i = 0; i < t; i++) {
            flow(r, c);
            air(a.get(0)[0], a.get(0)[1],0);
            rair(a.get(1)[0], a.get(1)[1],0);
            map[a.get(0)[1]][a.get(0)[0]]=-1;
            map[a.get(1)[1]][a.get(1)[0]]=-1;
        }


        //답구하기
        int answer = 0;
        for (int i = 1; i <= r; i++) {
            for (int j = 1; j <= c; j++) {
                if (map[i][j] > 0) {
                    answer+=map[i][j];
                }
            }
        }
        bw.write(answer + "");
        bw.flush();
    }

    //확산
    public static void flow(int r, int c) {
        int[][] temp = new int[r + 1][c + 1];
        for (int i = 1; i <= r; i++) {
            for (int j = 1; j <= c; j++) {
                if (map[i][j] > 0) {
                    int how = 0;
                    for (int k = 0; k < 4; k++) {
                        int ty = i + numy[k];
                        int tx = j + numx[k];

                        if (ty < 1 || ty > r || tx < 1 || tx > c || map[ty][tx] == -1) {
                            continue;
                        }
                        how++;
                        //확산되어 들어온 미세먼지 기록.
                        temp[ty][tx] += map[i][j] / 5;
                    }
                    //확산으로 인해 줄어든 미세먼지 기록
                    temp[i][j] -= how * (map[i][j] / 5);
                }
            }
        }
        for (int i = 1; i <= r; i++) {
            for (int j = 1; j <= c; j++) {
                map[i][j] += temp[i][j];
            }
        }
    }


    //정화 시계방향
    public static void air(int x, int y, int f) {
        int temp=0;
        temp=xr(x,y,f);
        temp=yu(c,y,temp,1);
        temp=xl(c,1,temp);
        yd(1,1,temp,y);
    }

    //정화 반시계
    public static void rair(int x, int y,int f) {
        int temp=0;
        temp=xr(x,y,f);
        temp=yd(c,y,temp,r);
        temp=xl(c,r,temp);
        yu(1,r,temp,y);
    }

    //미세먼지 마지막꺼 반환
    public static int xr(int x, int y, int f) {
        x = 2;
        int temp = map[y][x];
        int temp2 = 0;
        while (x < c) {
            x++;
            temp2 = map[y][x];
            map[y][x] = temp;
            temp = temp2;
        }
        map[y][2]=0;
        return temp;
    }

    public static int xl(int x, int y, int f) {
        x = c-1;
        int temp = f;
        int temp2 = 0;
        while (x >= 1) {
            temp2 = map[y][x];
            map[y][x] = temp;
            temp = temp2;
            x--;
        }
        return temp;
    }

    public static int yu(int x, int y, int f, int ty) {
        y-=1;
        int temp = f;
        int temp2 = 0;
        while (y >= ty) {
            temp2 = map[y][x];
            map[y][x]=temp;
            temp = temp2;
            y--;
        }
        return temp;
    }

    public static int yd(int x, int y, int f, int ty) {
        y+=1;
        int temp = f;
        int temp2 = 0;
        while (y <= ty) {
            temp2 = map[y][x];
            map[y][x]=temp;
            temp = temp2;
            y++;
        }
        return temp;
    }

}