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
| class Solution { public int numRookCaptures(char[][] board) { int m = board.length; int n = board[0].length; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (board[i][j] == 'R') { return dfs(i, j, board); } } } return 0; }
private int dfs(int i, int j, char[][] board) { int result = 0; for (int l = j + 1; l < board[0].length; l++) { if (board[i][l] == 'B') { break; } else if (board[i][l] == '.') { continue; } else { result++; break; } } for (int l = j - 1; l >= 0; l--) { if (board[i][l] == 'B') { break; } else if (board[i][l] == '.') { continue; } else { result++; break; } } for (int l = i + 1; l < board[0].length; l++) { if (board[l][j] == 'B') { break; } else if (board[l][j] == '.') { continue; } else { result++; break; } } for (int l = i - 1; l >= 0; l--) { if (board[l][j] == 'B') { break; } else if (board[l][j] == '.') { continue; } else { result++; break; } } return result; } }
|