๋ฌธ์ ์ค๋ช
๊ณต๊ธฐ์ฒญ์ ๊ธฐ โก๏ธ 1๋ฒ ์ด์ ์ค์น, ๋ ํ์ ์ฐจ์งํจ$A_{r,c}$
: (r,c)์ ์๋ ๋ฏธ์ธ๋จผ์ง์ ์
1์ด ๋์ ๋ฐ์ํ๋ ์ผ
- ๋ฏธ์ธ ๋จผ์ง ํ์ฐ (๋ชจ๋ ๋์์ ๋ฐ์)
- (r,c)์ ์๋ ๋ฏธ์ธ๋จผ์ง๋ ์ธ์ ํ ๋ค ๋ฐฉํฅ์ผ๋ก ํ์ฐ
- ์ธ์ ํ ๋ฐฉํฅ์ ๊ณต๊ธฐ์ฒญ์ ๊ธฐ๊ฐ ์๊ฑฐ๋, ๋ฒ์๊ฐ ๋ฒ์ด๋๋ฉด ํ์ฐโ
- ํ์ฐ ๋๋ ์ : $A_{r,c}$/5, ์์์ ์ ๋ฒ๋ฆผ
- (r,c)์ ๋จ๋ ์ : $A_{r,c} - (A_{r,c}/5)$x(ํ์ฐ๋ ๋ฐฉํฅ์ ๊ฐ์)
- ๊ณต๊ธฐ์ ์ฒญ๊ธฐ ์๋
- ์์ชฝ ๋ฐ๋์ ๋ฐ์๊ณ๋ฐฉํฅ ์ํ, ์๋์ชฝ ๋ฐ๋์ ์๊ณ๋ฐฉํฅ์ผ๋ก ์ํ
- ๋ฏธ์ธ๋จผ์ง๋ ๋ฐ๋ ๋ฐฉํฅ๋๋ก ํ ์นธ์ฉ ์ด๋
- ๊ณต๊ธฐ์ฒญ์ ๊ธฐ๋ก ๋ค์ด๊ฐ ๋ฏธ์ธ๋จผ์ง๋ ์ ํ
T์ด ํ, ๋จ์ ๋ฏธ์ธ๋จผ์ง์ ์โ
๋ฌธ์ ํ์ด
์์๋๋ก ์งํํ๋ฉด ๋๋คโผ
fineDust(int x, int y, int amout)
๐ ๋ฏธ์ธ๋จผ์ง ์ ๋ณด ๊ฐ์ฒด
amountOfFineDust[i][j] : (i,j)์ ๋ฏธ์ธ๋จผ์ง ์
airCleaner : ๊ณต๊ธฐ ์ฒญ์ ๊ธฐ์ ์ ํ ๊ฐ
1. ๋ฏธ์ธ๋จผ์ง ์์น ํ์ธ & ํ์ฐ
1) checkDust()
: ๋ฏธ์ธ๋จผ์ง๋ฅผ dusts์ ์ถ๊ฐ
2) spreadDust()
: ๋ฏธ์ธ๋จผ์ง ํ์ฐ
- (i,j์ ๋ฏธ์ธ๋จผ์ง ์ < 5๋ผ๋ฉด ํ์ฐํ์ง ์๋๋ค.
- ๊ณต๊ธฐ์ฒญ์ ๊ธฐ๊ฐ ์๋๊ณ , ๋ฒ์๊ฐ ๋ฒ์ด๋์ง ์๋ ๊ฐ ๋ค ๋ฐฉํฅ์ผ๋ก now.amount/5 ๋ฅผ ์ถ๊ฐ
- (i,j)์ ๋ฏธ์ธ๋จผ์ง ์ -= (amountOfSpread * cnt)
2. ๊ณต๊ธฐ์ฒญ์ ๊ธฐ ์๋
operateAirCleaner()
๊ฐ๋จํ๊ฒ ๊ตฌํ~!
๋ฐ๋ ๋ฐฉํฅ์ผ๋ก ์ด๋์์ผ์ฃผ๋ฉด ๋๋ค!
๊ณต๊ธฐ์ฒญ์ ๊ธฐ๋ก ๋ค์ด์ค๋ฉด ์ ํ
์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main_BOJ_17144_๋ฏธ์ธ๋จผ์ง_์๋
{
static int R, C, T;
static int airCleaner = -1;
static int[][] amountOfFineDust;
static int[] dx = {-1, 0, 0, 1};
static int[] dy = {0, -1, 1, 0};
static Queue<fineDust> dusts;
static class fineDust {
int x;
int y;
int amount;
public fineDust(int x, int y, int amount) {
this.x = x;
this.y = y;
this.amount = amount;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stringTokenizer = new StringTokenizer(br.readLine());
R = Integer.parseInt(stringTokenizer.nextToken());
C = Integer.parseInt(stringTokenizer.nextToken());
T = Integer.parseInt(stringTokenizer.nextToken());
amountOfFineDust = new int[R][C];
for (int i = 0; i < R; i++) {
stringTokenizer = new StringTokenizer(br.readLine());
for (int j = 0; j < C; j++) {
amountOfFineDust[i][j] = Integer.parseInt(stringTokenizer.nextToken());
if(amountOfFineDust[i][j] == -1 && airCleaner == -1) airCleaner = i;
}
}
while (T-- > 0) {
checkDust();
spreadDust();
operateAirCleaner();
}
int ans = 0;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
ans += amountOfFineDust[i][j];
}
}
System.out.println(ans);
}
private static void operateAirCleaner() {
int top = airCleaner;
int bottom = airCleaner + 1;
for (int i = top - 1; i > 0; i--) {
amountOfFineDust[i][0] = amountOfFineDust[i - 1][0];
}
for (int i = 0; i < R-1; i++) {
amountOfFineDust[0][i] = amountOfFineDust[0][i + 1];
}
for (int i = 0; i < top - 1; i++) {
amountOfFineDust[i][C - 1] = amountOfFineDust[i + 1][C - 1];
}
for (int i = C - 1; i > 0; i--) {
amountOfFineDust[top][i] = amountOfFineDust[top][i - 1];
}
amountOfFineDust[top][1] = 0;
for (int i = bottom + 1; i < R - 1; i++) {
amountOfFineDust[i][0] = amountOfFineDust[i + 1][0];
}
for (int i = 0; i < C - 1; i++) {
amountOfFineDust[R - 1][i] = amountOfFineDust[R - 1][i + 1];
}
for (int i = R - 1; i > bottom ; i--) {
amountOfFineDust[i][C - 1] = amountOfFineDust[i - 1][C - 1];
}
for (int i = C - 1; i > 0; i--) {
amountOfFineDust[bottom][i] = amountOfFineDust[bottom][i - 1];
}
amountOfFineDust[bottom][1] = 0;
}
private static void spreadDust() {
while (!dusts.isEmpty()) {
fineDust now = dusts.poll();
if(now.amount < 5) continue;
int amountOfSpread = now.amount / 5;
int cnt = 0;
for (int d = 0; d < 4; d++) {
int nx = now.x + dx[d];
int ny = now.y + dy[d];
if(nx < 0 || ny < 0 || R <= nx || C <= ny) continue;
if(amountOfFineDust[nx][ny] == -1) continue;
amountOfFineDust[nx][ny] += amountOfSpread;
cnt++;
}
amountOfFineDust[now.x][now.y] -= (amountOfSpread * cnt);
}
}
private static void checkDust() {
dusts = new LinkedList<>();
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (amountOfFineDust[i][j] == -1 || amountOfFineDust[i][j] == 0) continue;
dusts.add(new fineDust(i, j, amountOfFineDust[i][j]));
}
}
}
}
'์๊ณ ๋ฆฌ์ฆ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ๋ฐฑ์ค / BOJ 17135 ] ์บ์ฌ ๋ํ์ค ( ์๋ฐ / JAVA ) (0) | 2022.01.31 |
---|---|
[ ๋ฐฑ์ค / BOJ 16236 ] ์๊ธฐ ์์ด ( ์๋ฐ/JAVA ) (0) | 2022.01.30 |
[ ๋ฐฑ์ค / BOJ 21610 ] ๋ง๋ฒ์ฌ ์์ด์ ๋น๋ฐ๋ฆฌ๊ธฐ ( JAVA ) (0) | 2022.01.30 |
[ ๋ฐฑ์ค / BOJ 16234 ] ์ธ๊ตฌ ์ด๋ ( JAVA ) (0) | 2022.01.30 |
[ ๋ฐฑ์ค / BOJ 1487 ] ๋ฌผ๊ฑด ํ๊ธฐ ( ์๋ฐ / JAVA ) (0) | 2021.11.24 |