PivotOJ

Color Tubes

시간 제한: 1000ms메모리 제한: 1024MB출처: ICPC Rocky Mountain Regional 2022-2023BOJ 27571

문제

There is a new puzzle generating buzz on social media---Color Tubes. The rules are relatively simple: you are given n+1n+1 tubes filled with 3n3n colored balls. Each tube can hold at most 3 balls, and each color appears on exactly 3 balls (so there are nn colors).

Using a series of moves, you are supposed to reach a Color Tubes state---each tube should either hold 3 balls of a single color or it should be empty.

The only move allowed is to take the top ball from one tube and place it into a different tube that has room for it (i.e. holds at most two balls before the move).

You want to write a program to solve this puzzle for you. Initially, you are not interested in an optimal solution, but you want your program to be good enough to solve any puzzle configuration using at most 20n20n moves.

입력

The first line of input contains a single integer nn (1n10001 \leq n \leq 1\,000), which is the number of colors.

Each of the next n+1n+1 lines contains three integers bb, mm and tt (0b,m,tn0 \le b,m,t \le n), which are the descriptions of each tube, where bb is the color of the ball on the bottom, mm is the color of the ball in the middle, and tt is the color of the ball on the top.

The tubes are numbered from 11 to n+1n+1 and are listed in order. The colors are numbered from 11 to nn. The number 00 describes an empty space. It is guaranteed that no empty space will be below a colored ball.

출력

On the first line output an integer mm, the number of moves that your program will use to solve the puzzle. Remember, mm has to be at most 20n20n.

On the next mm lines, output two space-separated integers uu and vv that describe a move (1u,vn+11 \le u,v \le n+1). In each move, you are taking the uppermost ball out of tube uu and placing it in tube vv, where it will fall until it hits the uppermost ball already in that tube, or the bottom of the tube if the tube is empty.

Your solution will be deemed incorrect if it uses more than 20n20n moves, or any of the moves are not allowed, or the final configuration is not a Color Tubes state.

예제

예제 1

입력
3
2 2 0
1 3 1
3 1 2
3 0 0
출력
6
3 1
2 3
2 4
3 2
3 2
3 4

예제 2

입력
1
0 0 0
1 1 1
출력
0
코드를 제출하려면 로그인하세요.