일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- geometry
- SQL
- bit manipulation
- Class
- 코테
- string
- database
- Tree
- Matrix
- 구현
- Data Structure
- Method
- array
- implement
- two pointers
- 자바
- 코딩테스트
- hash table
- dynamic programming
- Math
- Number Theory
- java
- greedy
- Binary Tree
- simulation
- 파이썬
- sorting
- Counting
- Binary Search
- Stack
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 999. Available Captures for Rook 본문
1. Input
1) char[][] board
- board는 8*8 체스보드를 나타낸다.
- ‘R': 룩. board에 딱 1개뿐이다. 상하좌우 중 한 방향을 선택해 가고싶은 만큼 이동할 수 있다.
- 'B': 비숍. 비숍에 막히면 룩은 이동할 수 없다.
- ‘p: 폰. 룩이 잡을 수 있는 말이다.
- ‘.': 보드의 빈 공간을 뜻한다.
2. Output
1) 룩(R)이 잡을 수 있는 폰의 개수를 반환
3. Constraint
1) board.length == 8
2) board[i].length == 8
3) board에는 'R', '.', 'B', 'p' 이외의 문자는 없다.
4. Example

Input: board = [[".",".",".",".",".",".",".","."], -> Output: 3
[".",".",".","p",".",".",".","."],
[".",".",".","p",".",".",".","."],
["p","p",".","R",".","p","B","."],
[".",".",".",".",".",".",".","."],
[".",".",".","B",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".",".",".",".",".","."]]
설명: 룩이 이동해서 잡을 수 있는 폰은 빨간색으로 표시된 것이다.
5. Code
1) 첫 코드(2023/06/06)
class Solution {
public int numRookCaptures(char[][] board) {
int row = 0, col = 0;
for(int i=0 ; i<8 ; i++)
for(int j=0 ; j<8 ; j++)
if(board[i][j]=='R'){
row = i; col = j; break;
}
int ans = 0;
int x = row;
while(--x>=0){
if(board[x][col]=='p'){
ans++; break;
} else if(board[x][col]=='B')
break;
}
x = row;
while(++x<8){
if(board[x][col]=='p'){
ans++; break;
} else if(board[x][col]=='B')
break;
}
x = col;
while(--x>=0){
if(board[row][x]=='p'){
ans++; break;
} else if(board[row][x]=='B')
break;
}
x = col;
while(++x<8){
if(board[row][x]=='p'){
ans++; break;
} else if(board[row][x]=='B')
break;
}
return ans;
}
}
- 100%, 49%
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[백준 온라인 저지] 2133. 타일 채우기 (0) | 2023.06.07 |
---|---|
[LeetCode/Easy] 1002. Find Common Characters (0) | 2023.06.06 |
[LeetCode/Easy] 965. Univalued Binary Tree (0) | 2023.06.06 |
[LeetCode/Easy] 938. Range Sum of BST (0) | 2023.06.06 |
[LeetCode/Easy] 914. X of a Kind in a Deck of Cards (0) | 2023.06.05 |