-
Notifications
You must be signed in to change notification settings - Fork 0
/
B头条产品变程序员.java
85 lines (77 loc) · 2.58 KB
/
B头条产品变程序员.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
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Integer[]> arr = new ArrayList<>();
while (in.hasNextLine()) {
String input = in.nextLine();
if (input.equals(""))
break;
String temp[] = input.split(" ");
Integer[] line = new Integer[temp.length];
for (int i = 0; i < temp.length; i++) {
line[i] = Integer.valueOf(temp[i]);
}
arr.add(line);
}
int res = 0;
int row = arr.size();
int col = arr.get(0).length;
LinkedList<Integer> que = new LinkedList<>();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (arr.get(i)[j] == 1) {
if ((j - 1) >= 0 && arr.get(i)[j - 1] != 0)
continue;
if ((j + 1) < col && arr.get(i)[j + 1] != 0)
continue;
if ((i + 1) < row && arr.get(i + 1)[j] != 0)
continue;
if ((i - 1) >= 0 && arr.get(i - 1)[j] != 0)
continue;
System.out.println(-1);
return;
}
if (arr.get(i)[j] == 2) {
que.add(i * col + j);
}
}
}
que.add(-1);
// for (Integer[] i : arr) {
// for (Integer j : i) {
// System.out.print(j + " ");
// }
// System.out.println();
// }
while (que.size() > 1) {
int index = que.removeFirst();
int i, j;
if (index == -1) {
res += 1;
que.add(-1);
continue;
} else {
i = index / col;
j = index % col;
}
if ((j - 1) >= 0 && arr.get(i)[j - 1] == 1) {
arr.get(i)[j - 1] = 2;
que.add((i) * col + j - 1);
}
if ((j + 1) < col && arr.get(i)[j + 1] == 1) {
arr.get(i)[j + 1] = 2;
que.add((i) * col + j + 1);
}
if ((i + 1) < row && arr.get(i + 1)[j] == 1) {
arr.get(i + 1)[j] = 2;
que.add((i + 1) * col + j);
}
if ((i - 1) >= 0 && arr.get(i - 1)[j] == 1) {
arr.get(i - 1)[j] = 2;
que.add((i - 1) * col + j);
}
}
System.out.println(res);
}
}