#P6806. [CEOI 2020] 象棋世界

[CEOI 2020] 象棋世界

Description

Chess World is a board with RR rows and CC columns, where RCR \geq C. All rows are numbered from 11 to RR, and all columns are numbered from 11 to CC.

In Chess World, there are five types of pieces: pawn, rook, bishop, queen, and king. Unlike the real world, chivalry has died in Chess World, so there are no knights.

In Chess World, each piece can make one move according to the following rules:

  • A pawn can only move one step toward increasing row numbers (from row rr to row r+1r+1), and its column does not change.
  • A rook can move only horizontally or vertically.
  • A bishop can move only diagonally.
  • A queen can move horizontally, vertically, or diagonally.
  • A king can move to any of the eight adjacent squares.

To help you understand, the figure below shows the legal move range for each piece. Here, X represents squares that the piece can move to.

Recently, many strange things have happened in Chess World: some pieces may be hijacked by an unknown force and then disappear from Chess World. In this situation, all pieces want to reach their destinations as soon as possible. They also want to know, among all ways that use the minimum number of moves, how many such ways there are. Two ways are considered different if and only if there exists at least one move where the square passed through is different.

In this problem, you need to solve the following task: a piece starts from column c1c_1 in row 11, and needs to reach column cRc_R in row RR. You are given the type of the piece and the values of c1,cRc_1, c_R. You need to compute the minimum number of moves needed, and the number of ways to reach the destination with the minimum number of moves.

Input Format

The first line contains three integers R,C,QR, C, Q, representing the number of rows, the number of columns, and the number of queries.

The next QQ lines each contain a letter TT, representing the piece type, and two integers c1,cRc_1, c_R, meaning the start is at column c1c_1 in row 11, and the end is at column cRc_R in row RR.

The mapping between letters and piece types is as follows:

Letter Piece Type
P\texttt{P} Pawn
R\texttt{R} Rook
B\texttt{B} Bishop
Q\texttt{Q} Queen
K\texttt{K} King

Output Format

For each query, output two integers. The first integer is the minimum number of moves needed to go from the start to the end. The second integer is the number of ways to go from the start to the end using the minimum number of moves.

Since the number of ways can be very large, output it modulo 109+710^9+7.

In particular, if it is impossible to reach the destination from the start, output one line 0 0.

8 8 5
P 1 2
R 4 8
Q 2 3
B 3 6
K 5 5
0 0
2 2
2 5
2 2
7 393

Hint

All test cases satisfy: 1Q10001 \leq Q \leq 1000, 2C10002 \leq C \leq 1000, CR109C \leq R \leq 10^9, $T \in \{\texttt{P},\texttt{R},\texttt{Q},\texttt{B},\texttt{K}\}$, 1c1,cRC1 \leq c_1, c_R \leq C.

The constraints for each subtask are as follows:

Subtask ID Score Constraints
11 00 Sample
22 88 T{P,R,Q}T \in \{\texttt{P},\texttt{R},\texttt{Q}\}
33 1515 T=BT=\texttt{B}, C,R100C, R \leq 100
44 2222 T=BT=\texttt{B}
55 T=KT=\texttt{K}, C,R100C, R \leq 100, Q50Q \leq 50
66 88 T=KT=\texttt{K}, C,R100C, R \leq 100
77 1515 T=KT=\texttt{K}, C100C \leq 100
88 2020 T=KT=\texttt{K}
99 77 No special constraints

Translated by ChatGPT 5