쵼쥬
쵼쥬의 개발공부 TIL
쵼쥬
전체 방문자
오늘
어제
  • 분류 전체보기 (276)
    • 코딩테스트 (192)
      • [알고리즘] 알고리즘 정리 (7)
      • [백준] 코딩테스트 연습 (126)
      • [프로그래머스] 코딩테스트 연습 (59)
    • Spring (71)
      • [인프런] 스프링 핵심 원리- 기본편 (9)
      • [인프런] 스프링 MVC 1 (6)
      • [인프런] 스프링 MVC 2 (4)
      • [인프런] 실전! 스프링 부트와 JPA 활용1 (7)
      • [인프런] 실전! 스프링 부트와 JPA 활용2 (5)
      • [인프런] 실전! 스프링 데이터 JPA (7)
      • [인프런] 실전! Querydsl (7)
      • JWT (5)
      • [인프런] Spring Cloud (17)
      • [인프런] Spring Batch (4)
    • Java (6)
      • [Java8] 모던인자바액션 (4)
      • [부스트코스] 웹 백엔드 (2)
      • [패스트캠퍼스] JAVA STREAM (0)
    • CS (6)
      • 디자인 패턴과 프로그래밍 패터다임 (2)
      • 네트워크 (4)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

  • 프로그래머스
  • 스프링
  • 비트마스킹
  • 알고리즘
  • 코딩테스트
  • 인프런
  • 위클리 챌린지
  • 부스트코스
  • 타임리프
  • 구현
  • BFS
  • 백준
  • 누적합
  • 백분
  • jpa
  • MVC
  • spring
  • Spring Data JPA
  • querydsl
  • 자바

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
쵼쥬

쵼쥬의 개발공부 TIL

미세먼지 안녕! - 17144번
코딩테스트/[백준] 코딩테스트 연습

미세먼지 안녕! - 17144번

2022. 4. 19. 13:25


package com.company;

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


public class Main {

    static int[][] d = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 시계방향
    static int[][] arr;

    static int R, C, T, refresh, answer;

    public static void main(String[] args) throws IOException {
        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());

        arr = new int[R][C];
        refresh = 0;
        for (int i = 0; i < R; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < C; j++) {
                arr[i][j] = Integer.parseInt(st.nextToken());
                if (arr[i][j] == -1 && refresh == 0) {
                    refresh = i;
                }
            }
        }

        for (int i = 0; i < T; i++) {
            dust();        // 미세먼지 확산
            air(1);     // 아래쪽
            air(-1);    // 위쪽
        }

        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                if (arr[i][j] > 0) {
                    answer += arr[i][j];
                }
            }
        }
        System.out.println(answer);
    }

    static void air(int a) {
        int x = a == 1 ? refresh - 1 : refresh + 2;
        int y = 0;
        int direct = a == 1 ? 0 : 2;
        while (true) {
            int nextX = x + d[direct][0];
            int nextY = y + d[direct][1];
            if (nextX == (a == 1 ? refresh : refresh + 1) && nextY == 0) {
                arr[x][y] = 0;
                break;
            }

            if (nextX > (a == 1 ? refresh : R - 1) || nextY >= C || nextX < (a == 1 ? 0 : refresh + 1) || nextY < 0) {
                direct = (direct + (a == 1 ? 1 : 3)) % 4;
                continue;
            }
            arr[x][y] = arr[nextX][nextY];
            x = nextX;
            y = nextY;
        }
    }


    static void dust() {
        int[][] temp = new int[R][C];

        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                int count = 0;

                if (arr[i][j] > 0) {
                    for (int k = 0; k < 4; k++) {
                        int nextR = i + d[k][0];
                        int nextC = j + d[k][1];
                        if (nextR >= 0 && nextC >= 0 && nextR < R && nextC < C && arr[nextR][nextC] != -1) {
                            count++;
                            temp[nextR][nextC] += arr[i][j] / 5;
                        }
                    }
                }
                temp[i][j] -= arr[i][j] / 5 * count;
            }
        }

        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                arr[i][j] += temp[i][j];
            }
        }
    }
}

'코딩테스트 > [백준] 코딩테스트 연습' 카테고리의 다른 글

게리맨더링 2 - 17779번  (0) 2022.04.27
연구소 3 -17142번  (0) 2022.04.25
나무 재테크 - 16235번  (0) 2022.04.18
빚 - 10427번  (0) 2022.04.16
낚시왕 - 17143번  (0) 2022.04.13
    '코딩테스트/[백준] 코딩테스트 연습' 카테고리의 다른 글
    • 게리맨더링 2 - 17779번
    • 연구소 3 -17142번
    • 나무 재테크 - 16235번
    • 빚 - 10427번
    쵼쥬
    쵼쥬

    티스토리툴바