NOIP '20 P3 - Game of Moving Ball

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem type

C is playing a game. There are n+1 poles in front of him, numbered from 1 to n+1. At the beginning, the poles 1 to n have m balls, placed from the bottom to the top. The n+1th pole has no balls on it. The n \times m balls have n colors, with each color having m balls.

C is trying to move the balls so that in the end, every pole only contains the balls of the same color, notice that it does not matter which pole has which color.

C can reach the goal using a number of operations, moving one ball from one pole to the other. More specifically, C can move the ball from pole x to pole y if and only if:

  1. There is at least one ball on pole x.
  2. There are at most m-1 balls on pole y.
  3. C can only move the top ball of pole x to the top of pole y.

C's goal is not hard to reach, so he challenges you to reach his goal in at most 820\,000 operations. Print out one way to reach his goal.

Input Specification

The first line contains two integers, n, m denoting the number of colors of the balls and the number of balls of each color. The next n lines contain m integers each, the ith line denoting the balls on the ith pole.

Output Specification

The first line of output contains one integer k that satisfies 1 \le k \le 820\,000, denoting the number of operations. The next k lines contain two integers x, y, denoting moving a ball from pole x to pole y. x, y should satisfy 1 \le x, y \le n+1 and x \ne y. Any valid solution will be accepted.

Sample Input

2 3
1 1 2
2 1 2

Sample Output

6
1 3
2 3
2 3
3 1
3 2
3 2

Constraints

For all test cases, 2 \le n \le 50, 2 \le m \le 400. For partials, refer to the table below.

Test Cases n= m=
1~2 2 20
3~5 10
6~8 50 85
9~14 300
15~20 400

Comments

There are no comments at the moment.