中国大学MOOC-陈越、何钦铭-数据结构 Saving James Bond - Easy Version

来源:互联网 发布:sql修改表的约束条件 编辑:程序博客网 时间:2024/05/22 08:15

题目描述:
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

思路:
这道题就是图遍历的一道应用,而且还是DFS的应用。要注意的是007的第一跳和其他的跳不同,所以第一次起跳要特殊处理。另外这道题还是对图的所有连通子集进行DFS遍历,这个不能忘记。而且,DFS不必完全进行完,只要007找到了一条鳄鱼,通过它007可以跳到岸上,那么DFS就可以停止了。

代码描述:

#include<iostream>#include<cmath>#define MaxSize 100typedef int Vertex;using namespace std;int N;//N表示鳄鱼的个数int D;//D表示007能跳跃的最大距离bool Visit[MaxSize];//判断节点是否被访问过struct Crocodiles{    int x, y;//鳄鱼的坐标    bool IsReachable;//判断007能否从这个鳄鱼跳到岸上};Crocodiles Cro[MaxSize];//保存鳄鱼的坐标信息等bool Judge(int x, int y){//判断007能否通过此鳄鱼跳上岸    int dist1 = abs(x)+D;int dist2=abs(y)+D;    if (dist1<50&&dist2<50){ return false; }    else { return true; }}void Input(){//输入函数    int x1, y1;    for (int i = 0; i < N; i++){        cin >> x1 >> y1;        Cro[i].IsReachable = Judge(x1, y1);//判断007能否直接从当前鳄鱼跳到岸上        Cro[i].x = x1; Cro[i].y = y1;    }}bool Calcuate(int x1, int y1, int x2, int y2, int total){    return total >= (int)sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));//判断total是否小于根号((x1-x2)^2+(y1-y2)^2)}bool DFS(Vertex v){    bool answer=0;    Visit[v] = true;//标记节点为已访问过    bool J;    if (Cro[v].IsReachable==true){ answer = true; }//如果007能通过这个节点直接到达岸边,就不再访问下面节点了    else{        for (int i = 0; i < N; i++){//遍历邻接点            J = Calcuate(Cro[i].x, Cro[i].y, Cro[v].x, Cro[v].y, D);//判断两个节点的距离是否小于D            if (!Visit[i]&&J){//如果007能够从j跳到i并且i还没有被访问过                answer=DFS(i);             }            if (answer) { break; }        }    }    return answer;}void Save007(){//处理所有情况    bool J;    bool answer = false;    for (int i = 0; i<N; i++){//以小岛为中心,搜索能跳到的鳄鱼        J = Calcuate(0, 0, Cro[i].x, Cro[i].y, 15 + D);//找寻位于以(0,0)为圆心,半径为15+D的圆中的鳄鱼        if (!Visit[i]&&J){//v还没有被访问,并且007可以从孤岛上跳到该鳄鱼头上            answer = DFS(i);        }        if (answer == true){ break; }//answer成立,就不必继续找了    }    if (answer==true){ cout << "Yes"; }    else { cout <<"No"; }}int main(){    cin >> N >> D;    Input();    if (D >= 35){//如果007能够一步跳出湖中,就不在进行判断        cout << "Yes";    }    else {        Save007();    }}
0 0