백준 17144 - 미세먼지 안녕

백준 17144 - 미세먼지 안녕

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

★ 풀이

문제에서 주어진 시간만큼 미세먼지의 확산과 공기청정기에 의한 미세먼지의 이동이 이루어진다. 나 같은 경우 미세먼지의 확산을 처리하는 함수 diffuse() 그리고 공기청정기에 의한 이동을 처리하는 함수 airClear() 이렇게 크게 두 가지 함수를 구현해서 문제를 풀었고, airClear() 함수에서 반시계 방향 공기 순환 처리는 circul_counter(), 시계 방향 공기 순환 처리는 circul() 에서 각각 나누어서 처리하도록 했다.

신경써서 처리해야 할 부분은 설명에서 미세먼지의 확산이 동시에 이루어진다고 했으므로 그것을 처리하기 위해서 Queue에 미세먼지가 존재하는 좌표값들을 모두 저장해 놓은 다음 한꺼번에 처리하도록 해야한다. 공기 순환 함수들은 배열의 이동만 제대로 구현해주면 간단하다.

★ 소스 코드

import java.io.*; import java.util.*; class Point{ int x,y,amount; Point(int x, int y, int amount){ this.x = x; this.y = y; this.amount = amount; } } public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static int r,c,t; static int board[][]; public static void main(String[] args) throws IOException { StringTokenizer st = new StringTokenizer(br.readLine()); r = Integer.parseInt(st.nextToken()); c = Integer.parseInt(st.nextToken()); t = Integer.parseInt(st.nextToken()); board = new int[r][c]; for(int i = 0; i q = new LinkedList(); for(int i = 0; i= 5) { q.add(new Point(i,j,board[i][j]/5)); } } } while(!q.isEmpty()) { Point p = q.poll(); // 4방향 퍼지기 for(int i = 0; i<4; i++) { int nx = p.x + dx[i]; int ny = p.y + dy[i]; if(nx >= 0 && ny >= 0 && nx < r && ny < c && board[nx][ny] != -1) { board[nx][ny] += p.amount; board[p.x][p.y] -= p.amount; } } } } static void airClear() { for(int i = 0; i=x; i--) { board[i + 1][c - 1] = board[i][c - 1]; } for(int i = c - 2; i>=1; i--) { board[x][i + 1] = board[x][i]; if(i == 1) { board[x][i] = 0; } } } // 4단계 (아래,왼,위,오른) static void circul_counter(int x) { for(int i = x - 1; i>=0; i--) { board[i + 1][0] = board[i][0]; if(i + 1 == x) { board[x][0] = -1; } } for(int i = 1; i<=c - 1; i++) { board[0][i - 1] = board[0][i]; } for(int i = 1; i<=x; i++) { board[i - 1][c - 1] = board[i][c - 1]; } for(int i = c - 2; i>=1; i--) { board[x][i + 1] = board[x][i]; if(i == 1) { board[x][i] = 0; } } } }

from http://sweet-smell.tistory.com/55 by ccl(A) rewrite - 2021-09-25 22:01:12