import java.util.*;
import java.lang.*;
import java.io.*;

class Main {
    static int[] dx = new int[]{0, 1, 1, 1, 0, -1, -1, -1};
    static int[] dy = new int[]{1, 1, 0, -1, -1, -1, 0, 1};
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int H = Integer.parseInt(st.nextToken());
        int W = Integer.parseInt(st.nextToken());
        
        char[][] grid = new char[H][W];
        int[][] dist = new int[H][W];
        
        Queue<int[]> q = new LinkedList<>();

        boolean hasBlack = false;

        for (int i=0; i<H; i++) {
            String s = br.readLine();
            for (int j=0; j<W; j++) {
                grid[i][j] = s.charAt(j);
            }
        }

        for (int t = 0; t < 2; t++) {
            ArrayList<int[]> black = new ArrayList<>();
            ArrayList<int[]> white = new ArrayList<>(); // set이 더 나을 듯
            for (int i=0; i<H; i++) {
                for (int j=0; j<W; j++) {
                    for (int d = 0; d<8; d++) {
                        if (grid[i][j] == '#') {
                            white.add(new int[]{i,j});
                            int ny = i+dy[d];
                            int nx = j+dx[d];
                            if(0 <= ny && ny < H && 0 <= nx && nx < W && grid[ny][nx] == '.') {
                                black.add(new int[]{ny, nx});
                            }
                        }
                    }
                }   
            }
            for (int[] b : black) grid[b[0]][b[1]] = '#';
            for (int[] w : white) grid[w[0]][w[1]] = '.';
        }

        for (int i=0; i<H; i++) {
            for (int j=0; j<W; j++) {
                dist[i][j] = -1;
                if(grid[i][j] == '#') {
                    q.offer(new int[]{i,j});
                    dist[i][j] = 0;    
                    hasBlack = true;
                } 
            }
        }

        while(!q.isEmpty()) {
            int[] current = q.poll();
            int y = current[0];
            int x = current[1];
            for (int d=0; d<8; d++) {
                int ny = y + dy[d];
                int nx = x + dx[d];
            

                if (0<= ny && ny < H && 0 <= nx && nx < W) {
                    if (dist[ny][nx] == -1) {
                        dist[ny][nx] = dist[y][x] + 1;
                        q.offer(new int[]{ny, nx});
                    }
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        for (int i=0; i<H; i++) {
            for (int j=0; j<W; j++) {
                if (dist[i][j] % 2 == 0) {
                    sb.append('#');
                } else {
                    sb.append('.');
                }
            }
            sb.append('\n');
        }
        System.out.print(sb);
        
        
        

        
        
        
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: