Saving James Bond - Easy Version (MOOC)
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 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算法
int DFS ( Vertex V )
{ visited[V] = true;
if ( IsSafe(V) ) answer = YES;
else {
for ( V 的每个邻接点 W )
if ( !visited[W] ) {
answer = DFS(W);
if (answer==YES) break;
}
}
return answer;
代码:
#include<cstdio>
#include<cmath>
using namespace std;
struct node{
int x,y;
}G[];
int n,d;
int vis[];
int ans=;
bool firstJump(int i){
int p1=pow(G[i].x,);
int p2=pow(G[i].y,);
int r=(d+7.5)*(d+7.5);
if(p1+p2<=r)return true;
return false;
}
bool jump(int v,int i){
int p1=pow(G[v].x-G[i].x,);
int p2=pow(G[v].y-G[i].y,);
if(p1+p2<=d*d)return true;
else return false;
}
bool isSave(int v){
if(G[v].x+<=d||-G[v].x<=d||+G[v].y<=d||-G[v].y<=d)return true;
return false;
}
//dfs 1.遍历所有结点,找到能够跳转的结点进行dfs 2.如果结点可以跳上岸,返回结果1.
int dfs(int v){
vis[v]=;
if(isSave(v))return ;
for(int i=;i<n;i++){
if(!vis[i]&&jump(v,i)){
ans=dfs(i);
if(ans) break;//如果换成了if(!ans)return ans; sample 2出现错误。因为循环并没有结束,不能用return
}
}
return ans;
} int main(){
scanf("%d %d",&n,&d);
getchar();
for(int i=;i<n;i++){
scanf("%d %d",&G[i].x,&G[i].y); //struct结构存储x,y坐标
getchar();
}
for(int i=;i<n;i++){
if(firstJump(i)&&!vis[i]){ //首先判断第一步能否跳出,同时没有遍历过该结点
ans=dfs(i);
if(ans)break;
}
}
if(ans)printf("Yes");
else printf("No"); return ;
}
Saving James Bond - Easy Version (MOOC)的更多相关文章
- 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 Bon ...
- 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 Bon ...
- 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 Bon ...
- Saving James Bond - Easy Version 原创 2017年11月23日 13:07:33
06-图2 Saving James Bond - Easy Version(25 分) This time let us consider the situation in the movie &q ...
- pat05-图2. Saving James Bond - Easy Version (25)
05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...
- PAT Saving James Bond - Easy Version
Saving James Bond - Easy Version This time let us consider the situation in the movie "Live and ...
- 07-图5 Saving James Bond - Hard Version(30 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- 07-图5 Saving James Bond - Hard Version (30 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- 06-图2 Saving James Bond - Easy Version
题目来源:http://pta.patest.cn/pta/test/18/exam/4/question/625 This time let us consider the situation in ...
随机推荐
- PAT——1061. 判断题
判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过100的正整数N和M,分别是学生人数和判断题数量.第二行给出M个不超过5的正 ...
- 【luogu P4180 严格次小生成树[BJWC2010]】 模板
题目链接:https://www.luogu.org/problemnew/show/P4180 这个题卡树剖.记得开O2. 这个题inf要到1e18. 定理:次小生成树和最小生成树差距只有在一条边上 ...
- G1 GC日志:Application time: 0.8766273 seconds
启动日志一直循环: 1.159: Application time: 0.8766273 seconds 1.160: Total time for which application threads ...
- 每天to do list
至少写一页书 写代码做一个实验 读10+页专业书 一年时间,如果经济状况没有改善的话,回归企业.
- NOIP Day1总结
Day1T1玄学考试 在开始之前,我犯了考前综合症,各种不安各种焦躁. 结果当我去到考场的时候,看了T1...... T1:road 这不是裸的原题么这!我当时心里瞬间想到积木大赛.这明显就是积木大赛 ...
- Ubuntu18.04挂载exfat格式移动硬盘
1.安装exfat-fuse 命令:sudo apt-get install exfat-fuse 2.重新插拔移动硬盘,即可识别 查看挂载命令:lsblk
- Uncaught SyntaxError: Unexpected token : 开发遇到的跨域问题
先描述一下,这个问题,是如何遇到的 在ajax调用远程服务的时候,报了一个Origin xxxxxx is not allowed by Access-Control-Allow-Origin 的错误 ...
- JQuery传值,接收PrintWriter的int接收不了
<servlet>中的代码 <JSP中的代码> 很苦恼,我想要,<servlet>中传入的(0),在<jsp>中接收到这个参数,然后进行判断,从而达到判 ...
- Hadoop源码学习笔记之NameNode启动场景流程四:rpc server初始化及启动
老规矩,还是分三步走,分别为源码调用分析.伪代码核心梳理.调用关系图解. 一.源码调用分析 根据上篇的梳理,直接从initialize()方法着手.源码如下,部分代码的功能以及说明,已经在注释阐述了. ...
- 智能家居系统 Home Assistant 系列 --介绍篇
一. HomeAssistant 是什么? HomeAssistant是构建智慧空间的神器.是一个成熟完整的基于 Python 的智能家居系统,设备支持度高,支持自动化(Automation).群组化 ...