- Time limit: 2.00 s
- Memory limit: 512 MB
Impatience is a one-person game played on an infinitely large 2-dimensional grid. Each grid cell is either empty or it contains one pebble. We refer to grid cells with coordinates , where is the row (numbered from top to bottom) and is the column (numbered from left to right).
You are given two shapes, A and B, that show the positions of the pebbles. In the beginning, you place all pebbles according to shape A, with the top left corner in cell . Then you can perform a sequence of valid rotations in which you move one pebble to a new position. You win if you find a sequence of rotations so that your final configuration looks like shape B (possibly shifted horizontally and vertically, but not rotated or mirrored).
A valid rotation is defined as follows. Let and be two adjacent rows (i.e., ) and let and be two adjacent columns (i.e., ). Now assume that:
- contains a pebble
- is empty
- exactly one of and contains a pebble.
Then we can move a pebble from to ; such a move is denoted by . See the figure below for an example of inputs and a sequence of 3 valid rotations that wins the game.
Note that you are free to use both negative and positive coordinates. You can freely step outside the boundaries of the images A and B.
Input
As input, you will receive two shapes. Each shape is encoded as follows:
- One line with two numbers and : the height and the width of the shape.
- lines, with characters on each line. The characters are either
0
or1
, indicating an empty slot or a slot with a pebble, respectively.
The shapes do not have any holes (starting from any empty slot, you can reach another empty slot by a sequence of moves up/down/left/right through empty slots).
Output
If there is no way to win the game, output just -1
.
Otherwise, output a sequence of valid rotations that wins the game, encoded as follows. First, output one line which contains just one number , the number of rotations. Then, output lines of the form " ", which indicates a rotation .
You can use at most 200000 rotations in your output.
Limits
- The number of pebbles in each shape is between 1 and 100.
Example 1
The following example corresponds to the above illustration.
Input:
3 3 001 111 100 2 4 1111 0010
Output:
3 1 3 2 4 2 2 3 3 3 1 2 2
Example 2
Input:
2 5 00000 11000 3 3 011 000 000
Output:
0
Example 3
Input:
3 13 0101010101110 0111010100100 0101010100100 1 1 1
Output:
-1
Example 4
Input:
1 3 111 3 2 01 01 10
Output:
3 1 3 0 2 1 2 0 3 0 3 -1 2