According to legend, in the kingdom of “Balabalabala” (kingdom name), there once was a town named “Balabalabala” (town name) that was massacred. Later, a group of “Balabalabala” (possibly scientists or wizards) from the “Balabalabala” kingdom discovered that the massacre of the “Balabalabala” town was due to divine punishment. Interestingly, the scope of the judgment expanded in a cross-shaped pattern.
In a grid coordinate system (assuming the center square is at (0, 0), with right being (0, 1), up being (-1, 0), left being (0, -1), and down being (1, 0)), if the range is 1 and the center coordinate is (0, 0), then the scope of the divine judgment would cover the following grid, with the affected areas represented by ●:
○○○○○
○○●○○
○●●●○
○○●○○
○○○○○
Range 2
○○●○○
○●●●○
●●●●●
○●●●○
○○●○○
Range 3
○○○●○○○
○○●●●○○
○●●●●●○
●●●●●●●
○●●●●●○
○○●●●○○
○○○●○○○
Now, given the coordinates of the judgment’s center, as well as the location of the “Balabalabala” kingdom’s capital (city name: “Balabalabala”), and the range of the judgment, please determine whether the capital will be destroyed by the divine judgment.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
Each test case consists of multiple test data points.
Each set of test data is provided in one line. The first and second numbers represent the coordinates of the divine judgment’s center. The third and fourth numbers represent the coordinates of the kingdom’s capital (all coordinate values are guaranteed to be between -2147483648 and 2147483647). The fifth number is the effective range r of the divine judgment (1 ≤ r ≤ 2147483647). | If the capital's coordinates fall within the range of divine judgment and are thus doomed to destruction, please output "die". If the capital's coordinates are not within the range of divine judgment and thus escape destruction, please output "alive". |
0 0 1 1 1 0 0 1 1 2 0 0 -1 2 3 -1 0 1 2 3 0 0 50 50 100 0 0 50 50 99 | alive die die alive die alive |
Thought Process
To calculate whether the city will be affected by divine judgment, compute the Manhattan distance between the city and the center of judgment. If the Manhattan distance is greater than the given range, the city will not be affected.
Manhattan Distance: |x1 – x2| + |y1 – y2|
It’s important to declare the variables and the variables used to calculate the Manhattan distance as long long int.
Sample Code-ZeroJudge D575: End of the World
#include <iostream>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
long long int centerX, centerY, cityX, cityY, distance;
while (cin >> centerX >> centerY >> cityX >> cityY >> distance) {
const long long int manhattan = abs(centerX - cityX) + abs(centerY - cityY);
if (manhattan > distance) {
cout << "alive\n";
}
else cout << "die\n";
}
}
//ZeroJudge D575
//Dr. SeanXD