공기청정기 순환 부분의 인덱스를 잘 조절하는 것이 필요
import java.util.Scanner;
public class Main {
static int N, M, T;
static int[][] map, temp, cnt, airMap;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int airX1=0;
static int airX2=0;
static int result;
static void initMap(){
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
temp[i][j] = 0;
cnt[i][j] = 0;
if(map[i][j] == -1){
temp[i][j] = -1;
cnt[i][j] = -1;
if(airX1 == 0){
airX1 = i;
} else {
airX2 = i;
}
}
}
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
T = sc.nextInt();
map = new int[N][M];
temp = new int[N][M];
cnt = new int[N][M];
airMap = new int[N][M];
for(int y=0; y<N; y++){
for(int x=0; x<M; x++){
map[y][x] = sc.nextInt();
temp[y][x] = 0;
cnt[y][x] = 0;
if(map[y][x] == -1){
temp[y][x] = -1;
cnt[y][x] = -1;
if(airX1 == 0){
airX1 = y;
} else {
airX2 = y;
}
}
}
}
for(int i=0; i<T; i++){
initMap();
for(int j=0; j<N*M; j++){
int y = j/M;
int x= j%M;
int value;
int count=0;
// -1도 건너 뛰어야함 나중에 추가 해주기
//확산되는 양
value = map[y][x]/5;
for(int k=0; k<4; k++){
int nextY = y+dy[k];
int nextX = x+dx[k];
// -1인것도 제외 식 추가해 줘야함
if(nextY >= 0 && nextX >= 0 && nextY < N && nextX < M){
if(temp[nextY][nextX] == -1) continue;
temp[nextY][nextX] += value;
count++;
}
}
if(cnt[y][x] != -1){
cnt[y][x] = count;
}
}
for(int a=0; a<N*M; a++){
int y = a/M;
int x = a%M;
if(map[y][x] == -1) continue;
map[y][x] = map[y][x] - (map[y][x]/5)*cnt[y][x] + temp[y][x];
}
// map 복사
for(int d=0; d<N; d++){
for(int f=0; f<M; f++){
airMap[d][f] = map[d][f];
}
}
airCleaner();
result = 0;
for(int a=0; a<N; a++){
for(int b=0; b<M; b++){
result += map[a][b];
}
}
}
System.out.println(result+2);
}
static void airCleaner() {
for(int i=0; i<2; i++){
if(i == 0){ // 공기청정기 위
int ay = airX1;
int ax = 0;
//아래로 땡기기
for(int y = ay-1; y > 0; y--) {
map[y][ax] = map[y-1][ax];
}
//오른쪽
for(int x = 0; x < M -1 ; x++) {
map[0][x] = map[0][x + 1];
}
//밑으로
for(int y = 0; y < ay; y++) {
map[y][M-1] = map[y+1][M-1];
}
//왼쪽
for(int x = M-1; x > ax +1 ; x--) {
map[ay][x] = map[ay][x - 1];
}
map[ay][ax+ 1] = 0;
}
if(i == 1){ // 공기청정기 아래
int ay = airX2;
int ax = 0;
//아래로 땡기기
for(int y = ay+1; y < N-1; y++) {
map[y][ax] = map[y+1][ax];
}
//오른쪽
for(int x = 0; x < M -1 ; x++) {
map[N-1][x] = map[N-1][x + 1];
}
//위
for(int y = N -1 ; y > ay -1; y--) {
map[y][M-1] = map[y-1][M-1];
}
//왼쪽
for(int x = M-1; x > ax +1 ; x--) {
map[ay][x] = map[ay][x - 1];
}
map[ay][ax+ 1] = 0;
}
}
}
}
'알고리즘' 카테고리의 다른 글
백준 15686 - 치킨배달 (0) | 2020.05.11 |
---|---|
백준 14500 - 테트로미노 (0) | 2020.05.11 |
SWEA - 요리사 (0) | 2020.05.11 |
백준 14502 - 연구소(새 풀이) (0) | 2020.05.11 |
SWEA - 벌꿀채취 (0) | 2020.05.11 |