Padaengi's Great Escape from School!
Time limit1sMemory limit1024 MB
Find whether Padaengi can reach (N, M), eat for K time units, and return to (1, 1) by time T without ever standing in a teacher's 8-cell watch range.
- Level
Medium6 of 10
- Topics
- BFS, Simulation, Matrix
- Solved
- No attempts yet
Problem
Padaengi is in high school and craves tteokbokki and fried food. So during lunch break, Padaengi plans to sneak out of school and go to the snack bar.

Padaengi looks around and sees teachers watching the area, so leaving is not easy. Check whether Padaengi can eat tteokbokki and return to the classroom within the lunch break without being spotted by a teacher.
The school is an grid. Padaengi knows the following.
- Padaengi decides to escape at , and must satisfy when returning to the classroom.
- Padaengi is in the classroom at cell , and the snack bar is at cell .
- In each cell,
#is an obstacle and.is empty. Teachers and Padaengi cannot pass through obstacles or leave the map. - At every , Padaengi can stay still or move to an adjacent cell. If the time when Padaengi finishes eating satisfies , Padaengi can move right after finishing.
- Teachers follow their given routes in order and move when .
- A teacher watches the 8 adjacent cells. Several teachers can stand on the same cell. Padaengi must not be watched by a teacher at any time. Padaengi is safe while moving between adjacent cells.
- Padaengi is not watched while in the classroom or at the snack bar.
Two cells and are adjacent when .

Input
The first line contains the grid height , width , the number of teachers , the remaining lunch time , and the time needed to eat tteokbokki and fried food, all as integers. The next lines give the map. The top left corner is and the bottom right corner is . Cells and have no obstacles. The next data describes teachers. Each teacher's first line gives the route length . The next lines give the route .
Output
Print YUMMY if Padaengi can eat tteokbokki and fried food and return to the classroom. Otherwise, print SAD.
Hint
The position of the teacher at is .