신경쓸부분 몇개 있는 구현문제
확산시 주의사항.
확산은 모두 동시에 일어난다. 순차적으로 하면 다른곳에서 확산이 일어나기전에 영향을 주기 때문에
확산을 위해 같은 크기의 배열에 복사해서 해당 배열에 업데이트를 하고 붙여넣는 식으로 해주어야 함.
공청기구현
일단 화살표 방향 따라서 구현해보려고 하니 한칸씩 밀고 방향꺾고 생각하다 너무 복잡하게 해버렸다..
공청기에서 바람이 나오는 첫 다음칸으로 이동하고 현재칸이 0인지, 값이있는지 구분하고
전 칸에서 밀려왔는지 오지않았는지 여부에 따라 구분해 각 분기점에따라
이번칸을 0으로 업데이트할지, 이전칸에서 저장한 값을 넣을지 결정하며 다음칸이 공청기칸인지 매번 확인한다.
다른분들의 코드를 살펴보니 반대방향으로 생각해서 화살표 역방향으로 공청기 빨아들이는 바로 앞 칸부터 시작하여
역방향으로 한칸씩 끌어당기면 마지막칸은 자연스럽게 사라져 공청기에 흡수한 구현효과를 내고
간단한 for문 4개로 해결이 가능했다..
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
public class Main {
/*
공청기가 쓸고갈 때 임시 저장할 변수를 하나 선언하자.
*/
static int r, c, t;
static int[][] arr = new int[100][100];
static int[][] arr2 = new int[100][100];
static int beforedust;
static int afterdust;
static class point {
public int row;
public int col;
}
static point[] cleaner = new point[2];
static int[] rowc = {0, 0, -1, 1};
static int[] colc = {1, -1, 0, 0};
static void spread() {
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
arr2[i][j]=arr[i][j];
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (arr[i][j] >= 5) {
for (int k = 0; k < 4; k++) {
int afterrow = rowc[k] + i;
int aftercol = colc[k] + j;
if (afterrow >= 0 && afterrow < r && aftercol >= 0 && aftercol < c && arr[afterrow][aftercol] != -1) {
arr2[afterrow][aftercol] += (arr[i][j] / 5);
arr2[i][j] -= (arr[i][j] / 5);
}
}
}
}
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
arr[i][j]=arr2[i][j];
}
}
}
static void clean() {
for (int i = 0; i < 2; i++) {
int temprow = cleaner[i].row;
int tempcol = cleaner[i].col;
if (c == 1) {
if (i == 0) temprow--;
else temprow++;
} else {
tempcol++;
}
int[] row = null;
int[] col = null;
if (i == 0) {
row = new int[]{0, -1, 0, 1};
col = new int[]{1, 0, -1, 0};
} else if (i == 1) {
row = new int[]{0, 1, 0, -1};
col = new int[]{1, 0, -1, 0};
}
int index = 0;
/*
현 위치가 0일때와 0이아닐때
0일때
걍다음으로
값이 있을 때
before에 전 칸 값이 들어있다.
내가 안밀려왔다 (check=0) before에 현재칸 값. 현재칸에 0 대입 before이 0이라면 check=0 아니라면 check=1;
내가 밀려왔다. 내 칸 값을 temp에 저장 후 현재칸에 before 값 대입. before에 temp 값 저장. before이 0이라면 check=0 아니라면 check=1;
*/
/*
2 1 1 0 before = 2
0 2 1 0 before = 1
0 2 1 1 before = 0
*/
beforedust=0;
int check=0;
while (!(temprow == cleaner[i].row && tempcol == cleaner[i].col)) {
int afterrow = temprow + row[index];
int aftercol = tempcol + col[index];
if (afterrow >= 0 && afterrow < r && aftercol >= 0 && aftercol < c) {
if(check==0){
beforedust = arr[temprow][tempcol];
arr[temprow][tempcol]=0;
}
else{
int temp = arr[temprow][tempcol];
arr[temprow][tempcol]=beforedust;
beforedust=temp;
}
if( beforedust==0) check=0;
else check=1;
temprow=afterrow;
tempcol=aftercol;
if(arr[afterrow][aftercol]==-1){
break;
}
} else {
index++;
}
}
}
}
public static void main(String[] args) throws Exception {
int cleanercount = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
StringTokenizer token = new StringTokenizer(str);
r = Integer.parseInt(token.nextToken());
c = Integer.parseInt(token.nextToken());
t = Integer.parseInt(token.nextToken());
for (int i = 0; i < r; i++) {
str = br.readLine();
token = new StringTokenizer(str);
for (int j = 0; j < c; j++) {
arr[i][j] = Integer.parseInt(token.nextToken());
if (arr[i][j] == -1) {
point temppoint = new point();
temppoint.row = i;
temppoint.col = j;
cleaner[cleanercount++] = temppoint;
}
}
}
for (int q = 0; q < t; q++) {
int result=0;
spread();
clean();
result=0;
}
int result=0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
//System.out.print(arr[i][j]+" ");
if(arr[i][j]!=-1){
result+=arr[i][j];
}
}
//System.out.println();
}
System.out.println(result);
}
}