浙大数据结构题目集:Saving James Bond - Easy Version

浙大数据结构题目集: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
思路:
本题主要考察图的深度遍历。要注意:第一次跳跃要单独写,因为跳跃的半径不同。要先判断两个节点的距离是否在跳跃半径之内,满足要求才可继续递归遍历。

#include 
#include 
#define MINLEN 42.5

using namespace std;
int n, m;
struct point{
     
  //鳄鱼位置坐标
  int x;
  int y;
}p[101];
bool visited[101] = {
     false}; //已经访问过的节点
bool answer = false; //判别是否找到路径


bool firstjump(int i){
     
  //判断能否第一次跳跃到i结点
  int a, b, r;
  a = pow(p[i].x, 2);
  b = pow(p[i].y, 2);
  r = (m+7.5)*(m+7.5);
  return (r >= (a+b));
}


bool is_safe(int x){
     
  //是否可以从这个节点直接离开水域
  if((p[x].x + m >= 50)||(p[x].x - m <= -50)||p[x].y + m >= 50||p[x].y - m <= -50)
    return true;
  return false;
}


bool jump(int x, int i){
     
  //判断能否从一个结点跳跃到另一个结点
  int a, b, r;
  a = pow(p[x].x - p[i].x, 2);
  b = pow(p[x].y - p[i].y, 2);
  r = m*m;
  return (r >= (a+b));
}


bool DFS(int x){
     
  //深度优先遍历
  visited[x] = true;
  
  if(is_safe(x)) //已经可以跳出
  {
     answer = true; return answer;}
  
  for(int i = 0; i < n; i++){
     
    if(!visited[i] && jump(x, i))
      answer = DFS(i);
  }
  
  return answer;
}


int main(){
     
  
  cin>>n>>m;
  
  if(m >= MINLEN){
     
    //能否一次就从小岛跳出
    cout<<"Yes"<<endl;
    return 0;
  }
  
  for(int i = 0; i < n; i++){
     
    cin>>p[i].x>>p[i].y;
  }
  
  for(int i = 0; i < n; i++){
     
    if(firstjump(i) && !visited[i])
      if(DFS(i)) //当answer为真时跳出循环
        break;
  }
  
  if(answer == true)
    cout<<"Yes"<<endl;
  else
    cout<<"No"<<endl;
  
  return 0;
}

你可能感兴趣的:(数据结构刷题,#,课后编程作业题,数据结构,dfs,二叉树,leetcode,算法)