-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution_1868_파핑파핑지뢰찾기_유승아.java
146 lines (115 loc) · 2.88 KB
/
Solution_1868_파핑파핑지뢰찾기_유승아.java
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
//파핑파핑 지뢰찾기
public class Solution_1868_파핑파핑지뢰찾기_유승아 {
static char[][] map;
static boolean[][] visited;
static int N, click;
static ArrayList<pair> al;
static int[] dy = { -1, 1, 0, 0, -1, -1, 1, 1 };
static int[] dx = { 0, 0, -1, 1, -1, 1, -1, 1 };
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(bf.readLine());
for (int tc = 1; tc <= T; tc++) {
N = Integer.parseInt(bf.readLine());
map = new char[N][N];
visited = new boolean[N][N];
al = new ArrayList<>();
click = 0;
int ans = 0;
for (int i = 0; i < N; i++) {
String str = bf.readLine();
for (int j = 0; j < N; j++) {
map[i][j] = str.charAt(j);
if (map[i][j] == '.')
click++; //숫자의 개수
}
} // intput
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (map[i][j] == '.') {
find(i, j);
}
}
}
for(pair p : al) {
int yy = p.y;
int xx = p.x;
if(!visited[yy][xx]) {
visit(yy,xx);
visited[yy][xx]=true;
click--;
ans++;
//System.out.println();
}
}
int rst = click+ans;
System.out.println("#"+tc+" "+rst);
} // end of tc
}// end of main
static void visit(int y, int x) {
visited[y][x]=true;
//click--;
//System.out.println(y + "," + x);
for (int k = 0; k < 8; k++) {
int ny = y + dy[k];
int nx = x + dx[k];
if (ny < 0 || ny > N - 1 || nx < 0 || nx > N - 1)
continue;
if (!visited[ny][nx]) {
visited[ny][nx] = true;
click--;
//System.out.println(ny + "," + nx);
if (map[ny][nx] == '0')
visit(ny, nx);
}
}
}
static void find(int y, int x) {
int bomb = 0;
for (int k = 0; k < 8; k++) {
int ny = y + dy[k];
int nx = x + dx[k];
if (ny < 0 || ny > N - 1 || nx < 0 || nx > N - 1)
continue;
if (map[ny][nx] == '*')// 지뢰면
bomb++;
}
map[y][x] = (char) (bomb + '0');
if(map[y][x] == '0')
al.add(new pair(y,x));
if (bomb == 0) {
zero(y, x);
}
}
static void zero(int y, int x) {
Queue<pair> q = new LinkedList<>();
for (int k = 0; k < 8; k++) {
int ny = y + dy[k];
int nx = x + dx[k];
if (ny < 0 || ny > N - 1 || nx < 0 || nx > N - 1 || map[ny][nx] =='*')
continue;
if (map[ny][nx] == '.')
q.add(new pair(ny, nx));
}
while (!q.isEmpty()) {
int yy = q.peek().y;
int xx = q.peek().x;
q.poll();
find(yy, xx);
}
}
public static class pair {
int y;
int x;
public pair(int y, int x) {
super();
this.y = y;
this.x = x;
}
}
}