본문 바로가기

Algorithm/Leet Code

LeetCode #999 AvailableCapturesForRook. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

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:

  1. board.length == board[i].length == 8
  2. board[i][j] is either 'R', '.', 'B', or 'p'
  3. 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/08/14 - [Life/Item review] - Mi Band 4 review, 미밴드4 후기, 장점, 단점, 리뷰, 한글, global review, 미밴드4 글로벌 후기, 리뷰, 구매, 사용방법, setting, 세팅, ProsNCons

 

2019/08/27 - [Programming] - 개발자 선배들에게서 배운 것들. Things I Learnt from a Senior Software Engineer. 코딩 잘하는 방법, how to code well, 소프트웨어,프로그래머,programmer

 

2019/09/17 - [Algorithm/Leet Code] - LeetCode #841 KeysAndRooms. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/08/28 - [Algorithm/Leet Code] - LeetCode #821 ShortestDistanceToACharacter. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접..

2019/08/25 - [Algorithm/Leet Code] - LeetCode #191 NumberOf1Bits. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트

2019/08/19 - [Algorithm/Leet Code] - LeetCode #942 DI StringMatch. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트

2019/07/21 - [Algorithm/Leet Code] - LeetCode #590 N-aryTreePostorderTraversal. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,..

2019/07/20 - [Algorithm/Leet Code] - LeetCode #589 N-aryTreePreorderTraversal. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,..

2019/07/17 - [Algorithm/Leet Code] - LeetCode #905 SortArrayByParity. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리..

2019/07/13 - [Algorithm/Leet Code] - LeetCode #994 RottingOranges. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트

2019/06/25 - [Algorithm/Leet Code] - LeetCode #766 ToeplitzMatrix. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트

 

 

2019/04/14 - [Programming/C++] - C++ Math - sqrt (square root, 제곱근, 루트). stl, math.h, 씨쁠쁠, example code, 예제코드

 

 

2018/10/19 - [Programming/Design Pattern ] - Design pattern - Prototype (디자인패턴 - 프로토타입) / Java C++ C#

 

 

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #60 sudoku. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #59 spiralNumbers. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/08 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #58 messageFromBinaryCode. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/07 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #57 fileNaming. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

 

 

2018/12/28 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 - (#9, Tell, Don't Ask)

2018/12/26 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 (1)

 

 

2019/01/14 - [Programming/Java] - 자바 메모리 누수 체크/확인/고치는 방법, Memory leak check/fix in Java application, cleanCode/좋은코드/oop/객체지향

 

 

2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/16 - [Life/Health care] - Finasteride 피나스테라이드,탈모약 usage/side effects/dosage 효능/부작용/효과/sexual effect/두타스테라이드/프로페시아/propecia/finpecia/카피약/copy drug/hair loss

2019/02/25 - [Life/Health care] - Folic Acid 엽산 vitaminB9,비타민M usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법

2019/03/03 - [Life/Health care] - Vitamin B1, Thiamine, 비타민 B1, 티아민 usage/side effects/dosage 효능/부작용/성인,소아 용법