반응형
구현에 관한 문제이다.
완전탐색 방식으로 구현하였다.
아래 예시는 확산+클리너를 1회 작동하였을때 예시이다.
import java.util.*;
import java.io.*;
class Main {
static int Row, Col, T;
static int[][] grid;
static int[] cleanerR = new int[2], cleanerC = new int[2];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
Row = Integer.parseInt(st.nextToken());
Col = Integer.parseInt(st.nextToken());
T = Integer.parseInt(st.nextToken());
grid = new int[Row][Col];
int cleaner = 0;
for (int i = 0; i < Row; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < Col; j++) {
grid[i][j] = Integer.parseInt(st.nextToken());
if (grid[i][j] == -1) {
cleanerR[cleaner] = i;
cleanerC[cleaner++] = j;
}
}
}
for(int t=0; t<T; t++) {
perSecond();
}
int total=0;
for (int i = 0; i < Row; i++) {
for (int j = 0; j < Col; j++) {
total+=grid[i][j];
//System.out.print(grid[i][j] + " ");
}
}
System.out.println(total+2);
}
static void perSecond() {//초당 발생
spreadDust();
circulateDust();
}
static int[] rx = {0, 1, 0, -1};
static int[] ry = {-1, 0, 1, 0};
static void spreadDust() {
int[][] copy = new int[Row][Col];
for (int i = 0; i < Row; i++) {//배열복사
if (Col >= 0) System.arraycopy(grid[i], 0, copy[i], 0, Col);
}
for (int i = 0; i < Row; i++) {
for (int j = 0; j < Col; j++) {
if (copy[i][j] != 0 && copy[i][j] != -1) {//먼지있는곳
int divideCount = 0;
//4방향
for (int r = 0; r < 4; r++) {
int y = i + ry[r];
int x = j + rx[r];
if (x < 0 || y < 0 || x >= Col || y >= Row) continue;
if (copy[y][x] != -1) {
grid[y][x] += copy[i][j] / 5;
divideCount++;//몇번 나눴는지.
}
}
grid[i][j] -= copy[i][j] / 5 * divideCount;
}
}
}
}
static void circulateDust() {//순환시키기
int r = cleanerR[0];
int c = cleanerC[0];
//r행
Queue<Integer> queue = new LinkedList<>();
for (int j = 1; j < Col - 1; j++) {//x : 1~끝에서 두번째까지(위로올려야됨 맨끝은)
queue.offer(grid[r][j]);
}
for (int i = r; i > 0; i--) {//y : r~1까지
queue.offer(grid[i][Col-1]);
}
for (int j = Col - 1; j > 0; j--) {//x : 끝~1
queue.offer(grid[0][j]);
}
for (int i = 0; i < r; i++) {
queue.offer(grid[i][0]);
}
grid[r][1] = 0;
for (int j = 2; j < Col - 1; j++) {//x : 1~끝에서 두번째까지(위로올려야됨 맨끝은)
grid[r][j] = queue.remove();
}
for (int i = r; i > 0; i--) {//y : r~1까지
grid[i][Col-1] = queue.remove();
}
for (int j = Col - 1; j > 0; j--) {//x : 끝~1
grid[0][j] = queue.remove();
}
for (int i = 0; i < r; i++) {
grid[i][0] = queue.remove();
}
//하클리너
Queue<Integer> queue2 = new LinkedList<>();
r=cleanerR[1];
c=cleanerC[1];
//r행
for (int j = 1; j < Col - 1; j++) {//x : 1~끝에서 두번째까지(위로올려야됨 맨끝은)
queue2.offer(grid[r][j]);
}
for (int i = r; i < Row - 1; i++) {//y : r~1까지
queue2.offer(grid[i][Col-1]);
}
for (int j = Col - 1; j > 0; j--) {//x : 끝~1
queue2.offer(grid[Row-1][j]);
}
for (int i = Row-1; i > r; i--) {
queue2.offer(grid[i][0]);
}
grid[r][1] = 0;
for (int j = 2; j < Col - 1; j++) {//x : 1~끝에서 두번째까지(위로올려야됨 맨끝은)
grid[r][j] = queue2.remove();
}
for (int i = r; i < Row - 1; i++) {//y : r~1까지
grid[i][Col-1] = queue2.remove();
}
for (int j = Col - 1; j > 0; j--) {//x : 끝~1
grid[Row-1][j] = queue2.remove();
}
for (int i = Row-1; i > r; i--) {
grid[i][0] = queue2.remove();
}
}
}
반응형