Combination Lock

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

요약
3-다이얼과 5-다이얼이 체커판처럼 놓인 격자에서 목표 값을 만족하도록, 한 번의 이동이 칸과 상하좌우 이웃을 증가시킬 때 20nm 이하의 이동 순서를 찾는다.
난이도

어려움10점 중 8점

유형
수학, 구현, 행렬, 그리디
정답자
아직 제출이 없습니다

문제

You are faced with a combination lock that consists of an nn by mm grid of dials. A kk-dial is a dial that can display values 0,1,⋯k−10, 1, \cdots k-1. A kk-dial currently displaying value vv can be incremented to make it display value (v+1)mod  k(v + 1)\mod k.

This particular lock consists of 33-dials and 55-dials in a checkerboard arrangement, with the top left dial being a 33-dial. In a single move, you can increment a dial and all of its horizontal and vertical neighbors.

For example, here is one possible sequence of moves on a grid with n=3n=3, m=4m=4 (where 33-dials are gray and 55-dials are white):

Initially, all of the dials are displaying 00. You remember what positions the dials must be in for the lock to open, but you forgot the combination of moves to reach it. Find a sequence of moves that sets all dials to the correct positions. You do not need to minimize the number of moves, but you can use no more than 20nm20nm moves.

It can be shown that such a solution always exists.

입력

The first line of the input contains a single integer tt (1≤t≤10001 \le t \le 1000) --- 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≤1001 \le n, m \le 100) --- the number of rows and columns in the grid, respectively.

The ii-th of the next nn lines contains mm integers describing the ii-th row of the combination. The jj-th of these is a_ija\_{ij}, the desired position of the dial at position (i,j)(i, j). If i+ji+j is even, 0≤a_ij<30 \le a\_{ij} < 3. Otherwise, 0≤a_ij<50 \le a\_{ij} < 5.

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

출력

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

The next kk lines of output should each contain integers ii and jj, indicating that the next move will be performed at position (i,j)(i, j).

If there are multiple solutions, print any.

힌트

Here is the move used in the first sample case:

In the second sample case, the lock is already in the target position, so no moves are needed.

예제1

  1. 예제 1

    입력
    5
    3 3
    0 1 0
    1 1 1
    0 1 0
    2 3
    0 0 0
    0 0 0
    3 5
    0 1 2 3 0
    0 1 4 0 2
    0 0 1 2 0
    1 1
    1
    2 2
    2 4
    4 2
    
    예상 출력
    1
    2 2
    0
    4
    1 3
    2 4
    2 4
    2 3
    1
    1 1
    4
    1 1
    1 1
    2 2
    2 2