Dazzling 개발 노트

[백준] 16918 - 봄버맨 (Java) 본문

Algorithm/백준

[백준] 16918 - 봄버맨 (Java)

dj._.dazzling 2023. 7. 17. 22:43

[백준] 16918 - 봄버맨 (Java)

문제

https://www.acmicpc.net/problem/16918

풀이/후기

처음 생각보다 막히지 않고 잘 풀었는데,

조금씩 정답 출력과 다른 부분이 있어 문제를 찾느라 시간이 좀 걸렸다.

bombFunc()에서 if (arr[cx][cy] != 2) 부분을 해주지 않아

자꾸 폭탄이 있는데 터지지 않는(?) 현상이 발생했다.

디버깅으로 열심히 찾아내서 뿌듯함...!

 

그리고 자바로 제출한 사람들과 비교하니 제출 시간이 좀 걸리길래 StringBuilder로 sysout을 바꾸니 확실히 시간이 확 줄었다!!! 이것 또한 뿌듯!

코드

package GraphTheory;

import java.io.*;

public class Problem16918 {
	
	static int R,C,N;
	static int[][] arr;
	static int[][] dir = {{0,1}, {0,-1}, {1,0}, {-1,0}};
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] input = br.readLine().split(" ");
		
		R = Integer.parseInt(input[0]);
		C = Integer.parseInt(input[1]);
		N = Integer.parseInt(input[2]);
		
		arr = new int[R][C];
		
		for (int i=0; i<R; i++) {
			input = br.readLine().split("");
			for (int j=0; j<C; j++) {
				//빈칸이면 0, 폭탄이 있으면 1
				if (input[j].equals(".")) {
					arr[i][j] = 0;
				} else if (input[j].equals("O")) {
					arr[i][j] = 1;
				}
			}
		}
		
		int ind = 1;
		while(ind++ <= N) {
			int tempT = ind % 2;
			
			for (int i=0; i<R; i++) {
				for (int j=0; j<C; j++) {
					if (tempT == 1) {
						if (arr[i][j] == 1) {
							arr[i][j] = 2;
						} else arr[i][j] = 1;
					} else if (tempT == 0) {
						if (arr[i][j] == 2) {
							bombFunc(i,j);
						}
					}
					
				}
			}
			
		}
		
		StringBuilder sb = new StringBuilder();
		for (int i=0; i<R; i++) {
			for (int j=0; j<C; j++) {
				if (arr[i][j] == 0) {
					sb.append(".");
				} else {
					sb.append("O");
				}
				
			}
			sb.append("\n");
		}
		System.out.println(sb);
		
	}
	
	static void bombFunc(int x, int y) {
		arr[x][y] = 0;
		for (int i=0; i<4; i++) {
			int cx = x + dir[i][0];
			int cy = y + dir[i][1];
			if (cx >= 0 && cy >= 0 && cx < R && cy < C ) {
				if (arr[cx][cy] != 2) {
					arr[cx][cy] = 0;
				}
			}
		}
	}

}

Commit

https://github.com/allrightDJ0108/CodingTestStudy/commit/fefd4f9cdbb8abdde8227ec1fe298a69bb343b26

참고

오늘은 검색 없이 풀이 완료vV