Šah
Time limit3sMemory limit2048 MB
Count the squares attacked by at least one knight, rook, or queen on an N by N board, where pieces attack through other pieces and their own square counts.
- Level
Easy2 of 10
- Topics
- Implementation, Matrix, Brute force
- Solved
- No attempts yet
Problem
Two chess giants, Vito and Patrik, will play a game of chess this year in front of the iconic theater on Jane Street, and the match will decide who is the greatest player of all time. Standard chess has become boring for them, so they changed the rules to make the game more interesting. Only the rules relevant to this task are described here.
The chessboard is a square matrix with rows and columns. Only the knight, rook, and queen are used. Each piece moves as it does in standard chess. A rook attacks every square in its row and column. A queen attacks every square in its row and column, and also every square along its two diagonals. A knight attacks squares that are two rows and one column away from it, or one row and two columns away. Examples of these moves are shown in the hint.
Note: Each piece also attacks the square it stands on. Pieces attack through other pieces, so a piece attacks squares by the rules above even when another piece lies between them.
Vito wants to practice quick observation. He places chess pieces on the board and then finds every attacked square. Your task is to count the attacked squares on the given board.
Input
The first line contains positive integers and (, ).
Each of the next lines contains an uppercase English letter for the piece type, which is N (knight), R (rook), or Q (queen), followed by integers and (), the row and column of that piece.
At most one piece is placed on any square of the board.
Output
Output the number of attacked squares on the board in the first and only line.
Hint
The sketches show the first and second examples. Every attacked square is marked with a dot, except the square where the piece stands.
(a) First example. 
(b) Second example. 