Classical Hard Chess Problem

Time limit: 1s Memory limit: 256MB Input: Output:

Mr. COACH\mathbb{Mr. \ COACH} really likes chess. In fact, he likes it so much that before going to bed he reads chess problems, and solves them while sleeping. Tonight he can't sleep, so he asks you to help him solve the problem he read.

You are given a N×NN \times N chess board with multiple pieces of a single type on it. You can do the following operation several times: take a piece and move it to a square that is occupied by another piece, and remove it. It is important to note that in this problem, pieces don't have to stop when meeting the first piece in their way. In the end you want to have only one piece on the board.

Can you help Mr. COACH\mathbb{Mr. \ COACH} find a sequence of moves that solves the problem, or say that it is impossible?

How each piece moves:

  • The king can make exactly 1 step horizontally, vertically or diagonally
  • The queen can make any number of steps horizontally, vertically or diagonally
  • The rook can make any number of steps horizontally or vertically
  • The bishop can make any number of steps diagonally
  • The knight can move in an L-shape: 2 steps horizontally and 1 vertically, or 1 step horizontally and 2 vertically.

Input data

The first line contains an integer NN (1N1001 \le N \le 100) and a character cc that determines the type of piece on the board (K - king, Q - queen, R - rook, B - bishop, N - knight).
The next NN lines containt NN characters each, describing the board. The characters can be either ., meaning an empty square, or cc, meaning a piece.

Output data

The first line print YES if the problem can be solved, and NO otherwise. If the answer was YES, print the sequence of moves that solves the problem, one move per line. A move is described by 44 integers R1,C1,R2,C2R1, C1, R2, C2, meaning that you move a piece from row R1R1 column C1C1 to row R2R2 column C2C2.

Example 1

stdin

2 K
K.
KK

stdout

YES
2 2 2 1
2 1 1 1

Example 2

stdin

3 B
B..
B..
..B

stdout

NO

Example 3

stdin

3 Q
.Q.
QQQ
...

stdout

YES
2 3 2 1
2 1 2 2
1 2 2 2

Log in or sign up to be able to send submissions!