
소스코드:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br;
static BufferedWriter bw;
static int width, height, T;
static int[][] board;
static int machine;
static Queue<int[]> munziQueue;
static final int[] dy = new int[]{-1, 1, 0, 0};
static final int[] dx = new int[]{0, 0, -1, 1};
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
height = Integer.parseInt(st.nextToken());
width = Integer.parseInt(st.nextToken());
T = Integer.parseInt(st.nextToken());
munziQueue = new LinkedList<>();
board = new int[height][width];
boolean isFind = false;
for (int y = 0; y < height; y++) {
st = new StringTokenizer(br.readLine());
for (int x = 0; x < width; x++) {
board[y][x] = Integer.parseInt(st.nextToken());
if (board[y][x] == -1 && !isFind) {
isFind = true;
machine = y;
}
if (board[y][x] > 0) {
munziQueue.add(new int[]{y, x});
}
}
}
br.close();
int curTime = 0;
while (curTime != T) {
int[][] temp_board = new int[height][width];
while (!munziQueue.isEmpty()) {
int[] cur = munziQueue.poll();
int curY = cur[0];
int curX = cur[1];
int munziValue = board[curY][curX];
int fly_munziValue = munziValue / 5;
int fly_cnt = 0;
if (fly_munziValue == 0) {
continue;
}
for (int i = 0; i < 4; i++) {
int nextY = curY + dy[i];
int nextX = curX + dx[i];
if (isValidPos(nextY, nextX)) {
temp_board[nextY][nextX] += fly_munziValue;
fly_cnt++;
}
}
board[curY][curX] -= fly_munziValue * fly_cnt;
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (board[y][x] == -1) {
continue;
}
if (temp_board[y][x] > 0) {
board[y][x] += temp_board[y][x];
}
}
}
turnOn();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (board[y][x] == -1) {
continue;
}
if (board[y][x] > 0) {
munziQueue.add(new int[]{y, x});
}
}
}
curTime++;
}
int result = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (board[y][x] > 0) {
result += board[y][x];
}
}
}
bw.write(result + "\n");
bw.close();
}
public static boolean isValidPos(int y, int x) {
if (((y == machine) && (x == 0)) || ((y == (machine + 1)) && (x == 0))) {
return false;
}
return (y >= 0 && y < height && x >= 0 && x < width);
}
public static void turnOn() {
int topY = machine; // 위
int downY = machine + 1; // 아래
for (int y = topY - 1; y > 0; y--) {
board[y][0] = board[y - 1][0];
}
for (int x = 0; x < width - 1; x++) {
board[0][x] = board[0][x + 1];
}
for (int y = 0; y < topY; y++) {
board[y][width - 1] = board[y + 1][width - 1];
}
for (int x = width - 1; x > 1; x--) {
board[topY][x] = board[topY][x - 1];
}
board[topY][1] = 0;
for (int y = downY + 1; y < height - 1; y++) {
board[y][0] = board[y + 1][0];
}
for (int x = 0; x < width - 1; x++) {
board[height - 1][x] = board[height - 1][x + 1];
}
for (int y = height - 1; y > downY; y--) {
board[y][width - 1] = board[y - 1][width - 1];
}
for (int x = width - 1; x > 1; x--) {
board[downY][x] = board[downY][x - 1];
}
board[downY][1] = 0;
}
}
글의 내용 중 잘못된 점이나 수정이 필요한 부분, 혹은 궁금한 사항이 있다면 언제든 댓글로 남겨주시면 감사하겠습니다.
여러분의 피드백은 더 나은 글을 작성하는 데 큰 도움이 됩니다. 감사합니다.
'알고리즘 > 백준' 카테고리의 다른 글
| [JAVA-자바] 1865번: 웜홀 (0) | 2025.05.21 |
|---|---|
| [JAVA-자바] 1238번: 파티 (0) | 2025.05.19 |
| [JAVA-자바] 14938번: 서강그라운드 (0) | 2025.05.16 |
| [JAVA-자바] 14502번: 연구소 (0) | 2025.05.15 |
| [JAVA-자바] 12851번: 숨바꼭질 2 (0) | 2025.05.14 |