Bombs (Codeforces 350C)
Tags: Implement, sortings

You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations:  Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k ≥ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb.  Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container.  Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane.

Input: The first line contains a single integer n (1 ≤ n ≤ 105) — the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). 

Output: In a single line print a single integer k — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≤ 106.


Sample Inputs:
2
1 1
-1 -1

3
5 0
0 5
1 0

Sample Outputs:
12
1 1 R
1 1 U
2
1 1 L
1 1 D
3
1 1 L
1 1 D
2
1 1 R
1 1 U
3

12
1 1 R
2
1 1 L
3
1 5 R
2
1 5 L
3
1 5 U
2
1 5 D
3

Solution (Python):
def f(i, j):
    (x, y) = (str(abs(i)), str(abs(j)))
    (l, r, u, d) = (' L', ' R', ' U', ' D')
    if i < 0:
        (l, r) = (r, l)
    if j < 0:
        (u, d) = (d, u)
    if i:
        if j:
            return ['1 ' + x + r, '1 ' + y + u, '2', '1 ' + x + l, '1 ' + y + d, '3']
        else:
            return ['1 ' + x + r, '2', '1 ' + x + l, '3']
    else:
        return ['1 ' + y + u, '2', '1 ' + y + d, '3']
(p, n) = ([], int(input()))
t = [(abs(i) + abs(j), i, j) for (i, j) in tuple((map(int, input().split()) for i in range(n)))]
t.sort()
for (r, i, j) in t:
    p += f(i, j)
print(len(p))
print('\n'.join(p))

Solution (Python):
import sys
input = sys.stdin.readline

def inp():
    return int(input())

def inlt():
    return list(map(int, input().split()))

def insr():
    s = input()
    return list(s[:len(s) - 1])

def invr():
    return map(int, input().split())

def Bombs2():

    def command_path(x, y):
        if x > 0:
            x_command_f = ' R'
            x_command_r = ' L'
        elif x < 0:
            x_command_f = ' L'
            x_command_r = ' R'
        if y > 0:
            y_command_f = ' U'
            y_command_r = ' D'
        elif y < 0:
            y_command_f = ' D'
            y_command_r = ' U'
        if x != 0 and y != 0:
            path = '1 ' + str(abs(x)) + x_command_f + '\n' + '1 ' + str(abs(y)) + y_command_f + '\n' + '2' + '\n' + '1 ' + str(abs(x)) + x_command_r + '\n' + '1 ' + str(abs(y)) + y_command_r + '\n' + '3'
            instructions = 6
        elif x != 0:
            path = '1 ' + str(abs(x)) + x_command_f + '\n' + '2' + '\n' + '1 ' + str(abs(x)) + x_command_r + '\n' + '3'
            instructions = 4
        else:
            path = '1 ' + str(abs(y)) + y_command_f + '\n' + '2' + '\n' + '1 ' + str(abs(y)) + y_command_r + '\n' + '3'
            instructions = 4
        return (path, instructions)
    n = inp()
    dist_from_origin = []
    all_paths = []
    totalInstructions = 0
    for _ in range(n):
        (x, y) = invr()
        (path, instructions) = command_path(x, y)
        totalInstructions += instructions
        all_paths.append(path)
        dist_from_origin.append(abs(x) + abs(y))
    sorted_data = sorted(zip(dist_from_origin, all_paths))
    (dist_from_origin_sorted, all_paths_acc) = zip(*sorted_data)
    outputStr = '\n'.join((x for x in all_paths_acc))
    outputStr = str(totalInstructions) + '\n' + outputStr
    outputStr = outputStr.strip()
    print(outputStr)
    return
Bombs2()

Solution (Python):
from bisect import bisect_left as bl, bisect_right as br, insort
import sys
import heapq
from collections import defaultdict as dd, deque

def data():
    return sys.stdin.readline().strip()

def mdata():
    return map(int, data().split())
n = int(data())
A = []
ans = []
d = dd(list)
for i in range(n):
    (x, y) = mdata()
    A.append((x, y))
A.sort(key=lambda x: abs(x[0]) + abs(x[1]))
for i in A:
    (x, y) = i
    if x > 0:
        ans.append('1 %d R' % x)
    elif x < 0:
        ans.append('1 %d L' % -x)
    if y > 0:
        ans.append('1 %d U' % y)
    elif y < 0:
        ans.append('1 %d D' % -y)
    ans.append('2')
    if x > 0:
        ans.append('1 %d L' % x)
    elif x < 0:
        ans.append('1 %d R' % -x)
    if y > 0:
        ans.append('1 %d D' % y)
    elif y < 0:
        ans.append('1 %d U' % -y)
    ans.append('3')
print(len(ans))
print('\n'.join(ans))