forked from ckcz123/codejam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA.java
83 lines (77 loc) · 2.62 KB
/
A.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
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* APAC 2015 Round C Problem A: Minesweeper
* Check README.md for explanation.
*/
public class Main {
private String solve(Scanner scanner) {
int n=scanner.nextInt();
char[][] chars=new char[n][n];
for (int i=0;i<n;i++) chars[i]=scanner.next().toCharArray();
int[][] nums=new int[n][n];
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
if (chars[i][j]=='*') nums[i][j]=-1;
else {
for (int x=i-1;x<=i+1;x++) {
for (int y=j-1;y<=j+1;y++) {
if (x>=0 && x<n && y>=0 && y<n && chars[x][y]=='*')
nums[i][j]++;
}
}
}
}
}
int cnt=0;
while (true) {
// check 0
int zx=-1, zy=-1;
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
if (nums[i][j]==0) {
zx=i;zy=j;break;
}
}
if (zx!=-1) break;
}
if (zx==-1) break;
cnt++;
Queue<Integer> queue=new LinkedList<>();
queue.offer(zx); queue.offer(zy);
nums[zx][zy]=-1;
while (!queue.isEmpty()) {
int x=queue.poll(), y=queue.poll();
for (int dx=x-1;dx<=x+1;dx++) {
for (int dy=y-1;dy<=y+1;dy++) {
if (dx>=0 && dx<n && dy>=0 && dy<n && nums[dx][dy]!=-1) {
if (nums[dx][dy]==0) {queue.offer(dx); queue.offer(dy);}
nums[dx][dy]=-1;
}
}
}
}
}
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
if (nums[i][j]!=-1)
cnt++;
}
}
return String.valueOf(cnt);
}
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream("output.txt"));
Scanner scanner=new Scanner(System.in);
int times=scanner.nextInt();
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}