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;
}
}
'Algorithm > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Silver I/JAVA] 3187 ์์น๊ธฐ ๊ฟ (0) | 2024.12.19 |
---|---|
[Silver I/JAVA] 2667 ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ (1) | 2024.12.13 |
[Silver I/JAVA] 2583 ์์ญ ๊ตฌํ๊ธฐ (0) | 2024.12.12 |
[Silver II/JAVA] 2210 ์ซ์ํ ์ ํ (0) | 2024.12.12 |
[Silver IV/JAVA] 1388 ๋ฐ๋ฅ์ฅ์ (2) | 2024.12.10 |