코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static Queue<int[]> cloud = new ArrayDeque<>();
static int N, M;
static int[] dy = {0, -1, -1, -1, 0, 1, 1, 1};
static int[] dx = {-1, -1, 0, 1, 1, 1, 0, -1};
static int[][] arr;
static boolean[][] visit;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
arr = new int[N + 1][N + 1];
for (int i = 1; i <= N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 1; j <= N; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
cloud.add(new int[]{N, 1});
cloud.add(new int[]{N - 1, 1});
cloud.add(new int[]{N, 2});
cloud.add(new int[]{N - 1, 2});
for (int i = 1; i <= M; i++) {
st = new StringTokenizer(br.readLine());
int direction = Integer.parseInt(st.nextToken());
int distance = Integer.parseInt(st.nextToken());
useMagic(direction, distance);
fillQueue();
}
System.out.println(allWaterSum());
}
private static int allWaterSum() {
int sum = 0;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
sum += arr[i][j];
return sum;
}
private static void fillQueue() {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (arr[i][j] >= 2 && !visit[i][j]) {
cloud.add(new int[]{i, j});
arr[i][j] -= 2;
}
}
}
}
private static void useMagic(int direction, int distance) {
int size = cloud.size();
for (int i = 1; i <= size; i++) {
int[] current = cloud.poll();
int pointY = current[0] + dy[direction - 1] * (distance % N);
int pointX = current[1] + dx[direction - 1] * (distance % N);
pointY = changePoint(pointY);
pointX = changePoint(pointX);
arr[pointY][pointX] += 1;
cloud.add(new int[]{pointY, pointX});
}
visit = new boolean[N + 1][N + 1];
while (!cloud.isEmpty()) {
int[] current = cloud.poll();
visit[current[0]][current[1]] = true;
for (int i = 1; i <= 7; i += 2) {
int nextY = current[0] + dy[i];
int nextX = current[1] + dx[i];
if (nextY > N || nextY < 1 || nextX > N || nextX < 1) continue;
;
if (arr[nextY][nextX] >= 1) arr[current[0]][current[1]] += 1;
}
}
}
private static int changePoint(int point) {
if (point > N) {
point = point - N;
} else if (point < 1) {
point = point + N;
}
return point;
}
}
- 조건이 복잡한 구현문제.
- 구름을 추가할 때 이전에 구름이 사라졌던 위치를 visit배열로 기록해두고 처리했다.
- 큐를 사용해 구름의 위치들을 저장했다.