ALGORITHM/BOJ

[BOJ] 5212 지구 온난화

0298 2023. 6. 18. 22:18

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

 

5212번: 지구 온난화

첫째 줄에 지도의 크기 R과 C (1 ≤ R, C ≤ 10)가 주어진다. 다음 R개 줄에는 현재 지도가 주어진다.

www.acmicpc.net

2023-06-18


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.Scanner;
 
public class Main5212 {
    public static int[] dx = {-1010};
    public static int[] dy = {0-101};
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
 
        char[][] arr = new char[n][m];
 
        for(int i = 0; i < n; i++) {
            String tmp = sc.next();
            for(int j = 0; j < m; j++) {
                arr[i][j] = tmp.charAt(j);
            }
        }
 
        int sx  = 101;
        int sy = 101;
        int ex = -1;
        int ey = -1;
 
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                int count = 0;
                if(arr[i][j] == 'X') {
                    for(int k = 0; k < 4; k++) {
                        int nx = i + dx[k];
                        int ny = j + dy[k];
 
                        if(nx < 0 || ny < 0 || nx >= n || ny >= m || arr[nx][ny] == '.') count++;
                    }
                    if(count >= 3) arr[i][j] = '-';
                    else {
                        sx = Math.min(sx, i);
                        sy = Math.min(sy, j);
                        ex = Math.max(ex, i);
                        ey = Math.max(ey, j);
                    }
                }
            }
        }
 
        StringBuilder sb = new StringBuilder();
 
        for(int i = sx; i <= ex; i++) {
            for(int j = sy; j <= ey; j++) {
                if(arr[i][j] == '-') sb.append(".");
                else sb.append(arr[i][j]);
            }
            sb.append("\n");
        }
 
        System.out.println(sb.toString());
    }
}
 
cs

#문제풀이

땅 인접 4방향 중 3면 이상이 바다인 경우 바다로 만듦 

아닌 경우 직사각형 모양 만들기 위해서 좌표 체크