Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[SW Expert Academy] 1868. 파핑파핑 지뢰찾기 본문

SW_Expert

[SW Expert Academy] 1868. 파핑파핑 지뢰찾기

코드와이 2021. 3. 9. 01:14

 

문제링크

swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=4&contestProbId=AV5LwsHaD1MDFAXc&categoryId=AV5LwsHaD1MDFAXc&categoryType=CODE&problemTitle=&orderBy=PASS_RATE&selectCodeLang=JAVA&select-1=4&pageSize=10&pageIndex=2&&&&&&&&&&

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

package D4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class 파핑파핑_지뢰찾기 {
	
	static char[][] map;
	static int n, ans;
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		int T = Integer.parseInt(br.readLine());
		
		for(int tc = 1 ; tc <= T ; tc++) {
			sb.append("#").append(tc).append(" ");
			
			n = Integer.parseInt(br.readLine());
			map = new char[n][n];
			
			for(int i = 0 ; i < n ; i++) {
				map[i] = br.readLine().toCharArray();
			}
			ans = 0;
			
			for(int i = 0 ; i < n ; i++) {
				for(int j = 0 ; j < n ; j++) {
					if(map[i][j] == '.' && check(i,j) == 0) {
						ans += 1;
						zero_bomb(i,j);
//						for(char[] a: map) System.out.println(Arrays.toString(a));
					}
				}
			}
			for(int i = 0 ; i < n ; i++) {
				for(int j = 0 ; j < n ; j++) {
					if(map[i][j]=='.') ans += 1;
				}
			}
			sb.append(ans).append("\n");
		}
		sb.setLength(sb.length()-1);
		System.out.println(sb);
	}
	
	static int[] dr = {-1, -1, -1, 0, 0, 1, 1, 1};
	static int[] dc = {-1, 0, 1, -1, 1, -1, 0, 1};
	public static void bomb(int r, int c) {
		
		int sum = 0;
		for(int d = 0 ; d < 8 ; d++) {
			int nr = r + dr[d];
			int nc = c + dc[d];
			if(nr >= 0 && nr < n && nc >= 0 && nc < n && map[nr][nc] == '*') sum += 1;
		}

		map[r][c] = (char) (sum+'0');
		if(map[r][c] == '0') zero_bomb(r, c);
	}
	
	public static int check(int r, int c) {
		int sum = 0;
		for(int d = 0 ; d < 8 ; d++) {
			int nr = r + dr[d];
			int nc = c + dc[d];
			if(nr >= 0 && nr < n && nc >= 0 && nc < n && map[nr][nc] == '*') sum += 1;
		}
		return sum;
	}
	
	public static void zero_bomb(int r, int c) {
		map[r][c] = '0';
		for(int d = 0 ; d < 8 ; d++) {
			int nr = r + dr[d];
			int nc = c + dc[d];
			if(nr >= 0 && nr < n && nc >= 0 && nc < n && map[nr][nc] == '.') bomb(nr,nc);
		}
	}
}