#P3930. SAC E#1 - 一道大水题 Knight
SAC E#1 - 一道大水题 Knight
Description
They often play a game together. No, not StarCraft, but chess.
Du nai se thinks F91 is a chicken. He set up a formation on an chessboard using black rooks, knights, bishops, a queen, a king, and pawns.
However, F91 thinks Du nai se is a chicken. He issues a challenge: he will control a white knight, move without stepping on any square that is within the attack range of any piece (F91 can move continuously, and Du nai se’s pieces do not move, unless the white knight enters a square in their attack range), and capture Du nai se’s king (i.e., land on the square where the black king is).
Please tell F91 the minimum number of moves needed to accomplish this.
Note:
- When F91’s white knight moves onto a square occupied by one of Du nai se’s pieces, it captures that piece. That piece no longer threatens the white knight.
- If the white knight starts in the attack range of a black piece, it is captured immediately and F91 loses at once.
- Even if the white knight enters the attack range of other pieces at the exact moment it captures the king (i.e., other pieces “guard” the king’s square), F91 still wins.
Attack ranges:
Rook: all squares in the horizontal and vertical directions, until blocked by another piece.
..#..
..#..
##C##
..#..
..#..
Knight: all positions with a horizontal move of 2 and a vertical move of 1, or a horizontal move of 1 and a vertical move of 2 (at most 8 squares, L-shaped).
.#.#.
#...#
..K..
#...#
.#.#.
Bishop: all squares along diagonals (), until blocked by another piece.
#...#
.#.#.
..B..
.#.#.
#...#
Queen: the combination of rook and bishop (it can attack horizontally/vertically and along diagonals, until blocked by other pieces).
#.#.#
.###.
##Q##
.###.
#.#.#
King: the 8 neighboring squares in the 8-connected neighborhood.
.....
.###.
.#X#.
.###.
.....
Pawn: the squares down-left / down-right () (at most 2 squares).
.....
.....
..P..
.#.#.
.....
The letters indicate piece types; see the input format. # indicates squares in the attack range.
Input Format
The input contains multiple sets of testdata.
For each set, the first line contains an integer indicating the board size.
The next lines each contain a string of length describing the board.
Where:
.indicates an empty square.Oindicates the white knight.Cindicates a black rook.Kindicates a black knight.Bindicates a black bishop.Qindicates a black queen.Xindicates a black king.Pindicates a black pawn.
Output Format
For each set of testdata, output one integer per line, indicating the minimum number of moves F91 needs.
If it is impossible for F91 to capture the black king no matter how he moves, output -1.
8
...X....
........
........
........
........
........
........
......O.
4
8
......X.
........
.O......
...P.Q.C
.....B..
........
...K....
........
7
Hint
The input contains at most 5 sets of testdata.
- For 20% of the testdata, Du nai se has only the king. .
- For 30% of the testdata, Du nai se has only the king and knights. .
- For 60% of the testdata, Du nai se has only the king, knights, and the queen. .
- For 100% of the testdata, Du nai se may have the full set of 16 pieces (2 rooks, 2 knights, 2 bishops, 1 queen, 1 king, 8 pawns). .
Kind reminder: the time limit might be tighter than you think. Please pay attention to implementation details to ensure performance.
Explanation for sample 2:
One feasible sequence is:
......X.
.3..6...
.O5.....
4.2P.Q.C
1....B..
........
...K....
........
Translated by ChatGPT 5
京公网安备 11011102002149号