#P9322. [EGOI 2022] Superpiece / 超级棋子
[EGOI 2022] Superpiece / 超级棋子
Background
Day 2 Problem B.
The statement is translated from EGOI2022 superpiece。
Problem Description
You have an infinite chessboard. In this problem, the board is an infinite 2D grid, and each square is denoted by , representing the row and the column. There is currently exactly one piece on the board, called the superpiece. You are given the list of legal moves of your superpiece as a non-empty string, which is a subsequence of QRBNKP. In each turn, the superpiece may move as one of the given chess pieces. The superpiece starts at . Find the minimum number of moves needed to reach .
The chess rules that may be used in this problem are as follows:
There are six kinds of pieces: queen, rook, bishop, knight, king, and pawn. Their moves are:
- Queen (
Q) can move to any square in the same row, the same column, or the same diagonal. Formally, for any integer , the queen can move from to , , , .

- Rook (
R) can move to any square in the same row or the same column. Formally, for any integer , the rook can move from to , .

- Bishop (
B) can move to any square on the same diagonal. Formally, for any integer , the bishop can move from to , .

- Knight (
N) moves in anLshape: first move two squares in one direction, then one square in a perpendicular direction. Formally, the knight can move from to , , , , , , , .

- King (
K) can move to any of the eight adjacent squares. Formally, the king can move from to , , , , , , , .

- Pawn (
P) can move one square upward. Formally, the pawn can move from to .

Note that other chess rules you may know do not apply to this problem; only use the rules listed above.
Input Format
The first line contains an integer , the number of test cases. Then each test case is described by two lines:
- The first line of each test case contains a non-empty string, the legal move list of the superpiece. This string is a subset of
QRBNKP, and all characters appear in the same order. In other words, it is a subsequence ofQRBNKP. - The second line of each test case contains four integers , the starting and target positions of the superpiece. It is guaranteed that , i.e., the start and target positions are different.
Output Format
For each test case, output one integer per line, the minimum number of moves. If it is impossible to reach the target position, output .
2
NKP
3 3 5 1
NKP
2 6 5 3
2
2
2
B
2 8 3 6
B
2 8 5 5
-1
1
2
Q
3 3 4 5
QR
4 1 1 4
2
1
Hint
Explanation for Sample
In the first test case, we need to move from to . The legal moves are knight, king, and pawn. There are many ways that take two moves, for example:
- Pawn to , then knight to .
- Knight to , then king to .
- King to , then king to .
There is no solution with fewer than two moves—we would need a bishop or a queen to do that.
In the second test case, we need to move from to . Similarly, the optimal solution takes two moves. Here, every move must be a knight move, using or as an intermediate square.
Explanation for Sample
In the first test case, we need to move from to . The piece can only move like a bishop, which is impossible.
In the second test case, we need to move from to . The piece can only move like a bishop. It can be done in two moves, for example by using as an intermediate square.
Explanation for Sample
In the first test case, we need to move from to . The piece can only move like a queen. We can use as an intermediate square and reach it in two moves.
In the second test case, we need to move from to . The piece can only move like a queen and a rook. It can be reached in one move.
Constraints
For all testdata, , .
- Subtask 1 ( points):
Qis guaranteed to exist, andNis guaranteed not to exist. - Subtask 2 ( points):
QNis guaranteed to exist. - Subtask 3 ( points):
Ris guaranteed to exist, andQis guaranteed not to exist. - Subtask 4 ( points): only
Bexists. - Subtask 5 ( points):
Bis guaranteed to exist, andQRis guaranteed not to exist. - Subtask 6 ( points): only
Nexists. - Subtask 7 ( points):
Nis guaranteed to exist, andQRBis guaranteed not to exist. - Subtask 8 ( points):
Kis guaranteed to exist, andQRBNis guaranteed not to exist. - Subtask 9 ( points): only
Pexists.
Note that the subtasks are not ordered by difficulty.
Translated by ChatGPT 5
