[JAVA-자바] 17144번: 미세먼지 안녕!

2025. 5. 18. 13:50·알고리즘/백준

 

소스코드:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    static BufferedReader br;
    static BufferedWriter bw;

    static int width, height, T;
    static int[][] board;
    static int machine;
    static Queue<int[]> munziQueue;

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

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

        StringTokenizer st = new StringTokenizer(br.readLine());
        height = Integer.parseInt(st.nextToken());
        width = Integer.parseInt(st.nextToken());
        T = Integer.parseInt(st.nextToken());

        munziQueue = new LinkedList<>();
        board = new int[height][width];

        boolean isFind = false;

        for (int y = 0; y < height; y++) {
            st = new StringTokenizer(br.readLine());
            for (int x = 0; x < width; x++) {
                board[y][x] = Integer.parseInt(st.nextToken());
                if (board[y][x] == -1 && !isFind) {
                    isFind = true;
                    machine = y;
                }
                if (board[y][x] > 0) {
                    munziQueue.add(new int[]{y, x});
                }
            }
        }
        br.close();

        int curTime = 0;
        while (curTime != T) {
            int[][] temp_board = new int[height][width];

            while (!munziQueue.isEmpty()) {
                int[] cur = munziQueue.poll();
                int curY = cur[0];
                int curX = cur[1];
                int munziValue = board[curY][curX];
                int fly_munziValue = munziValue / 5;
                int fly_cnt = 0;

                if (fly_munziValue == 0) {
                    continue;
                }

                for (int i = 0; i < 4; i++) {
                    int nextY = curY + dy[i];
                    int nextX = curX + dx[i];
                    if (isValidPos(nextY, nextX)) {
                        temp_board[nextY][nextX] += fly_munziValue;
                        fly_cnt++;
                    }
                }
                board[curY][curX] -= fly_munziValue * fly_cnt;
            }

            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if (board[y][x] == -1) {
                        continue;
                    }
                    if (temp_board[y][x] > 0) {
                        board[y][x] += temp_board[y][x];
                    }
                }
            }

            turnOn();

            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if (board[y][x] == -1) {
                        continue;
                    }
                    if (board[y][x] > 0) {
                        munziQueue.add(new int[]{y, x});
                    }
                }
            }

            curTime++;
        }

        int result = 0;

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (board[y][x] > 0) {
                    result += board[y][x];
                }
            }
        }

        bw.write(result + "\n");
        bw.close();
    }

    public static boolean isValidPos(int y, int x) {
        if (((y == machine) && (x == 0)) || ((y == (machine + 1)) && (x == 0))) {
            return false;
        }

        return (y >= 0 && y < height && x >= 0 && x < width);
    }

    public static void turnOn() {
        int topY = machine; // 위
        int downY = machine + 1; // 아래

        for (int y = topY - 1; y > 0; y--) {
            board[y][0] = board[y - 1][0];
        }
        for (int x = 0; x < width - 1; x++) {
            board[0][x] = board[0][x + 1];
        }
        for (int y = 0; y < topY; y++) {
            board[y][width - 1] = board[y + 1][width - 1];
        }
        for (int x = width - 1; x > 1; x--) {
            board[topY][x] = board[topY][x - 1];
        }
        board[topY][1] = 0;

        for (int y = downY + 1; y < height - 1; y++) {
            board[y][0] = board[y + 1][0];
        }
        for (int x = 0; x < width - 1; x++) {
            board[height - 1][x] = board[height - 1][x + 1];
        }
        for (int y = height - 1; y > downY; y--) {
            board[y][width - 1] = board[y - 1][width - 1];
        }
        for (int x = width - 1; x > 1; x--) {
            board[downY][x] = board[downY][x - 1];
        }
        board[downY][1] = 0;
    }

}

 

글의 내용 중 잘못된 점이나 수정이 필요한 부분, 혹은 궁금한 사항이 있다면 언제든 댓글로 남겨주시면 감사하겠습니다.

여러분의 피드백은 더 나은 글을 작성하는 데 큰 도움이 됩니다. 감사합니다.

'알고리즘 > 백준' 카테고리의 다른 글

[JAVA-자바] 1865번: 웜홀  (0) 2025.05.21
[JAVA-자바] 1238번: 파티  (0) 2025.05.19
[JAVA-자바] 14938번: 서강그라운드  (0) 2025.05.16
[JAVA-자바] 14502번: 연구소  (0) 2025.05.15
[JAVA-자바] 12851번: 숨바꼭질 2  (0) 2025.05.14
'알고리즘/백준' 카테고리의 다른 글
  • [JAVA-자바] 1865번: 웜홀
  • [JAVA-자바] 1238번: 파티
  • [JAVA-자바] 14938번: 서강그라운드
  • [JAVA-자바] 14502번: 연구소
raven
raven
Github : https://github.com/RabeMaster | Email : ra___be@naver.com
  • raven
    배움을 원하는 사람
    raven
  • 전체
    오늘
    어제
  • 공지사항

    • 안녕하세요
    • 분류 전체보기 (169)
      • 네이버 부스트캠프 10기 (7)
        • 멤버십 (5)
        • 챌린지 (1)
        • 베이직 (1)
      • 공부 (2)
        • JAVA (1)
        • CS (0)
        • 정보처리기사 (1)
      • 알고리즘 (159)
        • 백준 (159)
      • 개발 (1)
        • 백준 확장 프로그램 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 인기 글

  • 태그

    네이버
    브루트포스 알고리즘
    정렬
    문자열
    부트캠프
    그래프 이론
    네부캠
    그래프 탐색
    코딩
    구현
    부스트캠프
    코테
    백준
    java
    자료 구조
    개발
    너비 우선 탐색
    자바
    멤버십
    스택
    최단 경로
    IT
    네이버부스트캠프
    알고리즘
    백트래킹
    코딩테스트
    수학
    그리디 알고리즘
    회고
    다이나믹 프로그래밍
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
raven
[JAVA-자바] 17144번: 미세먼지 안녕!
상단으로

티스토리툴바