zoukankan      html  css  js  c++  java
  • 06-图2 Saving James Bond

    不是我说,pats是真的坑 少个<math.h>头文件我以为代码错了,卡了我半小时
    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<iostream>
    #include<math.h>
    using namespace std;
    #define diameter 15.00
    #include<queue>
    struct Node             //鳄鱼信息
    {
    	double x;
    	double y;
    	bool visited = false;
    	bool safe = false;
    	bool jump = false;
    }node[100];
    int n;//鳄鱼数量 
    double r;//跳跃最大距离
    bool judge = false;
    
    double getlength(double x1, double y1, double x2, double y2)
    {
    	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    }
    bool getsafe(double x,double y)
    {
    	if (abs(x - 50) <= r || abs(x + 50) <= r || abs(y - 50) <= r || abs(y + 50) <= r)
    		return true;
    	else return false;
    }
    bool getjump(double x, double y)
    {
    	if (getlength(x, y, 0, 0) <= r + diameter / 2)
    		return true;
    	else return false;
    }
    void inition()
    {
    	cin >> n >> r;
    	for (int i = 0; i < n; i++) {
    		cin >> node[i].x >> node[i].y;
    		if (getsafe(node[i].x, node[i].y))
    			node[i].safe = true;
    		if (getjump(node[i].x, node[i].y))
    			node[i].jump = true;
    	}
    }
    void bfs(int i)
    {
    	node[i].visited = true;
    	queue<Node>q;
    	q.push(node[i]);
    	while (!q.empty()) {
    		Node tem = q.front();
    		q.pop();
    		int j;
    		if (tem.safe) {
    			judge = true;
    			return;
    		}
    		for (j = 0; j < n; j++) {
    			if (!node[j].visited && getlength(tem.x, tem.y, node[j].x, node[j].y) <= r) {
    				node[j].visited = true;
    				q.push(node[j]);
    			}
    		}
    	}
    }
    int main()
    {
    	inition();
    	for (int i = 0; i < n; i++) {
    		if (node[i].jump) {
    			bfs(i);
    		}
    	}
    	if (judge)
    		cout << "Yes";
    	else
    		cout << "No";
    	return 0;
    }
    
    
    
  • 相关阅读:
    动态规划(DP),模拟
    递推,动态规划(DP),字符串处理,最佳加法表达式
    递归,动态规划,找最短路径,Help Jimmy
    动规,模拟,递推,最长公共子序列
    递归,递推,记忆化搜索,空间优化(数字三角形)
    递归,记忆化搜索,(棋盘分割)
    递归,搜索,回溯,最优路径(线段)
    web前端开发初学者必看的学习路线(附思维导图)
    一位资深程序员大牛给予Java初学者的学习路线建议
    假如时光倒流,我会这么学习Java
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13109997.html
Copyright © 2011-2022 走看看