LeetCode #999 AvailableCapturesForRook. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Available Captures for Rook.
Memory Usage: 8.4 MB, less than 100.00% of C++ online submissions for Available Captures for Rook.
LeetCode #999
Q.
On an 8 x 8 chessboard, there is one white rook. There also may be empty squares, white bishops, and black pawns. These are given as characters 'R', '.', 'B', and 'p' respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.
The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies. Also, rooks cannot move into the same square as other friendly bishops.
Return the number of pawns the rook can capture in one move.
8x8 체스보드에서, 한개의 흰색 룩이 있다. 비어있는 사각형들이 있고, 흰색 비숍 그리고 검은색 폰들도 있다. 이것들은 문자 'R' (룩), '.' (비어있음), 'B' (흰색 비숍), 'p' (검은색 폰) 이다. 대문자는 흰색 말들을 나타내고, 소문자들은 검은색 말들을 나타낸다.
룩은 체스의 룰대로 움직인다. 네개의 방향 (동서남북)을 정해서 멈추고 싶을 때까지 가는데, 보드 끝까지 가거나 적군 말을 잡을 수 있다. 또한, 같은 편 비숍을 통과해서 갈수는 없다.
룩이 한번의 움직임으로 잡을 수 있는 경우의 검은 폰의 갯수를 세어봐라.
Example 1:
Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation: In this example the rook is able to capture all the pawns.
Example 2:
Input: [[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 0
Explanation: Bishops are blocking the rook to capture any pawn.
Example 3:
Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation: The rook can capture the pawns at positions b5, d6 and f5.
Note:
- board.length == board[i].length == 8
- board[i][j] is either 'R', '.', 'B', or 'p'
- There is exactly one cell with board[i][j] == 'R'
Process
// Process
//1. Input board 2d vector
//2. Find 'R' (Rook)
//3. Check 4 direction of Rook
//4. Return black pawn count
//5. Finish
// 처리과정
//1. 보드 2차원 벡터 입력받는다.
//2. 'R' 룩의 위치 좌표를 찾는다.
//3. 룩 위치에서 4방향의 검은 폰을 잡는 개수를 확인한다.
//4. 세어진 개수를 반환한다.
//5. 끝낸다.
Code.. lemme see example code!!!
코드.. 예제코드를 보자!!!
// Process
//1. Input board 2d vector
//2. Find 'R' (Rook)
//3. Check 4 direction of Rook
//4. Return black pawn count
//5. Finish
class Solution {
public:
int numRookCaptures(vector<vector<char>>& board) {
int count = 0;
int xCoord = 0;
int yCoord = 0;
this->findWhiteRook(board, xCoord, yCoord);
this->findCapturedBlackPawn(board, xCoord, yCoord, count);
return count;
}
private:
void findWhiteRook(vector<vector<char>>& board, int& xCoord, int& yCoord) {
bool isDone = false;
for (int i = 0; !isDone && i < board.size(); ++i) {
for (int j = 0; !isDone && j < board[i].size(); ++j) {
if (board[i][j] == 'R') {
xCoord = i;
yCoord = j;
isDone = true;
}
}
}
}
void findCapturedBlackPawn(vector<vector<char>>& board, const int xCoord, const int yCoord, int& count) {
bool isDone = false;
for (int i = xCoord + 1; !isDone && i < board.size(); ++i) {
if (board[i][yCoord] != '.') {
isDone = true;
if (board[i][yCoord] == 'p')
++count;
}
}
isDone = false;
for (int i = xCoord - 1; !isDone && i >= 0; --i) {
if (board[i][yCoord] != '.') {
isDone = true;
if (board[i][yCoord] == 'p')
++count;
}
}
isDone = false;
for (int j = yCoord + 1; !isDone && j < board[0].size(); ++j) {
if (board[xCoord][j] != '.') {
isDone = true;
if (board[xCoord][j] == 'p')
++count;
}
}
isDone = false;
for (int j = yCoord- 1; !isDone && j >= 0; --j) {
if (board[xCoord][j] != '.') {
isDone = true;
if (board[xCoord][j] == 'p')
++count;
}
}
}
};
Something else you might like...?
2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법
2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법