-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path241203-14890.java
124 lines (119 loc) · 4.25 KB
/
241203-14890.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int L = Integer.parseInt(st.nextToken());
int[][] map = new int[N][N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
int answer = 0;
for (int i = 0; i < N; i++) {
boolean isPossible = true;
int blockedJ = -1;
for (int j = 0; j < N - 1; j++) {
if (map[i][j] == map[i][j + 1]) {
continue;
}
if (map[i][j] - map[i][j + 1] == 1) {
for (int k = 2; k <= L; k++) {
if (j + k >= N) {
isPossible = false;
break;
}
if (map[i][j + 1] != map[i][j + k]) {
isPossible = false;
break;
}
}
j += L - 1;
blockedJ = j + 1;
if (blockedJ + 1 < N && map[i][blockedJ] < map[i][blockedJ + 1]) {
isPossible = false;
}
if (!isPossible) {
break;
}
} else if (map[i][j] - map[i][j + 1] == -1) {
for (int k = 1; k < L; k++) {
if (j - k < 0 || blockedJ >= j - k) {
isPossible = false;
break;
}
if (map[i][j] != map[i][j - k]) {
isPossible = false;
break;
}
}
if (!isPossible) {
break;
}
} else {
isPossible = false;
break;
}
}
if (isPossible) {
answer++;
}
}
for (int j = 0; j < N; j++) {
boolean isPossible = true;
int blockedI = -1;
for (int i = 0; i < N - 1; i++) {
if (map[i][j] == map[i + 1][j]) {
continue;
}
if (map[i][j] - map[i + 1][j] == 1) {
for (int k = 2; k <= L; k++) {
if (i + k >= N) {
isPossible = false;
break;
}
if (map[i + 1][j] != map[i + k][j]) {
isPossible = false;
break;
}
}
i += L - 1;
blockedI = i + 1;
if (blockedI + 1 < N && map[blockedI][j] < map[blockedI + 1][j]) {
isPossible = false;
}
if (!isPossible) {
break;
}
} else if (map[i][j] - map[i + 1][j] == -1) {
for (int k = 1; k < L; k++) {
if (i - k < 0 || blockedI >= i - k) {
isPossible = false;
break;
}
if (map[i][j] != map[i - k][j]) {
isPossible = false;
break;
}
}
if (!isPossible) {
break;
}
} else {
isPossible = false;
break;
}
}
if (isPossible) {
answer++;
}
}
System.out.print(answer);
}
}