Repository navigation
Expand file tree
/
Copy pathbrace.java
More file actions
48 lines (46 loc) · 1.43 KB
/
Copy pathbrace.java
File metadata and controls
48 lines (46 loc) · 1.43 KB
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
package swordToOffer;
import java.util.Scanner;
public class brace {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int col = sc.nextInt();
int n = sc.nextInt();
int[][] sea = new int[row][col];
int count = 0;
for(int i = 0; i < n; i++) {
int r = sc.nextInt();
int c = sc.nextInt();
if(r < 0 || r >= row || c < 0 || c >=col) {
if(i != n -1)
System.out.print(count + " ");
else System.out.print(count);
continue;
}
if(sea[r][c] == 1) {
if(i != n -1)
System.out.print(count + " ");
else System.out.print(count);
continue;
}
count++;
sea[r][c] = 1;
if(c - 1 > -1 && sea[r][c-1] == 1) {
if(count != 1)count--;
}
if(c + 1 < col && sea[r][c+1] == 1) {
if(count != 1)count--;
}
if(r - 1 > -1 && sea[r - 1][c] == 1) {
if(count != 1)count--;
}
if(r + 1 < row && sea[r + 1][c] == 1) {
if(count != 1)count--;
}
if(i != n -1)
System.out.print(count + " ");
else System.out.print(count);
}
sc.close();
}
}