티스토리 뷰
leetcode.com/problems/available-captures-for-rook/
Available Captures for Rook - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제
화이트 룩 'R'은 한개, 화이트 비숍 'B'과 블랙 폰 'p' 여러개와 빈공간 '.'로 구성되어있는 8*8 체스보드가 있다.
룩의 이동 ->
동서남북으로 몇칸이든 이동가능.
블랙 폰을 잡거나 화이트비숍에 의해 이동하지 못할 수 있음.
룩 차례에 한번에 잡을 수 있는 폰의 개수는?
어떻게 풀까
먼저 룩, 폰, 비숍의 위치를 저장한다.
for문을 폰 크기만큼 돌린다.
이때 룩이 폰의 위치로 이동가능한지 확인
-> 행 혹은 열이 같은지
-> 둘중 하나라도 같으면 -> 행이 같은지 -> 같은 행에 '.' 아닌거 있는지
-> 열이 같은지 -> 같은 열에 '.' 아닌거 있는지
구현 중 만난 문제/해결
행이 같을 때와 열이 같을 때만 if (isPossible) result++; 를 통해서 결과에 반영해야했는데 if(){...} else if(){...} if (isPossible) result++; 이런식으로 해버려서 틀렸었다.
마무리가 부족했던것같다ㅠ
구현
class Solution { // 999 class rowCol { int row; int col; rowCol(int row, int col) { this.row = row; this.col = col; } } public int numRookCaptures(char[][] board) { List<rowCol> pawn = new ArrayList<>(); List<rowCol> bshop = new ArrayList<>(); int result = 0; rowCol rook = new rowCol(0,0); // 폰, 비숍, 룩의 위치 등록 for (int i=0; i<board.length; i++) { for (int j=0; j<board.length; j++) { if (board[i][j] == '.') { continue; } else if (board[i][j] == 'B') { bshop.add(new rowCol(i,j)); } else if(board[i][j] == 'p') { pawn.add(new rowCol(i,j)); } else if(board[i][j] == 'R') { rook = new rowCol(i,j); } } } int rookRow = rook.row; int rookCol = rook.col; for (int i=0; i<pawn.size(); i++) { rowCol curPawn = pawn.get(i); int row = curPawn.row; int col = curPawn.col; boolean isPossible = true; if (row == rookRow) { // 행이 같은 경우 룩과 폰사이에 비숍만없으면 잡기 가능 char[] boardI = board[row]; int min = col; int max = rookCol; if (max < min) { min = rookCol; max = col; } for (int j = min+1; j<max; j++) { if (boardI[j] != '.') isPossible = false; } if (isPossible) result++; } else if (col == rookCol) { // 열이 같은 경우 룩과 폰사이에 비숍만없으면 잡기 가능 int min = row; int max = rookRow; if (max < min) { min = rookRow; max = row; } for (int j = min+1; j<max; j++) { if (board[j][col] != '.') isPossible = false; } if (isPossible) result++; } } return result; } }
'Algorithm > 릿코드(leetcode)' 카테고리의 다른 글
[릿코드] 204번 : Count Primes (0) | 2021.05.01 |
---|---|
[릿코드] 1491번 : Average Salary Excluding the Minimum and Maximum Salary (0) | 2021.04.28 |
[릿코드] 561번 : Array Partition I (0) | 2021.04.28 |
[릿코드] 989번: Add to Array-Form of Integer (0) | 2021.04.28 |
[릿코드] 717번: 1-bit and 2-bit Characters (0) | 2021.04.27 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- webhook
- 뒤늦은 1년 후기
- java
- Github
- 언제까지할수있을까
- gradle빌드
- binarySearch
- elastic ip
- leetcode 167
- leetcode 204
- leetcode 349
- ngrok
- binary search
- 1491
- leetcode 278
- leetcode 69
- LeetCode
- 백준
- Docker
- xmlpullparserexceptioin
- Leetcode717
- 릿코드
- 별찍기-10
- Jenkins
- 티스토리코드작성
- 도커
- config.xml
- leetcode 350
- leetcode 35
- 2447
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함