Let's make this one less mysterious. For 999. Available Captures for Rook, the solution in this repository is mainly a two pointers solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are numRookCaptures.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02On 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.
03
04The 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.
05
06Return the number of pawns the rook can capture in one move.
07**/
08
09//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Available Captures for Rook.
10//Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for Available Captures for Rook.
11
12class Solution {
13public:
14 int numRookCaptures(vector<vector<char>>& board) {
15 int Rx = 0, Ry = 0;
16
17 for(int i = 0; i < board.size(); i++){
18 for(int j = 0; j < board[0].size(); j++){
19 if(board[i][j] == 'R'){
20 Ry = i;
21 Rx = j;
22 }
23 }
24 }
25
26 int num_captures = 0;
27
28 int cur = 0;
29 char pos = '\0';
30 string dirs = "rlud";
31
32 for(int i = 0; i < dirs.size(); i++){
33 char dir = dirs[i];
34 if(dir=='r' or dir=='l'){
35 cur = Rx;
36 }else if(dir=='u' or dir=='d'){
37 cur = Ry;
38 }
39 //cout << "dir: " << dir << ", Rx: " << Rx << ", Ry: " << Ry << endl;
40 while(cur>=0 and cur < board.size()){
41 if(dir=='r' or dir=='l'){
42 //cout << "Ry: " << Ry << ", cur: " << cur << ", pos: " << pos << endl;
43 pos = board[Ry][cur];
44 }else if(dir=='u' or dir=='d'){
45 //cout << "cur: " << cur << ", Rx: " << Rx << ", pos: " << pos << endl;
46 pos = board[cur][Rx];
47 }
48
49 if(pos=='p'){
50 num_captures++;
51 break;
52 }else if(pos=='B'){
53 break;
54 }
55
56 if(dir=='r' or dir=='d'){
57 cur++;
58 }else if(dir=='l' or dir=='u'){
59 cur--;
60 }
61 }
62 }
63
64 return num_captures;
65 }
66};
Cost