06-图2 Saving James Bond - Easy Version (25分)
2016-04-17 11:24
453 查看
06-图2 Saving James Bond - Easy Version (25分)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 heperformed 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 NN (100≤100),the number of crocodiles, and DD,the maximum distance that James could jump. Then NN linesfollow, each containing the xy(x,y) locationof 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
主要思路:
1、深度优先遍历图,没到达新结点,判断能否从当前结点一步跳到安全区域
2、图中两点间有边的条件是小于bond能跳的半径
3、第一步Bond跳动的距离可以视为加上15(小岛半径)
#include <iostream>#include <vector>using namespace std;bool distance(vector<vector<int> > &vec,int node,int i,int D)//是否能从node点跳到i点{int distance_x=vec[node][0]-vec[i][0];int distance_y=vec[node][1]-vec[node][1];if ((distance_x*distance_x+distance_y*distance_y)<=D*D){return true;}else{return false;}}bool safe(vector<vector<int> > &vec,int node,int D)//当前node点是否能一步跳到安全区域{int x=vec[node][0];int y=vec[node][1];if (x>= 50-D || x<=-50+D || y>=50-D || y<=-50+D){return true;}else{return false;}}bool DFS(vector<vector<int> > &vec,int node,vector<int> &flag,int N,int D)//深度优先遍历图{flag[node]=1;if (safe(vec, node, D)){return true;}for (int i=1; i<=N; ++i){if (distance(vec, node,i,D) && flag[i]==0){return DFS(vec, i, flag, N, D);}}return false;}int main(){int N=0,D=0;cin>>N>>D;if (D>=50-15){cout<<"Yes";}else{vector<vector<int> > position(N+1,vector<int>(2));for (int i=1; i<=N; ++i){cin>>position[i][0]>>position[i][1];}position[0][0]=0;position[0][1]=0;vector<int> flag(N+1,0);int flag_result=0;for (int i=1; i<=N; ++i){if (distance(position, i,0,D+15))//岛的半径15,可以视为Bond从岛中心点跳出的第一步有长度15的能力值增加{if(DFS(position, i, flag, N, D)){flag_result=1;break;}}}if (flag_result){cout<<"Yes";}else{cout<<"No";}}}
相关文章推荐
- LeetCode——042
- POJ 1015 动态规划
- VMware 虚拟机和 Macintosh 键盘对应关系表
- Visual Tracking with Fully Convolutional Networks
- 【笔试/面试】—— 从余弦定理到三角形两边之和大于第三边的证明
- c++模板特化和偏特化
- XCODE7以后不能使用HTTP协议解决办法
- XCODE7以后不能使用HTTP协议解决办法
- 构建之法阅读笔记06
- ggplot2 上篇
- Android 嵌套滑动机制(NestedScrolling)
- Xcode7 中创建静态库:.a 和 .framework(二)
- QT + vs 运行的项目出现控制台的解决办法
- Java基本语法和命名规范
- 互质和与约数和
- 遗传网络算法
- windows下安装SVN服务器端和客户端和MyEclipse支持
- Struts2 知识体系
- 大数
- Hadoop安装配置、运行第一个WordCount示例程序