BJ 17144スモッグこんにちは!


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

これは複雑に見えるが、問題の要求を単純に体現している問題だ.
package day0225;

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

public class FineDust {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
	static StringTokenizer st;
	static int R, C, T, loca_AcBottom;
	static int[][] map;
	static int[][] dir = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
	static ArrayList<Dust> list;

	static class Dust {
		int x, y, dust;

		public Dust(int x, int y, int dust) {
			super();
			this.x = x;
			this.y = y;
			this.dust = dust;
		}
	}

	static void diffusion() {
		for (Dust d : list) {
			int md = d.dust / 5;
			for (int i = 0; i < 4; i++) {
				int nX = d.x + dir[i][0];
				int nY = d.y + dir[i][1];
				if (nX >= 0 && nX < R && nY >= 0 && nY < C && map[nX][nY] >= 0) {
					map[nX][nY] += md;
					map[d.x][d.y] -= md;
				}
			}
		}
	}

	static void setList() {
		list.clear();
		for (int i = 0; i < R; i++) {
			for (int j = 0; j < C; j++) {
				if (map[i][j] > 0) {
					list.add(new Dust(i, j, map[i][j]));
				}
			}
		}
	}

	static void lotation() {
		// 위쪽 공기 순환
		for(int i = loca_AcBottom - 2; i > 0; i--) { // 아래로
			map[i][0] = map[i - 1][0];
		}
		for(int i = 0; i < C - 1; i++) {// 왼쪽
			map[0][i] = map[0][i + 1];
		}
		for(int i = 0; i < loca_AcBottom - 1; i++) { // 위
			map[i][C - 1] = map[i + 1][C - 1];
		}
		for(int i = C - 1; i > 0; i--) {
			map[loca_AcBottom - 1][i] = map[loca_AcBottom - 1][i - 1];
		}
		map[loca_AcBottom - 1][1] = 0;
		// 아래쪽 공기 순환
		for(int i = loca_AcBottom + 1; i < R - 1; i++) { // 위
			map[i][0] = map[i + 1][0];
		}
		for(int i = 0; i < C - 1; i++) {// 왼쪽
			map[R - 1][i] = map[R - 1][i + 1];
		}
		for(int i = R - 1; i > loca_AcBottom; i--) { // 아래로
			map[i][C - 1] = map[i - 1][C - 1];
		}
		for(int i = C - 1; i > 0; i--) {
			map[loca_AcBottom][i] = map[loca_AcBottom][i - 1];
		}	
		map[loca_AcBottom][1] = 0;
	}

	static void print() throws IOException {
		bw.append("\n");
		for (int i = 0; i < R; i++) {
			for (int j = 0; j < C; j++) {
				bw.append(map[i][j] + " ");
			}
			bw.append("\n");
		}
	}

	public static void main(String[] args) throws IOException {
		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];
		list = new ArrayList<>();
		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] > 0) {
					list.add(new Dust(i, j, map[i][j]));
				} else if (map[i][j] == -1) {
					loca_AcBottom = i;
				}
			}
		}
		for (int i = 0; i < T; i++) {
			diffusion();
			lotation();
			setList();
		}
		int sumofDust = 0;
		
		for(Dust d : list) {
			sumofDust += d.dust;
		}
		bw.append(sumofDust + "");
		bw.flush();
	}
}