Deadly School Route
InterviewTime limit1sMemory limit1024 MB
Given a grid of red and gray blocks, the task checks whether a path of same-colored blocks with Manhattan jumps up to X connects the top-left and bottom-right corners.
Problem
Jihun moves along his school route by stepping only on paving blocks of the same color. He dies if he steps on a block of a different color.
The route can be represented as an matrix, and each element of the matrix is one paving block. Jihun starts at row 1, column 1 and must arrive at row , column .
While moving along a route made of red or gray blocks, Jihun must step only on blocks with the same color as the block he is currently standing on. His jump power is , so he can move only between blocks whose Manhattan distance is at most . Determine whether Jihun can reach school alive.
Input
The first line contains the number of rows . The second line contains the number of columns . The next lines each contain integers separated by spaces, where the -th of these lines gives the colors of the blocks in row from left to right. means a red block and means a gray block. The last line contains Jihun's jump power .
Output
Print ALIVE if Jihun can complete the trip alive, otherwise print DEAD.
Hint
The Manhattan distance between the block at row , column and the block at row , column is defined as .