PDP-22 (2010) - Phase C' - 3 (getout)

View as PDF

Submit solution

Points: 35 (partial)
Time limit: 1.0s
Memory limit: 128M

Author:
Problem types
Allowed languages
C, C++, Java, Pascal, Python

Get out!

A square parking space for cars has a side length of N meters. Cars of dimensions 2 \times 1 meters are parked in it. The cars are parked parallel to the sides of the square and at integer coordinates, some horizontally (along rows) and some vertically (along columns). Each car can only move along its axis and within the square, provided it is not obstructed by another car, and cannot rotate.

The natural gas supply company wants to dig along a horizontal line (row) at position R inside the parking space to pass an underground pipeline. For this purpose, the cars parked on this line must be moved.

Problem:

Develop a program in one of the IOI languages which, after reading the length of the square's side, the position where the pipeline will be placed, and the positions of the parked cars, calculates the minimum number of moves required to clear the pipeline's row. In each move, a car is moved along its axis (forward or backward), one or more positions.

Input Files:

The input files named getout.in are text files with the following structure: The first line contains two integers: the length N of the square's side (4 \le N \le 8) and the position R of the row where the pipeline will be placed (1 \le R \le N). The next N lines describe the cars parked horizontally along the rows of the square. Each line starts with the number X_i, the number of cars parked along this row, followed by X_i integers: the coordinates where the cars are parked along this row. (The coordinates are between 1 and N. A car parked horizontally with coordinate 5 is located on columns 5 and 6 of the square.) Similarly, the next N lines describe the cars parked vertically along the columns of the square.

Output Files:

The output files named getout.out are text files with only one line containing only one number: the minimum number of moves. If it is not possible to clear row R to place the pipeline, the result will be -1.

Example of Input - Output Files:

STDIN (getout.in)

5 4
2 2 4
0
1 2
0
0
1 3
0
1 4
1 3
0

STDOUT (getout.out)

4

pdp-2010-C3.svg

Explanation of the Example:

To free up row 4 of the square, it is sufficient to move car Δ two positions down, then car Z one position down, then car Γ one position left, and finally car Ε two positions down. Therefore, 4 moves are needed.

Formatting: In the output, each line terminates with a newline character.
Maximum execution time: 1 sec.
Maximum available memory: 128 MB.


Comments

There are no comments at the moment.