끄적끄적 코딩
article thumbnail
728x90

시뮬레이션 문제입니다.

미세먼지를 확산하고 공기청정기를 작동하는 것을 1초이며, t초 후 미세먼지의 양을 출력합니다.
미세먼지는 미세먼지/5를 4방향으로 확산합니다. (공기청정기, 벽일 경우 확산x)
확산 된 만큼 현재 위치의 미세먼지는 줄어듭니다.

공기청정기는 작동 시 그림에서 나온 방향으로 미세먼지가 이동하고
공기청정기로 들어온 미세먼지는 없어집니다.

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

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int ans = 0;
		int r = Integer.parseInt(st.nextToken());
		int c = Integer.parseInt(st.nextToken());
		int t = Integer.parseInt(st.nextToken());
		int[][] map = new int[r][c];
		int[] dy = {0, 1, 0, -1};
		int[] dx = {1, 0, -1, 0};
		int ay1 = 0;
		int ay2 = 0;
		int ax = 0;
		
		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) {
					ay2 = i;
				}
			}
		}
		ay1 = ay2 - 1;

		int time = 0;
		while(time < t) {
			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) {
						continue;
					}
					for(int k=0; k<4; ++k) {
						int nextY = i + dy[k];
						int nextX = j + dx[k];
						if(nextY < 0 || nextX < 0 || nextY >= r || nextX >= c || map[nextY][nextX] < 0) {
							continue;
						}
						temp[nextY][nextX] += map[i][j] / 5;
						temp[i][j] -= map[i][j] / 5;
					}
				}
			}
			for(int i=0; i<r; ++i) {
				for(int j=0; j<c; ++j) {
					if((i == ay1 || i == ay2) && j == ax) {
						continue;
					}
					map[i][j] += temp[i][j];
				}
			}
			int y = ay1 - 1; // 위
			int x = ax;
			int d = 3;
			while(true) {
				int nextY = y + dy[d];
				int nextX = x + dx[d];
				if(nextY == ay1 && nextX == ax) {
					break;
				}
				if(nextY < 0 || nextX < 0 || (d == 1 && nextY > ay1) || nextX >= c) {
					d = (d + 1) % 4;
					continue;
				}
				map[y][x] = map[nextY][nextX];
				map[nextY][nextX] = 0;
				y = nextY;
				x = nextX;
			}
			y = ay2 + 1; // 아래
			x = ax;
			d = 1;
			while(true) {
				int nextY = y + dy[d];
				int nextX = x + dx[d];
				if(nextY == ay2 && nextX == ax) {
					break;
				}
				if((d == 3 && nextY < ay2) || nextX < 0 || nextY >= r || nextX >= c) {
					d = (d + 3) % 4;
					continue;
				}
				map[y][x] = map[nextY][nextX];
				map[nextY][nextX] = 0;
				y = nextY;
				x = nextX;
			}
			++time;
		}
		for(int i=0; i<r; ++i) {
			for(int j=0; j<c; ++j) {
				ans += map[i][j];
			}
		}
		ans += 2;
		bw.write(ans + "");
		bw.close();
	}
}

'알고리즘' 카테고리의 다른 글

[Java] 백준 1235번 학생 번호  (0) 2023.02.13
[Java] 백준 2493번 탑  (0) 2023.02.13
[Java] 백준 21608번 상어 초등학교  (0) 2023.02.13
[Java] 백준 13335번 트럭  (0) 2023.02.13
[Java] 백준 2606번 바이러스  (0) 2023.02.12

검색 태그