0. 단순 구현 문제
1. 구름을 이동하고 -> 대각선 물 양을 증가시키고 -> 새 구름을 생성한다 의 반복
2. 구름이 사라진 칸은 boolean 배열로 관리한다
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main{
static int[][] dir = new int[][]{
{0,-1},{-1,-1},{-1,0},{-1,1},
{0,1},{1,1},{1,0},{1,-1}
};
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int n = pint(st.nextToken());
int m = pint(st.nextToken());
int sum=0;
int[][]map = new int[n][n];
boolean[][]chk;
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < n; j++) {
map[i][j]=pint(st.nextToken());
}
}
Queue<int[]> cloud = new LinkedList<>();
cloud.add(new int[] {n-1,0}); cloud.add(new int[] {n-1,1});
cloud.add(new int[] {n-2,0}); cloud.add(new int[] {n-2,1});
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int d = pint(st.nextToken())-1;
int s = pint(st.nextToken());
chk=new boolean[n][n];
//구름 이동 페이즈
int len = cloud.size();
for (int j = 0; j < len; j++) {
int[] cur = cloud.poll();
int newX = cur[0]+dir[d][0]*s;
int newY = cur[1]+dir[d][1]*s;
newX = ((newX%n)+n)%n;
newY = ((newY%n)+n)%n;
//1증가
map[newX][newY]++;
cloud.add(new int[] {newX,newY});
}
//이동 후 물양 증가 페이즈
for (int j = 0; j < len; j++) {
int[] cur = cloud.poll();
if(cur[0]-1>=0 && cur[1]-1>=0 && map[cur[0]-1][cur[1]-1]>0) {
map[cur[0]][cur[1]]++;
}if(cur[0]-1>=0 && cur[1]+1<n && map[cur[0]-1][cur[1]+1]>0) {
map[cur[0]][cur[1]]++;
}if(cur[0]+1<n && cur[1]-1>=0 && map[cur[0]+1][cur[1]-1]>0) {
map[cur[0]][cur[1]]++;
}if(cur[0]+1<n && cur[1]+1<n && map[cur[0]+1][cur[1]+1]>0) {
map[cur[0]][cur[1]]++;
}
chk[cur[0]][cur[1]]=true;
}
//신규 구름 생성 페이즈
for (int j = 0; j < n; j++) {
for (int j2 = 0; j2 < n; j2++) {
if(!chk[j][j2] && map[j][j2]>=2) {
map[j][j2]-=2;
cloud.add(new int[] {j,j2});
}
}
}
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
sum+=map[x][y];
}
}System.out.println(sum);
}
static int pint(String s) {
return Integer.parseInt(s);
}
}
'알고리즘 문제 풀이 > BOJ 골드' 카테고리의 다른 글
[백준] 23288 - 주사위 굴리기 2 (0) | 2021.11.03 |
---|---|
[백준] 19236 - 청소년 상어 (0) | 2021.11.03 |
[백준] 20058 - 마법사 상어와 파이어스톰 (0) | 2021.10.25 |
[백준] 1339 - 단어수학 (0) | 2021.10.10 |
[백준] 14938 - 서강그라운드 (0) | 2021.10.03 |
[백준] 14891 - 톱니바퀴 (0) | 2021.09.27 |
[백준] 16919 - 봄버맨 2 (0) | 2021.09.25 |