## CCO '15 P4 - Cars on Ice

View as PDF

Points: 10 (partial)
Time limit: 7.0s
Memory limit: 1G

Problem type

Guarding a bank during Christmas night can get very boring. That's why Barry decided to go skating around the parking lot for a bit. However the parking lot isn't empty as the other security guards have their cars parked there. So Barry decides to push their cars out of the parking lot. He notices that their cars are parked facing either North, South, East or West. Since the parking lot is frozen, pushing a car will make it slide until it has left the parking lot or hit another car. Cars can only be pushed in the direction which they are facing. Not wanting to crash the cars, Barry enlists your help to find out what order he has to push the cars so as to clear the parking lot.

#### Input Specification

The first line contains two integers and representing the number of rows and columns of the parking lot. The next lines each contain characters representing the parking lot itself, where . represents an empty spot, while N, S, E and W each represent a car (facing North, South, East or West, respectively).

For at least 70% of the marks for this problem, and .

#### Output Specification

Output the order in which the cars have to be pushed so as to clear the parking lot without crashes. Output each car in the form , where and are the car's coordinates on the parking lot (where is the top leftmost spot and is the bottom rightmost spot).

You can assume there will always be at least one valid solution.

If there are multiple possible solutions, output any valid solution.

#### Sample Input 1

5 5
.....
.E.S.
.....
.....
.N.E.

#### Output for Sample Input 1

(4,3)
(1,3)
(1,1)
(4,1)

#### Explanation for Output for Sample Input 1

The only car that isn't initially blocked by another car is the one at . After that's pushed out to the right side of the parking lot, then the car above it (at ) can be pushed, and then the one at , and finally the car at , clearing the parking lot.

#### Sample Input 2

4 3
...
.N.
.S.
...

#### Output for Sample Input 2

(1,1)
(2,1)

#### Explanation for Output for Sample Input 2

Either car could be pushed first to clear the parking lot, so this output is acceptable (as would the output with the lines outputted in reverse order).

• commented on Jan. 20, 2020, 10:54 p.m. edit 3

Passed TC 10 with iteration, not with recursion

• commented on Feb. 12, 2019, 7:13 p.m.

This comment is hidden due to too much negative feedback. Show it anyway.

• commented on Feb. 12, 2019, 11:32 p.m. edit 2

Yes it is possible! In pypy the worst test took ~2 seconds.

• commented on Jan. 28, 2018, 3:34 p.m.

what if the car is already in the place we want to put it in

• commented on Jan. 28, 2018, 4:03 p.m.

That can't be the case, you're trying to clear the board of cars.