-
Notifications
You must be signed in to change notification settings - Fork 112
/
0999-AvailableCapturesForRook.cs
70 lines (65 loc) · 1.88 KB
/
0999-AvailableCapturesForRook.cs
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
//-----------------------------------------------------------------------------
// Runtime: 88ms
// Memory Usage: 23.8 MB
// Link: https://leetcode.com/submissions/detail/331970361/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0999_AvailableCapturesForRook
{
public int NumRookCaptures(char[][] board)
{
(var row, var col) = FindRook(board);
int count = 0;
for (var i = row - 1; i >= 0; i--)
{
if (board[i][col] == 'p')
{
count++;
break;
}
else if (board[i][col] == 'B')
break;
}
for (var i = row + 1; i < 8; i++)
{
if (board[i][col] == 'p')
{
count++;
break;
}
else if (board[i][col] == 'B')
break;
}
for (var i = col - 1; i >= 0; i--)
{
if (board[row][i] == 'p')
{
count++;
break;
}
else if (board[row][i] == 'B')
break;
}
for (var i = col + 1; i < 8; i++)
{
if (board[row][i] == 'p')
{
count++;
break;
}
else if (board[row][i] == 'B')
break;
}
return count;
}
private (int row, int col) FindRook(char[][] board)
{
for (var i = 0; i < 8; i++)
for (var j = 0; j < 8; j++)
if (board[i][j] == 'R')
return (i, j);
return (-1, -1);
}
}
}