Domino Swap

시간 제한4초메모리 제한1024 MB

요약
같은 색인 인접한 두 칸의 색을 맞바꾸는 연산만으로 시작 격자를 목표 격자로 바꾸거나, 불가능하다고 판정한다.
난이도

어려움10점 중 8점

유형
그래프, 시뮬레이션, 그리디, 구현
정답자
아직 제출이 없습니다

문제

There is an nn by mm grid of black and white squares. In one operation, you can pick any two adjacent squares of the same color and swap the colors of both. For example, here is a valid sequence of two operations on a grid with n=4n=4, m=3m=3:

You are also given a target grid of nn by mm black and white squares. Perform at most 200nm200nm operations on the starting grid to reach the target grid, or state that it is impossible to do so. You do not need to minimize the number of operations.

It can be shown that if there is a solution, there is one that uses at most 200nm200nm operations.

입력

The first line of the input contains a single integer tt (1≤t≤5001 \le t \le 500) --- the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n,m≤501 \le n, m \le 50) --- the number of rows and columns in the grid, respectively.

The ii-th of the next nn lines contains mm characters describing the ii-th row of the starting grid. The jj-th of these is '\#' if the square at position (i,j)(i, j) is black, and '.' if the square at position (i,j)(i, j) is white.

The next nn lines describe the target grid in the same format as the starting grid.

It is guaranteed that the sum of n⋅mn\cdot m over all test cases does not exceed 25002500.

출력

For each test case, if there is no solution, output a single integer −1-1.

Otherwise, the first line of output for each test case should contain a single integer kk (0≤k≤200nm0 \le k \le 200nm) --- the number of operations you will perform.

The next kk lines of output should each contain four integers i_1i\_1, j_1j\_1, i_2i\_2, and j_2j\_2 (1≤i_1,i_2≤n1 \le i\_1, i\_2 \le n, 1≤j_1,j_2≤m1 \le j\_1, j\_2 \le m) --- the two adjacent squares (i_1,j_1)(i\_1, j\_1) and (i_2,j_2)(i\_2, j\_2) that are part of this operation.

If there are multiple solutions, print any.

힌트

Here is the sequence of 33 operations described by the output of the first test case:

It is impossible to perform any operations in the second test case, so it is not possible to reach the target grid.

예제1

  1. 예제 1

    입력
    6
    3 3
    ..#
    .#.
    .#.
    #.#
    ..#
    .##
    4 6
    #.#.#.
    .#.#.#
    #.#.#.
    .#.#.#
    ......
    ......
    ......
    ......
    1 1
    #
    #
    4 5
    #...#
    ...#.
    ###.#
    ....#
    ..#.#
    .#...
    ###.#
    .##.#
    1 8
    ...#....
    ........
    4 2
    ..
    ..
    ..
    ..
    ##
    ##
    ##
    ##
    
    예상 출력
    3
    1 1 1 2
    1 2 2 2
    2 3 3 3
    -1
    0
    5
    1 2 1 3
    2 2 2 3
    1 1 1 2
    2 3 2 4
    4 2 4 3
    -1
    4
    1 1 1 2
    4 1 4 2
    2 1 3 1
    2 2 3 2