Saving James Bond - Easy Version
This time let us consider the situation in the movie “Live and Let Die” in which James Bond, the world’s most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape – he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head… Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).
Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him whether or not he can escape.
Input Specification:
Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the (x,y) location of a crocodile. Note that no two crocodiles are staying at the same position.
Output Specification:
For each test case, print in a line “Yes” if James can escape, or “No” if not.
Sample Input 1:
14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12
Sample Output 1:
Yes
Sample Input 2:
4 13
-12 12
12 12
-12 -12
12 -12
Sample Output 2:
No
solution: 题目的意思就是James在一个直径为15的孤岛上,如果把鳄鱼(crocodile)当作点的话,那么判断是否James通过跳跃这些点能够到达岸边。其中鳄鱼的位置是以坐标的形式给出。
如果把坐标当成图的顶点,坐标之间的距离当成权重的话,那么题目就变成给定一个初始顶点(James的位置)是否有一条路径可以到达岸边,其中该条路径满足所有的权重值都要小于等于James能够跳的距离,并且该路径是连通的。因此,很显然我们应该使用深度优先搜索来解决该问题。
对于该问题我们需要注意两个地方:
1. 第一跳及从岛跳到鳄鱼上去,其中岛的半径为7.5,因此第一跳能够跳到鳄鱼的最大长度为dis(James能跳的距离) 加上7.5。
2. 从鳄鱼跳到岸边
code
#include<iostream>
#include<vector>
using namespace std;
//定义一个结构体用于存储鳄鱼的坐标
struct position {
int x;
int y;
position() = default ;
position(int xx, int yy) :
x(xx), y(yy) {}
};
//计算两点之间的距离
int getDistance(position pos1, position pos2)
{
int dis = (pos1.x - pos2.x)*(pos1.x - pos2.x) +
(pos1.y - pos2.y)*(pos1.y - pos2.y);
return (int)(sqrt(dis));
}
//判断是否能够跳到岸边
bool isSafe(position pos1, int dis)
{
int x, y;
x = pos1.x >= 0 ? 50 - pos1.x : pos1.x + 50;
y = pos1.y >= 0 ? 50 - pos1.y : pos1.y + 50;
return min(x, y) <= dis;
}
//第一跳及判断是否能够从岛跳到鳄鱼上去
bool firstJump(position pos, int dis)
{
int jump = (int)(sqrt(pos.x * pos.x + pos.y * pos.y) - 7.5);
return jump <= dis;
}
//深度优先搜索
bool dfs(vector<position> pos, int s,
vector<bool> &visit, int dis)
{
visit[s] = true;
//判断是否能够跳到岸边
if (isSafe(pos[s], dis))
return true;
for (int i = 0; i < pos.size(); ++i) {
//判断是否能够从一条鳄鱼跳到另外一条鳄鱼上去
if (i != s && !visit[i]
&& getDistance(pos[i], pos[s]) <= dis) {
if (dfs(pos, i, visit, dis))
return true;
}
}
return false;
}
void canSave(vector<position> pos, int dis)
{
vector<bool> visit(pos.size(), false);
for (vector<bool>::size_type i = 0; i < pos.size(); ++i) {
if (!visit[i] && firstJump(pos[i], dis)) {
if (dfs(pos, i, visit, dis)) {
cout << "Yes" << endl;
return ;
}
}
}
cout << "No" << endl;
}
int main()
{
int N, dis;
vector<position> pos;
cin >> N >> dis;
for (int i = 0; i != N; ++i) {
int x, y;
cin >> x >> y;
pos.push_back(position(x, y));
}
canSave(pos, dis);
return 0;
}