forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_999.java
79 lines (76 loc) · 3.13 KB
/
_999.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
package com.fishercoder.solutions;
public class _999 {
public static class Solution1 {
int[] directions = new int[]{0, 1, 0, -1, 0};
public int numRookCaptures(char[][] board) {
int m = board.length;
int n = board[0].length;
int rowR = -1;
int colR = -1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'R') {
rowR = i;
colR = j;
break;
}
}
}
int count = 0;
for (int i = 0; i < 4; i++) {
int neighborRow = rowR + directions[i];
int neighborCol = colR + directions[i + 1];
if (neighborRow >= 0 && neighborRow < m
&& neighborCol >= 0 && neighborCol < n
&& board[neighborRow][neighborCol] != 'B') {
if (directions[i] == 0 && directions[i + 1] == 1) {
while (neighborCol < n) {
if (board[neighborRow][neighborCol] == 'p') {
count++;
break;
} else if (board[neighborRow][neighborCol] == 'B') {
break;
} else {
neighborCol++;
}
}
} else if (directions[i] == 1 && directions[i + 1] == 0) {
while (neighborRow < m) {
if (board[neighborRow][neighborCol] == 'p') {
count++;
break;
} else if (board[neighborRow][neighborCol] == 'B') {
break;
} else {
neighborRow++;
}
}
} else if (directions[i] == 0 && directions[i + 1] == -1) {
while (neighborCol >= 0) {
if (board[neighborRow][neighborCol] == 'p') {
count++;
break;
} else if (board[neighborRow][neighborCol] == 'B') {
break;
} else {
neighborCol--;
}
}
} else {
while (neighborRow >= 0) {
if (board[neighborRow][neighborCol] == 'p') {
count++;
break;
} else if (board[neighborRow][neighborCol] == 'B') {
break;
} else {
neighborRow--;
}
}
}
}
}
return count;
}
}
}