#시뮬레이션 #simulation
특정 위치를 선택하면 해당 위치의 배열값 만큼 십자 모양이 결정되고, 그 모양 만큼 값을 없앤 뒤에 터진 모양 만큼 칸들이 아래로 떨어진다.
기억할 만한 점은 총 2가지이다.
Math.abs(row - i) + Math.abs(col - j) <= k : 🔷마름모 모양(x == centerX || y == centerY) && Math.abs(x - centerX) + Math.abs(y - centerY) < bombRange; : ✝️십자모양(|x1 - x2| + |y1 - y2|)row 의 값을 배열의 끝 부분인 N-1 에서 부터 0 까지 하나씩 감소시키며, 터진 부분(BLANK)를 제외한 값들을 tempArr 에 채워주고, tempArr 을 다시 원래의 배열인 arr 에 값을 넣어준다.import java.util.*;
public class Main {
private static final int BLANK = 0;
private static int N = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] arr = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
int r = sc.nextInt()-1;
int c = sc.nextInt()-1;
sc.close();
N = n;
bomb(arr, r, c);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
return;
}
private static void bomb(int[][] arr, int x, int y) {
int length = arr[x][y]-1;
// for (int i = x-length; i <= x+length; ++i) {
// if (isInRange(i,y)) {
// arr[i][y] = 0;
// }
// }
// for (int j = y-length; j <= y+length; ++j) {
// if (isInRange(x,j)) {
// arr[x][j] = 0;
// }
// }
int bombRange = arr[x][y];
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (isInBombRange(x,y,i,j, bombRange)) {
arr[i][j] = BLANK;
}
}
}
for (int j = 0; j < N; ++j) {
gravity(arr, j);
}
}
private static void gravity(int[][] arr, int y) {
int[] tempArr = new int[N];
int endOfTempArr = N-1;
for (int i = N-1; i >= 0; --i) {
if (arr[i][y] != BLANK) {
tempArr[endOfTempArr--] = arr[i][y];
}
}
for (int i = N-1; i >= 0; --i) {
arr[i][y] = tempArr[i];
}
}
private static boolean isInBombRange(int centerX, int centerY, int x, int y, int bombRange) {
return (centerX == x || centerY == y) && (Math.abs(x-centerX) + Math.abs(y-centerY) < bombRange);
}
private static boolean isInRange(int x, int y) {
return 0 <= x && x < N && 0 <= y && y < N;
}
}