Available catch] [Leetcode car (one question per day)

Topic link: available catch car


Meaning of the questions: on the board of an 8 x 8, there is a white car (rook). It may also be available box, white elephant (bishop) and the black pawn (pawn). They are given to the character "R", ".",and "p". The uppercase characters indicate White, Black lowercase characters are represented.

The car moves in chess rule: choose one of four basic directions (North, East, West and South), and then move in that direction until it choose to stop, or reach the edge of the board to move to the same square capture and death on the opposite color squares. In addition, the car can not be friendly with the other (white) like into the same box.

Returns the number of death in a car to be able to capture the move.


Solution: is to understand the meaning of the title. . Explain that, find the location of the car, find death so far from the current location to the surrounding. Hit like to stop.

With a very violent methods. .


Code:

 1 class Solution {
 2 public:
 3     int numRookCaptures(vector<vector<char>>& board) {
 4         int posx,posy;
 5         int cnt = 0;
 6         for(int i = 0; i < 8 ;i++){
 7             for(int j = 0; j < 8 ;j++){
 8                 if(board[i][j] == 'R'){
 9                     posx = i;
10                     posy = j;
11                     break;
12                 }
13             }
14         }
15 
16         //
17         for(int i = posx-1; i >= 0; i--){
18             if(board[i][posy] == '.') continue;   //空方块
19             else if(board[i][posy] == 'B') BREAK ; // image 
20 is              the else  IF (Board [I] [POSY] == ' P ' ) {        // Death 
21 is                  CNT = + . 1 ;
 22 is                  BREAK ;
 23 is              }
 24          }
 25          // case 
26 is          for ( int I = POSX + . 1 ; I < . 8 ; I ++ ) {
 27              IF (Board [I] [POSY] == ' . ' ) Continue ;    // empty squares 
28             else if(board[i][posy]== 'B') break; //
29             else if(board[i][posy] == 'p'){       //
30                 cnt+=1;
31                 break;
32             }
33         }
34         //
35         for(int i = posy-1; i >= 0; i--){
36             if(board[posx][i] == '. ' ) Continue ;    // empty squares 
37 [              the else  IF (Board [POSX] [I] == ' B ' ) BREAK ; // image 
38 is              the else  IF (Board [POSX] [I] == ' P ' ) {        / / Death 
39                  CNT + = . 1 ;
 40                  BREAK ;
 41 is              }
 42 is          }
 43 is          // Right 
44 is          for ( int I = POSY + . 1 ; I <. 8 ; I ++ ) {
 45              IF (Board [POSX] [I] == ' . ' ) Continue ;    // empty squares 
46 is              the else  IF (Board [POSX] [I] == ' B ' ) BREAK ; // as 
47              the else  IF (Board [POSX] [I] == ' P ' ) {        // Death 
48                  CNT + = . 1 ;
 49                  BREAK ;
 50              }
 51 is          }
 52 is          return cnt;
53     }
54 };

 

Guess you like

Origin www.cnblogs.com/Asumi/p/12578479.html