본문 바로가기

코딩테스트/JAVA

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

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

 

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

public class Main {
    public static int R, C, T, ans = 0, uR, uC, dR, dC;
    public static int[][] map;
    public static int[] dr = {-1, 0, 1, 0};
    public static int[] dc = {0, 1, 0, -1};
    public static boolean chk = true;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        R = Integer.parseInt(st.nextToken());
        C = Integer.parseInt(st.nextToken());
        T = Integer.parseInt(st.nextToken());
        map = new int[R][C];
        for(int i = 0; i < R; i++){
            st = new StringTokenizer(br.readLine());
            for(int j = 0; j < C; j++){
                map[i][j] = Integer.parseInt(st.nextToken());
                if(map[i][j] == -1){
                    if(chk) {
                        uR = i;
                        uC = j;
                        chk = false;
                    } else{
                        dR = i;
                        dC = j;
                    }
                }
            }
        }

        while(T-- > 0){
            // 확산
            diffusion();

            // 공기청정기
            purifier();
        }

        // 계산
        for(int i = 0; i < R; i++){
            for(int j = 0; j < C; j++){
                if(map[i][j] == -1) continue;
                ans += map[i][j];
            }
        }

        System.out.println(ans);
    }
    public static void diffusion() {
        int[][] temp = new int[R][C];
        for(int i = 0; i < R; i++){
            for(int j = 0; j < C; j++){
                if(map[i][j] > 0) {
                    int w = map[i][j];
                    int cnt = 0;
                    for(int d = 0; d < 4; d++){
                        int nr = i + dr[d];
                        int nc = j + dc[d];
                        if(nr < 0 || nr >= R || nc < 0 || nc >= C || map[nr][nc] == -1) continue;
                        temp[nr][nc] += w / 5;
                        cnt++;
                    }
                    temp[i][j] += w - ((w / 5) * cnt);
                }
            }
        }

        map = temp;
        map[uR][uC] = -1;
        map[dR][dC] = -1;
    }
    public static void purifier() {
        // 위 공기청정기
        int d = 0;
        int r = uR - 1;
        int c = uC;
        while(true){
            int nr = r + dr[d];
            int nc = c + dc[d];
            if(nr < 0 || nr > uR || nc < 0 || nc >= C) {
                d = (d + 1) % 4;
                continue;
            }
            map[r][c] = map[nr][nc];
            r = nr;
            c = nc;
            if(r == uR && c == uC + 1) break;
        }
        map[r][c] = 0;

        // 아래 공기청정기
        d = 2;
        r = dR + 1;
        c = dC;
        while(true){
            int nr = r + dr[d];
            int nc = c + dc[d];
            if(nr < dR || nr >= R || nc < 0 || nc >= C) {
                d = (d + 3) % 4;
                continue;
            }
            map[r][c] = map[nr][nc];
            r = nr;
            c = nc;
            if(r == dR && c == dC + 1) break;
        }
        map[r][c] = 0;
    }
}

 

생각보다 오래걸렸다.

 

이런 범위 문제에서 범위를 실수하는 일이나 방향을 실수하는일이 좀 많은거 같다. 예전에도 이랬는데 그냥 많이 풀다보면 해결되려나