https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRQm6qfL0DFAUo
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
시뮬레이션의 정수를 담은 문제입니다.
주어진 문제의 조건과 순서를 따라 잘 구현하면 되지만 잘 구현하는것이 어렵습니다.
코드의 가독성을 높이기 위해 여러 단계를 나누어 함수로 구현하고
실행 시간을 줄이기 위해 재귀함수를 이용해서 이전 상태를 기억해 거기서부터 다시 탐색합니다.
상태를 기억할때는 Map이 2차원 배열임을 감안해 깊은 복사를 위해 copy() 함수를 따로 만들었습니다.
Java언어에서 N차원 배열로 모든 배열을 순회하며 복사해도 되지만 clone() 메소드가 Array에 존재해 활용했습니다.
모든 경우를 탐색해야하기 때문에 효율적인 코드를 짜는게 중요할 것 같습니다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class q5656_SWEA_벽돌깨기 {
static StringTokenizer st;
static StringBuilder sb = new StringBuilder();
static int answer, N, W, H;
static int[][] delta = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
static int[][] map;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int tc = 1; tc <= T; tc++) {
answer = Integer.MAX_VALUE;
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
W = Integer.parseInt(st.nextToken());
H = Integer.parseInt(st.nextToken());
map = new int[H][W];
for (int h = 0; h < H; h++) {
st = new StringTokenizer(br.readLine());
for (int w = 0; w < W; w++) {
map[h][w] = Integer.parseInt(st.nextToken());
}
}
dfs(0);
sb.append("#").append(tc).append(" ").append(answer).append("\n");
}
System.out.println(sb);
}
static void dfs(int count) {
if (count == N) {
answer = Math.min(answer, countBlock(map));
return;
}
int[][] save = new int[H][W];
for (int w = 0; w < W; w++) {
copy(map, save);
shoot(w);
dfs(count + 1);
copy(save, map);
}
}
static boolean isIn(int x, int y) {
if (0 <= x && x < H && 0 <= y && y < W) {
return true;
}
return false;
}
static void copy(int[][] from, int[][] to) {
for (int h = 0; h < H; h++) {
to[h] = from[h].clone();
}
}
static int countBlock(int[][] result) {
int count = 0;
for (int h = 0; h < H; h++) {
for (int w = 0; w < W; w++) {
if (result[h][w] != 0) {
count++;
}
}
}
return count;
}
static void shoot(int w) {
for (int h = 0; h < H; h++) {
if (map[h][w] != 0) {
boom(h, w);
return;
}
}
return;
}
static void boom(int x, int y) {
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[] { x, y, map[x][y]-1 });
map[x][y] = 0;
while (!queue.isEmpty()) {
int[] now = queue.poll();
int range = now[2];
for (int i = 0; i < 4; i++) {
int nX = now[0];
int nY = now[1];
for (int r = 0; r < range; r++) {
nX += delta[i][0];
nY += delta[i][1];
if (isIn(nX, nY) && map[nX][nY] != 0) {
queue.offer(new int[] { nX, nY, map[nX][nY]-1 });
map[nX][nY] = 0;
}
}
}
}
gravity();
}
static void gravity() {
Queue<Integer> queue;
for (int w = 0; w < W; ++w) {
queue = new LinkedList<>();
for (int h = H - 1; h >= 0; --h) {
if (map[h][w] > 0) {
queue.offer(map[h][w]);
}
}
for (int h = H - 1; h >= 0; --h) {
if (!queue.isEmpty()) {
map[h][w] = queue.poll();
} else {
map[h][w] = 0;
}
}
}
}
}