-
Notifications
You must be signed in to change notification settings - Fork 8
/
AvailableCapturesForRook.java
83 lines (75 loc) · 2.13 KB
/
AvailableCapturesForRook.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
package matrix;
// Source : https://leetcode.com/problems/available-captures-for-rook/
// Id : 999
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-06-05
// Topic : Matrix
// Level : Easy
// Other :
// Tips :
// Result : 100.00% 99.95%
public class AvailableCapturesForRook {
public int numRookCaptures(char[][] board) {
int rowR = 0, columnR = 0, count = 0;
boolean found = false;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if (board[i][j] == 'R') {
rowR = i;
columnR = j;
found = true;
break;
}
}
if (found)
break;
}
if (!found)
return 0;
if (columnR != 0) {
for (int i = columnR - 1; i >= 0; i--) {
if (board[rowR][i] == 'B') {
break;
}
if (board[rowR][i] == 'p') {
count++;
break;
}
}
}
if (columnR != board.length - 1) {
for (int i = columnR + 1; i < board.length; i++) {
if (board[rowR][i] == 'b') {
break;
}
if (board[rowR][i] == 'p') {
count++;
break;
}
}
}
if (rowR != 0) {
for (int i = rowR - 1; i >= 0; i--) {
if (board[i][columnR] == 'b') {
break;
}
if (board[i][columnR] == 'p') {
count++;
break;
}
}
}
if (rowR != board.length - 1) {
for (int i = rowR + 1; i < board.length; i++) {
if (board[i][columnR] == 'b') {
break;
}
if (board[i][columnR] == 'p') {
count++;
break;
}
}
}
return count;
}
}