Acceptable Seating Arrangements

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

요약
각 행이 왼쪽에서 오른쪽으로 증가하는 두 개의 허용 가능한 자리 배치가 주어질 때, 중간 과정도 항상 허용 가능하게 유지하면서 첫 배치를 두 번째 배치로 바꾸는 10^4개 이하의 교환을 출력한다.
난이도

어려움10점 중 8점

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

문제

Charlie is managing a classroom. The seats in the classroom are arranged in a grid with rows and columns. Each student has a distinct height.

A configuration of students to seats is acceptable if the following conditions are met:

  • Each student is assigned to exactly one seat.
  • The students are seated in increasing order of height from left to right in each row.

The students are initially seated in an acceptable arrangement. Charlie wants to rearrange students into a potentially different acceptable arrangement. To do this, he can swap any two students. However, he wants to ensure that the configuration stays acceptable after each swap.

Help Charlie devise a strategy to move the students from the original arrangement to his preferred arrangement. You don't need to minimize the number of swaps, but you are limited to at most 10410^4 swaps.

It can be proven that this is always possible for all possible inputs that satisfy the input constraints.

입력

The first line of input contains two integers rr and cc (1≤r,c≤201 \le r,c \le 20). Charlie's classroom has rr rows and cc columns of seats.

Each of the next rr lines contains cc integers hh (1≤h≤r⋅c1 \le h \le r \cdot c), representing the heights of the students in each row in the original arrangement. The heights are guaranteed to be distinct, and the arrangement is guaranteed to be acceptable.

Each of the next rr lines contains cc integers hh (1≤h≤r⋅c1 \le h \le r \cdot c), representing the heights of the students in each row in Charlie's desired arrangement. The heights are guaranteed to be distinct, and the arrangement is guaranteed to be acceptable.

출력

On the first line, output an integer kk, which is the number of swaps to perform (0≤k≤1040 \le k \le 10^4).

Then, output the kk swaps which change the original arrangement to Charlie's preferred arrangement. On each of the next kk subsequent lines, output four integers r_1,c_1,r_2,c_2r\_1, c\_1, r\_2, c\_2 (1≤r_1,r_2≤r1 \le r\_1, r\_2 \le r, 1≤c_1,c_2≤c1 \le c\_1, c\_2 \le c, (r_1,c_1)≠(r_2,c_2)(r\_1, c\_1) \ne (r\_2, c\_2)). This represents a swap of the student in row r_1r\_1, column c_1c\_1 with the student in row r_2r\_2, column c_2c\_2.

It can be proven that it's always possible to accomplish this in under 10410^4 swaps for all possible inputs that satisfy the input constraints. Remember that the arrangement must be acceptable after each swap.

예제1

  1. 예제 1

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