아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Color Tubes

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

요약
3n개의 색깔 공이 담긴 n+1개의 튜브가 주어질 때, 각 튜브가 한 색의 공 3개 또는 비어 있도록 20n번 이내의 이동 순서를 만든다.
난이도

보통10점 중 7점

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

문제

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 (1≤n≤1,0001 \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 (0≤b,m,t≤n0 \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 (1≤u,v≤n+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.

예제2

  1. 예제 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. 예제 2

    입력
    1
    0 0 0
    1 1 1
    
    예상 출력
    0