Under construction

Still being built. Things may change

Padaengi's Great Escape from School!

Time limit1sMemory limit1024 MB

Summary
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 N×MN \times M grid. Padaengi knows the following.

  • Padaengi decides to escape at t=1t = 1, and must satisfy t≤Tt \le T when returning to the classroom.
  • Padaengi is in the classroom at cell (1,1)(1, 1), and the snack bar is at cell (N,M)(N, M).
  • In each cell, # is an obstacle and . is empty. Teachers and Padaengi cannot pass through obstacles or leave the map.
  • At every t≡5(mod10)t \equiv 5 \pmod{10}, Padaengi can stay still or move to an adjacent cell. If the time teatt_{eat} when Padaengi finishes eating satisfies teat≡5(mod10)t_{eat} \equiv 5 \pmod{10}, Padaengi can move right after finishing.
  • Teachers follow their given routes in order and move when t≡0(mod10)t \equiv 0 \pmod{10}.
  • 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 (a,b)(a, b) and (x,y)(x, y) are adjacent when max⁡(∣a−x∣,∣b−y∣)=1\max(|a - x|, |b - y|) = 1.

Input

The first line contains the grid height NN, width MM, the number of teachers LL, the remaining lunch time TT, and the time KK needed to eat tteokbokki and fried food, all as integers. (2≤N,M≤1,000;1≤L≤2,000;1≤T≤3,600;1≤K≤T)(2 \le N, M \le 1,000; 1 \le L \le 2,000; 1 \le T \le 3,600; 1 \le K \le T) The next NN lines give the map. The top left corner is (1,1)(1, 1) and the bottom right corner is (N,M)(N, M). Cells (1,1)(1, 1) and (N,M)(N, M) have no obstacles. The next data describes LL teachers. Each teacher's first line gives the route length pp. (1≤p≤100)(1 \le p \le 100) The next pp lines give the route xi,yix_i, y_i. (1≤xi≤N;1≤yi≤M)(1 \le x_i \le N; 1 \le y_i \le M)

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 t=nt = n is (x(⌊n10⌋ mod p+1),y(⌊n10⌋ mod p+1))(x_{(\lfloor \frac{n}{10} \rfloor \bmod p + 1)}, y_{(\lfloor \frac{n}{10} \rfloor \bmod p + 1)}).

Examples3

  1. Example 1

    Input
    4 4 1 55 10
    ....
    #..#
    .#..
    .#..
    1
    1 4
    
    Expected output
    YUMMY
    
  2. Example 2

    Input
    6 5 2 3600 400
    .###.
    #...#
    ..#..
    ..#..
    ##.##
    .....
    7
    1 5
    1 5
    2 4
    2 3
    2 2
    2 3
    2 4
    8
    2 3
    2 4
    3 4
    4 4
    5 3
    4 4
    3 5
    2 4
    
    Expected output
    YUMMY
    
  3. Example 3

    Input
    3 4 1 90 11
    ...#
    .##.
    #...
    11
    1 2
    1 3
    1 3
    1 3
    1 3
    1 3
    1 3
    1 3
    1 3
    1 3
    1 3
    
    Expected output
    SAD