Available catch car (3.26leetcode daily punch)

On a 8 x 8 board, 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", ".", "B" 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.
 
输入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:3
输入:[[".",".",".",".",".",".",".","."],[".","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",".","."],[".",".",".",".",".",".",".","."],[".",".",".","..... "," "," "," "," "]] as to prevent the car capture any stroke.to explain:
Output: 0

 
Ideas: the title was simply a simple traversal of the four directions can be mainly good moderation, such as catch can stop a soldier, a good moderation before being effective.
. 1  int numRookCaptures ( char ** Board, int boardSize, int * boardColSize)
 2  {
 . 3      int CNT = 0 ;
 . 4      int Row, COL;
 . 5      int I, J;
 . 6      
. 7      // Find position where the white vehicle
 8      // vehicle R
 9      // as B
 10      @ Death P
 . 11      // empty squares. 
12 is      for (I = 0 ; I < . 8 ; I ++ )
 13 is      {
 14         for(j = 0; j < 8; j++)
15         {
16             if(board[i][j] == 'R')
17             {
18                 row = i;
19                 col = j;
20                 break;
21             }
22         }
23     }
24     
25     //搜寻右边
26     for(i = col+1; i < 8; i++)
27     {
28         if(board[row][i] == 'p')
29         {
30             cnt++;
31             break;
32         }
33         if(board[row][i] == 'B')
34         break;
35     }
36 
37     //搜寻左边
38     for(i = col-1; i >= 0; i--)
39     {
40         if(board[row][i] == 'p')
41         {
42             cnt++;
43             break;
44         }
45         if(board[row][i] == 'B')
46         break;
47     }
48 
49     //搜寻下边
50     for(i = row+1; i < 8; i++)
51     {
52         if(board[i][col] == 'p')
53         {
54             cnt++;
55             break;
56         }
57         if(board[i][col] == 'B')
58         break;      
59     }
60 
61     //搜寻上边
62     for(i = row-1; i >= 0; i--)
63     {
64         if(board[i][col] == 'p')
65         {
66             cnt++;
67             break;
68         }
69         if(board[i][col] == 'B')
70         break;      
71     }
72     return cnt;
73 }

 

 
 

Guess you like

Origin www.cnblogs.com/ZhengLijie/p/12575547.html