17144번: 미세먼지 안녕!
미세먼지를 제거하기 위해 구사과는 공기청정기를 설치하려고 한다. 공기청정기의 성능을 테스트하기 위해 구사과는 집을 크기가 R×C인 격자판으로 나타냈고, 1×1 크기의 칸으로 나눴다. 구사
www.acmicpc.net
- 내용
공기청정기(1열에 2칸을 차지하고있음)랑 A(r,c)위치에 미세먼지 양이 적혀있다.
A(r,c)에 있는 미세먼지 양은 턴 한번마다 네방향(상, 하, 좌, 우)로 퍼질 수 있고 퍼지는 양은 A(r,c)/5만큼 퍼진다.(소수점 버림) 퍼진 뒤 A(r,c)의 미세먼지 양은 A(r,c) - A(r,c)/5 X (퍼진방향수)로 바뀐다.
공기청정기는 위에 2칸이 있다고 있는데 위칸은 시계 반대방향으로 불고, 아래칸은 시계 방향으로 분다.
한 턴마다 미세먼지가 먼저 퍼지고 공기청정기의 바람이 분다.
이때, T턴 후 남아있는 미세먼지의 양을 출력한다.
- 생각
T초마다 미세먼지 퍼뜨리고 공기청정기 돌리면 된다. (딱히 문법 사용할거 없이 구현하면 될 것 같다.)
미세먼지 양은 네 방향으로 퍼뜨려도 총 미세먼지의 양은 변화가 없다. (공기청정기가 불어서 미세먼지가 줄어드는 2칸만 총양에서 빼주면 됨.)
- 코드
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Main {
static int[] x = { -1, 1, 0, 0 };
static int[] y = { 0, 0, -1, 1 };
static int[][] array, temp;
static int r, c, answer, airCleaner1, airCleaner2;
public static void main(String[] args) throws Exception {
SetData();
System.out.println(answer);
}
// 데이터
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
answer = 2;
r = in.nextInt();
c = in.nextInt();
int testcase = in.nextInt();
array = new int[r][c];
temp = new int[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
array[i][j] = in.nextInt();
answer += array[i][j];
if (array[i][j] < 0) {
if (airCleaner1 == 0) {
airCleaner1 = i;
} else {
airCleaner2 = i;
}
}
}
}
while (testcase-- > 0) {
SpreadFineDust();
PlayAirCleaner();
CopyMap(temp, array);
}
}
private static void SpreadFineDust() {
for(int i = 0; i < r; i++)
Arrays.fill(temp[i], 0);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
temp[i][j] += array[i][j];
if(temp[i][j] == -1) continue;
if(array[i][j] == 0) continue;
for (int direction = 0; direction < 4; direction++) {
int R = i + x[direction];
int C = j + y[direction];
if (R < 0 || C < 0 || R >= r || C >= c) continue;
if (array[R][C] == -1) continue;
temp[R][C] += (array[i][j] / 5);
temp[i][j] -= (array[i][j] / 5);
}
}
}
}
private static void PlayAirCleaner() {
// 위쪽 공기청정기는 반시계방향
int top = airCleaner1;
answer -= temp[top-1][0];
for (int x = top - 1; x > 0; x--) {
temp[x][0] = temp[x - 1][0];
}
for (int y = 0; y < c - 1; y++) {
temp[0][y] = temp[0][y + 1];
}
for (int x = 0; x < top; x++) {
temp[x][c - 1] = temp[x + 1][c - 1];
}
for (int y = c - 1; y > 1; y--) {
temp[top][y] = temp[top][y - 1];
}
temp[top][1] = 0;
// 아래쪽 공기청정기는 시계 방향
int bottom = airCleaner2;
answer -= temp[bottom + 1][0];
for (int x = bottom + 1; x < r - 1; x++) {
temp[x][0] = temp[x + 1][0];
}
for (int y = 0; y < c - 1; y++) {
temp[r - 1][y] = temp[r - 1][y + 1];
}
for (int x = r - 1; x > bottom; x--) {
temp[x][c - 1] = temp[x - 1][c - 1];
}
for (int y = c - 1; y > 1; y--) {
temp[bottom][y] = temp[bottom][y - 1];
}
temp[bottom][1] = 0;
}
static void CopyMap(int[][] one,int[][] two) {
for(int i=0;i<one.length;++i) {
System.arraycopy(one[i], 0, two[i], 0, two[i].length);
}
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 14503번 : 로봇 청소기 (0) | 2021.03.13 |
---|---|
[BOJ/JAVA] 백준 13460번 : 구슬 탈출 2 (0) | 2021.03.12 |
[BOJ/JAVA] 백준 1525번 : 퍼즐 (0) | 2021.03.10 |
[BOJ/JAVA] 백준 1963번 : 소수 경로 (0) | 2021.03.08 |
[BOJ/JAVA] 백준 2666번 : 벽장문의 이동 (0) | 2021.03.04 |