본문 바로가기

Notice
Recent Posts
Recent Comments
«   2024/11   »
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
Tags
더보기
Archives
관리 메뉴

1868. 파핑파핑 지뢰찾기(D4) 본문

알고리즘 문제풀기/SW Expert Academy

1868. 파핑파핑 지뢰찾기(D4)

알광(Algwang) 2019. 3. 6. 16:33

문제링크 : https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5LwsHaD1MDFAXc&categoryId=AV5LwsHaD1MDFAXc&categoryType=CODE




- 소스 코드 - 

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.Scanner;
 
public class Solution1868 {
    static int num;
    static int dx[] = { -1-1-100111 };
    static int dy[] = { 01-1-11-101 };
    static int map[][];
    static boolean check[][];
    
    static void solution(int x,int y) {
        check[x][y]=true;
        for(int i=0;i<8;i++) {
            // 주변 칸이 방문하지 않은 0일 경우 해당 칸에 solution 실행 
            if(map[x+dx[i]][y+dy[i]]==0&&!check[x+dx[i]][y+dy[i]]) {
                solution(x+dx[i],y+dy[i]);
            }
            // 0의 주변 칸이 숫자일 경우 방문 처리
            else if(map[x+dx[i]][y+dy[i]]!=-1&&map[x+dx[i]][y+dy[i]]!=99&&!check[x+dx[i]][y+dy[i]]) {
                check[x+dx[i]][y+dy[i]]=true;
            }
        }
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int T = scan.nextInt();
        for (int test_case = 1; test_case <= T; test_case++) {
            num = 0;
            int N = scan.nextInt();
            map = new int[N + 2][N + 2];
            check = new boolean[N + 2][N + 2];
            //////////////////// 1 입력받기 ///////////////////////////
            for (int i = 0; i <= N + 1; i++) {
                String str="";
                if(i!=0&&i!=N+1) {
                    str=scan.next();
                }
                for (int j = 0; j <= N + 1; j++) {
                    if (i == 0 || i == N + 1 || j == 0 || j == N + 1) {
                        map[i][j] = -1// 맵 바깥
                    } else {
                        if(str.charAt(j-1)=='.') {
                            map[i][j]=1;
                        }else {
                            map[i][j]=99// 지뢰
                        }
                    }
                }
            }
            //////////////////// 2 주변 지뢰 개수 세기 ///////////////////////////
            for(int i=1;i<=N;i++) {
                for(int j=1;j<=N;j++) {
                    if(map[i][j]==1) {
                        int count=0;
                        for(int k=0;k<8;k++) {
                            if(map[i+dx[k]][j+dy[k]]==99) {
                                count++;
                            }
                        }
                        map[i][j]=count;
                    }
                }
            }
            //////////////////// 3 0들 먼저 처리하기 ///////////////////////////
            for(int i=1;i<=N;i++) {
                for(int j=1;j<=N;j++) {
                    if(map[i][j]==0&&!check[i][j]) {
                        solution(i,j);
                        num++;
                    }
                }
            }
            //////////////////// 4 남아있는 숫자들 처리하기 ///////////////////////////
            for(int i=1;i<=N;i++) {
                for(int j=1;j<=N;j++) {
                    if(map[i][j]!=0&&map[i][j]!=-1&&map[i][j]!=99&&!check[i][j]) {
                        num++;
                    }
                }
            }
            System.out.printf("#%d %d\n",test_case,num);
        }
    }
}
 
cs





Comments