Froggie
Time limit2sMemory limit512 MB
Simulate Froggie hopping across lanes of evenly spaced moving cars on a grid and report whether she exits the top lane safely or gets squished.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
You are recreating the classic 'Frogger' video game. You do not need to worry about sprites, music, or animation, since the game's art team handles those.
Each level has a road with several lanes, and cars travel in both directions. Cars within a lane are evenly spaced and move at the same speed in the same direction. Lane directions alternate, and the top lane moves from left to right.
Froggie starts below the bottom lane and crosses the road from bottom to top. At each time step, Froggie hops one cell up, down, left, or right. The goal is to get Froggie across the road and above the top lane without a car hitting her.
Avoiding Cars
If Froggie enters the path of a moving car, she is squished. Figure D.6 shows one car over two consecutive times. Suppose that at time the car is in the leftmost cell and Froggie is not in this lane. At time , the car moves at speed 3 to the rightmost cell, and Froggie tries to hop into the lane. The lane is four cells wide, so the only safe cell is the leftmost one, where the car had just been. The other three cells would get her squished by the car, ending the simulation.

Figure D.6: A single car across two consecutive time steps, starting at the left and moving right.
Goal
Given a description of the road, the car positions and speeds, and Froggie's starting position and moves, determine the outcome of the simulation. There are two possible outcomes: Froggie safely exits the top lane, or she gets squished.
Before the road, Froggie may move left or right to plan her route. Once she has entered the road, she may only exit through the top. Her path never leaves the left, right, or bottom boundaries of the lanes.
Input
Each input describes one simulation. The first line contains two integers and , separated by a space. is the number of lanes (), and is the width of the grid in cells ().
Next come lines, one per lane from top to bottom. Each line has three integers: , the starting offset of the first car, , the interval between cars, and , the speed of the cars. The bounds are and . All offsets are measured in the direction of the lane's movement.
The final line starts with an integer (), then a space, then a string of characters () from the set {U, D, L, R}. is Froggie's starting position counted from the left, and the string is the sequence of moves (up, down, left, right) she makes.
Output
If Froggie crosses the road and exits from the top lane, print "safe". If she is hit by a car, or she does not end above the road, print "squish".