본문 바로가기
백준

[JAVA] 백준 17144 - 미세먼지 안녕!

by 맴썰 2025. 8. 30.

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

2차원 배열에 있는 미세먼지가 상하좌우로 확산되면서 동시에 공기청정기로 위에서 한바퀴 밑에서 한바퀴 돌면서 공기 청정기 위아래 칸을 없애는 문제였다.

위에 작성한대로 미세먼지를 확산시키고 재귀함수로 위에 한바퀴 밑에 한바퀴 배열을 밀어서 해결했다.

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

public class Main {
    static int ti= -1;
    static int ti2 = -1;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int[] a = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        int time = a[2];
        int[][] map = new int[a[0]][a[1]];
        for (int i = 0; i < map.length; i++) {
            map[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        }
        for (int i = 0; i < map.length; i++) {
            if(map[i][0]==-1&&ti==-1) ti = i;
            if(map[i][0]==-1&&ti!=-1) ti2 = i;
        }
        for (int t = 0; t < time; t++) {
            int[][] temp = new int[a[0]][a[1]];
            temp[ti][0] = -1;
            temp[ti2][0] = -1;
            for (int i = 0; i < map.length; i++) {
                for (int j = 0; j < map[0].length; j++) {
                    if(map[i][j]>0){
                        int target = map[i][j]/5;
                        if(diffusion(temp,i-1,j,target)) map[i][j]-=target;
                        if(diffusion(temp,i+1,j,target)) map[i][j]-=target;
                        if(diffusion(temp,i,j-1,target)) map[i][j]-=target;
                        if(diffusion(temp,i,j+1,target)) map[i][j]-=target;
                        temp[i][j]+=map[i][j];
                    }
                }

            }
            upperPush(temp,ti,1,0);
            downPush(temp,ti2,1,0);
            System.arraycopy(temp, 0, map, 0, map.length);
        }
        int sum = 0;
        for (int i = 0; i < map.length; i++) {
           sum+=Arrays.stream(map[i]).sum();
        }
        System.out.println(sum+2);
    }
    static boolean diffusion(int[][] map, int i, int j, int add){
        if(i<0||i>=map.length) return false;
        if(j<0||j>=map[0].length) return false;
        if(map[i][j]==-1) return false;
        map[i][j]+=add;
        return true;
    }

    static void upperPush(int[][] map, int i, int j, int direction){
        if(direction==0){//right
            if(j==map[0].length-1){
                upperPush(map,i,j,1);
            }else{
                upperPush(map,i,j+1,0);
            }
            if(j==1){
                map[i][j] = 0;
            }else{
                map[i][j] = map[i][j-1];
            }
        }else if(direction==1){
            if(i==0){
                upperPush(map,i,j,2);
            }else{
                upperPush(map,i-1,j,1);
            }
            if(i==ti){
                map[i][j] = map[i][j-1];
            }else{
                map[i][j] = map[i+1][j];
            }
        }else if(direction==2){
            if(j==0){
                upperPush(map,i,j,3);
            }else{
                upperPush(map,i,j-1,2);
            }
            if(j==map[0].length-1){
                map[i][j] = map[i+1][j];
            }else{
                map[i][j] = map[i][j+1];
            }
        }else if(direction==3){
            if(map[i][j]==-1){
                return;
            }else{
                upperPush(map,i+1,j,3);
            }
            if(i==0){
                map[i][j] = map[i][j+1];
            }else{
                map[i][j] = map[i-1][j];
            }
        }
    }

    static void downPush(int[][] map, int i, int j, int direction){
        if(direction==0){//right
            if(j==map[0].length-1){
                downPush(map,i,j,3);
            }else{
                downPush(map,i,j+1,0);
            }
            if(j==1){
                map[i][j] = 0;
            }else{
                map[i][j] = map[i][j-1];
            }
        }else if(direction==1){//up
            if(map[i][j]==-1){
                return;
            }else{
                downPush(map,i-1,j,1);
                if(i==map.length-1){
                    map[i][j] = map[i][j+1];
                }else{
                    map[i][j] = map[i+1][j];
                }
            }

        }else if(direction==2){//left
            if(j==0){
                downPush(map,i,j,1);
            }else{
                downPush(map,i,j-1,2);
            }
            if(j==map[0].length-1){
                map[i][j] = map[i-1][j];
            }else{
                map[i][j] = map[i][j+1];
            }
        }else if(direction==3){//down
            if(i==map.length-1){
                downPush(map,i,j,2);
            }else{
                downPush(map,i+1,j,3);
            }
            if(i==0){
                map[i][j] = map[i][j+1];
            }else{
                map[i][j] = map[i-1][j];
            }
        }
    }
}