SOLIT - Solitaire

Solitaire is a game played on an 8x8 chessboard. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively.

There are four identical pieces on the board. In one move it is allowed to:

  • move a piece to an empty neighboring field (up, down, left or right),
  • jump over one neighboring piece to an empty field (up, down, left or right).
possible moves in solitaire

There are 4 moves allowed for each piece in the configuration shown above. As an example let's consider a piece placed in the row 4, column 4. It can be moved one row up, two rows down, one column left or two columns right.

Task

Write a program that:

  • reads two chessboard configurations from the standard input,
  • verifies whether the second one is reachable from the first one in at most 8 moves,
  • writes the result to the standard output.

Input

The input begins with the integer t, the number of test cases. Then t test cases follow.

For each test case, each of two input lines contains 8 integers a1, a2, ..., a8 separated by single spaces and describes one configuration of pieces on the chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the position of one piece - the row number and the column number respectively.

Output

For each test case the output should contain one word for each test case - 'YES' if a configuration described in the second input line is reachable from the configuration described in the first input line in at most 8 moves, or one word 'NO' otherwise.

Example

Sample input:
1
4 4 4 5 5 4 6 5
2 4 3 3 3 6 4 6

Sample output:
YES

Added by:adrian
Date:2004-07-07
Time limit:2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:ACM Central European Programming Contest, Warsaw 2002

hide comments
2021-04-19 12:02:19
Ignore Sudhakar's comment, it's the configurations that need to be reachable, not the individual pieces. Eg.:
1 1 1 2 1 3 7 8
2 1 2 2 2 3 8 8 -> YES

There are less than 30 cases per file.
2013-01-14 23:42:23 chy
The picture is broken
2011-06-08 17:35:31 biQar
what is the meaning of "Time limit - 7s" ??? i think my algo is fast enough to cover 7 sec. But i get tle. One more question ... how many test cases are for this problem ??? problem setter, please specify number of testcases. This problem makes my life crazy !!! :(

Last edit: 2011-06-08 18:08:34
2010-03-19 19:35:16 Sudhakar
Clarification needed on statement.
"verifies whether the second one is reachable from the first one in at most 8 moves"

This means each piece in first configuration should be reachable from any piece of second configuration.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.