Notice
Recent Posts
문제 풀이 및 개발 공간
[백준] 17144번 미세먼지 안녕! (gold 4 본문
import com.sun.security.jgss.GSSUtil;
import java.io.*;
import java.util.*;
public class Main {
static int[][] map;
static int[] numx = {-1, 1, 0, 0};
static int[] numy = {0, 0, -1, 1};
static int r;
static int c;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
r = Integer.valueOf(st.nextToken());
c = Integer.valueOf(st.nextToken());
int t = Integer.valueOf(st.nextToken());
int frog = 0;
//공기청정기.
ArrayList<int[]> a = new ArrayList<>();
map = new int[r + 1][c + 1];
//map초기화.
for (int i = 1; i <= r; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 1; j <= c; j++) {
frog = Integer.valueOf(st.nextToken());
if (frog == -1) {
a.add(new int[]{j, i});
}
map[i][j]=frog;
}
}
//시간의 흐름.
for (int i = 0; i < t; i++) {
flow(r, c);
air(a.get(0)[0], a.get(0)[1],0);
rair(a.get(1)[0], a.get(1)[1],0);
map[a.get(0)[1]][a.get(0)[0]]=-1;
map[a.get(1)[1]][a.get(1)[0]]=-1;
}
//답구하기
int answer = 0;
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
if (map[i][j] > 0) {
answer+=map[i][j];
}
}
}
bw.write(answer + "");
bw.flush();
}
//확산
public static void flow(int r, int c) {
int[][] temp = new int[r + 1][c + 1];
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
if (map[i][j] > 0) {
int how = 0;
for (int k = 0; k < 4; k++) {
int ty = i + numy[k];
int tx = j + numx[k];
if (ty < 1 || ty > r || tx < 1 || tx > c || map[ty][tx] == -1) {
continue;
}
how++;
//확산되어 들어온 미세먼지 기록.
temp[ty][tx] += map[i][j] / 5;
}
//확산으로 인해 줄어든 미세먼지 기록
temp[i][j] -= how * (map[i][j] / 5);
}
}
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
map[i][j] += temp[i][j];
}
}
}
//정화 시계방향
public static void air(int x, int y, int f) {
int temp=0;
temp=xr(x,y,f);
temp=yu(c,y,temp,1);
temp=xl(c,1,temp);
yd(1,1,temp,y);
}
//정화 반시계
public static void rair(int x, int y,int f) {
int temp=0;
temp=xr(x,y,f);
temp=yd(c,y,temp,r);
temp=xl(c,r,temp);
yu(1,r,temp,y);
}
//미세먼지 마지막꺼 반환
public static int xr(int x, int y, int f) {
x = 2;
int temp = map[y][x];
int temp2 = 0;
while (x < c) {
x++;
temp2 = map[y][x];
map[y][x] = temp;
temp = temp2;
}
map[y][2]=0;
return temp;
}
public static int xl(int x, int y, int f) {
x = c-1;
int temp = f;
int temp2 = 0;
while (x >= 1) {
temp2 = map[y][x];
map[y][x] = temp;
temp = temp2;
x--;
}
return temp;
}
public static int yu(int x, int y, int f, int ty) {
y-=1;
int temp = f;
int temp2 = 0;
while (y >= ty) {
temp2 = map[y][x];
map[y][x]=temp;
temp = temp2;
y--;
}
return temp;
}
public static int yd(int x, int y, int f, int ty) {
y+=1;
int temp = f;
int temp2 = 0;
while (y <= ty) {
temp2 = map[y][x];
map[y][x]=temp;
temp = temp2;
y++;
}
return temp;
}
}
'백준공부 > java' 카테고리의 다른 글
[백준] 20950번 미술가 미미 (silver 2 (0) | 2024.03.08 |
---|---|
[백준] 20949번 효정과 새 모니터 (silver 5 (0) | 2024.03.08 |
[백준] 11723번 집합 (silver 5 (0) | 2024.03.07 |
[백준] 7662번이중 우선순위 큐 (gold 4 (0) | 2024.03.07 |
[백준] 2638번 치즈 (gold 3 (0) | 2024.03.06 |