알고리즘

백준 10711번 모래성 (JAVA)

박카스마시며코딩 2023. 3. 9. 13:20

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

 

10711번: 모래성

첫째 줄에는 모래성의 가로세로 격자 크기 H, W가 주어진다. (1 ≤ H, W ≤ 1,000) 그 다음 H줄에 걸쳐 W개의 문자로 모래성의 상태를 나타내는 문자가 들어온다. 각 문자는 1~9 사이의 숫자, 또는 '.' 이

www.acmicpc.net

 

 

 

package BOJ.bfs;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_10711 {
    private static final char EMPTY = '.';
    private static final int[] DY = {-1,0,1,0,-1,1,1,-1};
    private static final int[] DX = {0,1,0,-1,1,1,-1,-1};
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        Function<String,Integer> stoi = Integer::parseInt;
        int n = stoi.apply(st.nextToken());
        int m = stoi.apply(st.nextToken());
        int[][] map = new int[n][m];
        Queue<int[]> q = new LinkedList<>();
        for(int i = 0 ; i < n ; i++){
            String command = br.readLine();
            for(int j = 0 ; j < m ; j++){
                char ch = command.charAt(j);
                if(ch == EMPTY){
                    map[i][j] = 0;
                    q.offer(new int[] {i,j});
                }else{
                    map[i][j] = ch - '0';
                }
            }
        }
        int time = 0;
        int[][] cnt = new int[n][m];
        boolean[][] used = new boolean[n][m];
        Queue<int[]> bomb = new LinkedList<>();
        while(!q.isEmpty()){
            int size = q.size();
            for(int s = 0 ; s < size ; s++){
                int[] now = q.poll();
                for(int i = 0 ; i < 8 ; i++){
                    int ny = now[0] + DY[i];
                    int nx = now[1] + DX[i];
                    if(checkBound(ny,nx,n,m)){
                        cnt[ny][nx]++;
                        if(map[ny][nx] != 0 && !used[ny][nx] && cnt[ny][nx] >= map[ny][nx]){
                            used[ny][nx] = true;
                            bomb.offer(new int[]{ny,nx});
                        }
                    }
                }
            }
            if(bomb.isEmpty()){
                break;
            }
            while(!bomb.isEmpty()){
                int[] now = bomb.poll();
                map[now[0]][now[1]] = 0;
                q.offer(now);
            }
            time++;
        }
        System.out.println(time);
    }

    private static boolean checkBound(int y, int x, int n, int m) {
        if(y >= 0 && y < n && x >= 0 && x < m){
            return true;
        }
        return false;
    }
}