Destroyer King Ttungppup
Time limit1.5sMemory limit1024 MB
Given a grid of pillars that cost energy to pass and at most T teleports, answer for each query whether a cell is reachable from (1,1) within energy p.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Dynamic programming
- Solved
- No attempts yet
Problem
The world where Ttungppup lives is an grid of cells. The top left cell is and the bottom right cell is . Each cell holds at most one pillar, and each pillar has a strength.
To move from one cell to another in this world, you must follow these rules.
- From the current cell, you can move to an adjacent cell up, down, left, or right. If the target cell has a pillar, you spend energy equal to its strength to destroy it and move there. You cannot move if the pillar's strength is greater than your remaining energy.
- If the current cell allows teleportation, you can teleport in one direction. Teleporting costs no energy. If the destination cell has a pillar, the pillar is destroyed automatically. You can teleport at most times, and each cell has at most one teleport destination.
Given an energy and a destination , Ttungppup wants to know whether he can move from to using at most energy while following these rules. He asks questions. Pillars destroyed in one question do not affect the others.
Input
The first line contains , , , , and , separated by spaces.
Each of the next lines contains values separated by spaces. Each value is the strength of the pillar in that cell, between and . A value of means the cell has no pillar. is always .
Each of the next lines contains four integers separated by spaces. This means you can teleport from to .
Each of the next lines contains an energy and a destination separated by spaces.
All input values are integers.
Output
For each question, print on a separate line if you can reach from using at most energy. Otherwise, print .