Hamutaro - Hamtaro 4

Algorithm/BOJ

[Silver III/JAVA] 16956 ๋Š‘๋Œ€์™€ ์–‘

carsumin 2024. 12. 25. 19:12

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

 

 

์กฐ๊ฑด
  • ํฌ๊ธฐ RxC์ธ ๋ชฉ์žฅ
  • ์–‘์€ ์ด๋™ํ•˜์ง€ ์•Š์Œ, ๋Š‘๋Œ€๋Š” ์ธ์ ‘ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ ๊ฐ€๋Šฅ
  • ์šธํƒ€๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด์„œ ๋Š‘๋Œ€๊ฐ€ ์–‘์ด ์žˆ๋Š” ์นธ์œผ๋กœ ๋ชป๊ฐ€๋„๋ก ํ•ด์•ผ ํ•จ
  • ์šธํƒ€๋ฆฌ ๊ฐœ์ˆ˜ ์ œํ•œ ์—†์Œ

 

ํ’€์ด ๊ณผ์ •
  • ์–‘ ์ฃผ๋ณ€์œผ๋กœ
    • 1. ๋Š‘๋Œ€-์–‘ ์ธ์ ‘ํ•œ๊ฐ€?
    • 2. ๋นˆ๊ณณ ์ฒดํฌํ•ด์„œ ์šธํƒ€๋ฆฌ ์„ค์น˜ ์—ฌ๋ถ€ ๊ฒฐ์ •

 

์ „์ฒด ์ฝ”๋“œ
import java.io.*;
import java.util.*;

public class Main{
    static int R, C;
    static char[][] map;
    static int[] dx = {-1,1,0,0};
    static int[] dy = {0,0,-1,1};
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        String[] str = br.readLine().split(" ");
        R = Integer.parseInt(str[0]);
        C = Integer.parseInt(str[1]);
        
        map = new char[R][C];
        
        //map์ดˆ๊ธฐํ™”
        for(int i=0; i<R; i++){
            String s = br.readLine();
            for(int j=0; j<C; j++){
                map[i][j] = s.charAt(j);
            }
        }
        
        //์šธํƒ€๋ฆฌ ์„ค์น˜ ์—ฌ๋ถ€ ์ฒดํฌ
        if(!canFence()){
            System.out.println(0);
        }else{
            System.out.println(1);
            //์ถœ๋ ฅ
            for(int i=0; i<R; i++){
                for(int j=0; j<C; j++){
                    System.out.print(map[i][j]);
                }
                System.out.println();
            }
        }
    }
    
    private static boolean canFence(){
        
        for(int i=0; i<R; i++){
            for(int j=0; j<C; j++){
                if(map[i][j] == 'S'){ //์–‘์ด ์žˆ์„ ๊ฒฝ์šฐ
                    for(int k=0; k<4; k++){
                        int nx = dx[k] + i;
                        int ny = dy[k] + j;
                        
                        if(nx >= 0 && nx < R && ny >= 0 && ny < C){
                            if(map[nx][ny] == 'W'){ //๋Š‘๋Œ€-์–‘ ์ธ์ ‘ํ•  ๊ฒฝ์šฐ ์šธํƒ€๋ฆฌ x
                                return false;
                            }else if(map[nx][ny] == '.'){ //๋นˆ๊ณณ์— ์šธํƒ€๋ฆฌ ์„ค์น˜
                                map[nx][ny] = 'D';
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
}