Mirko found a collection of
Each tank can be moved to one of the four neighbouring squares in a single move. A tank can shoot at any square in the same row and column. The tank is said to be guarding the row and column it is in.
Additionally, no two tanks can be in the same square at any time.
After many hours of play and two previous attempts, Mirko's mom yelled at them to come down for lunch again, and they decided to rearrange the tanks so that each tank guards a different row and column (meaning also that each row and column contains only one tank).
However, they want to do this using the minimum number of moves.
Write a program that finds the minimum number of moves required to rearrange the tanks so that each row and each column contains a single tank, and one such shortest sequence of moves.
Input Specification
The first line of input contains the integer
Each of the following
Rows and columns are marked
Output Specification
Output the minimum number of moves (call this number
Each of the next
Tanks are numbered
The direction can be one of four uppercase letters: L
for left, R
for right, U
for up and D
for down.
Note: The sequence need not be unique.
Scoring
If both the number
If your program outputs the correct number
Sample Input 1
5
1 1
1 2
1 3
1 4
1 5
Sample Output 1
10
1 D
2 D
3 D
4 D
1 D
2 D
3 D
1 D
2 D
1 D
Sample Input 2
5
2 3
3 2
3 3
3 4
4 3
Sample Output 2
8
1 R
1 R
2 U
2 U
4 D
4 D
5 L
5 L
Sample Input 3
6
1 1
1 2
2 1
5 6
6 5
6 6
Sample Output 3
8
2 R
2 D
3 D
3 R
4 U
4 L
5 L
5 U
Comments