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 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 a shortest path to reach one of the banks. The length of a path is the number of jumps that James has to make.
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, if James can escape, output in one line the minimum number of jumps he must make. Then starting from the next line, output the position (x,y) of each crocodile on the path, each pair in one line, from the island to the bank. If it is impossible for James to escape that way, simply give him 0 as the number of jumps. If there are many shortest paths, just output the one with the minimum first jump, which is guaranteed to be unique.
Sample Input 1:
17 15
10 -21
10 21
-40 10
30 -50
20 40
35 10
0 -10
-25 22
40 -40
-30 30
-10 22
0 11
25 21
25 10
10 10
10 35
-30 10
Sample Output 1:
4
0 11
10 21
10 35
Sample Input 2:
4 13
-12 12
12 12
-12 -12
12 -12
Sample Output 2:
0
#include<cstdio>
#include<queue>
#include<stack>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = ;
const int minLen = - 15.0/; struct Point
{
int x,y;
}point[maxn]; int path[maxn] = {};
bool vis[maxn] = {};
int n,d; void BFS();
void init(int b[]);
bool cmp(int x,int y);
int FirstJump(int v);
bool isSafe(int v);
bool Jump(int v1,int v2); int main()
{
scanf("%d%d",&n,&d);
for (int i = ; i < n; i++)
{
scanf("%d%d",&point[i].x,&point[i].y);
} if (d >= minLen)
{
printf("1\n");
}
else
{
BFS();
}
return ;
} void BFS()
{
int b[maxn];
init(b);
sort(b,b+n,cmp); queue<int> q;
int last;
int tail;
int step = ; for (int i = ; i < n; i++)
{
if(FirstJump(b[i]))
{
q.push(b[i]);
vis[b[i]] = true;
last = b[i];
}
} while(!q.empty())
{
int now = q.front();
q.pop(); if (isSafe(now))
{
int k =;
stack<int> s;
cout << step << endl; while (k < step)
{
s.push(now);
now = path[now];
k++;
} while (!s.empty())
{
now = s.top();
s.pop();
cout << point[now].x << " " << point[now].y << endl;
}
return;
} for (int i = ; i < n; i++)
{
if (!vis[i] && Jump(now,i))
{
q.push(i);
vis[i] = true;
tail = i;
path[i] = now;
}
} if (last == now)
{
last = tail;
step++;
}
} if (q.empty())
{
cout << "" << endl;
}
} void init(int b[])
{
for (int i = ; i < n; i++)
{
b[i] = i;
}
} bool cmp(int x,int y)
{
return FirstJump(x) < FirstJump(y);
} int FirstJump(int v)
{
int dis = pow(point[v].x,) + pow(point[v].y,);
int dis_Jump = pow(15.0/ + d,);
if (dis <= dis_Jump)
{
return dis;
}
else
{
return ;
}
} bool isSafe(int v)
{
int dis_safe = - d;
return abs(point[v].x) >= dis_safe || abs(point[v].y) >= dis_safe;
} bool Jump(int v1,int v2)
{
int dis_x = pow(point[v1].x-point[v2].x, );
int dis_y = pow(point[v1].y-point[v2].y, );
if (dis_x + dis_y <= d*d)
{
return true;
}
else
{
return false;
}
}
07-图5 Saving James Bond - Hard Version (30 分)的更多相关文章
- PTA 07-图5 Saving James Bond - Hard Version (30分)
07-图5 Saving James Bond - Hard Version (30分) This time let us consider the situation in the movie ...
- pat06-图4. Saving James Bond - Hard Version (30)
06-图4. Saving James Bond - Hard Version (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...
- PTA 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 ...
- Saving James Bond - Easy Version (MOOC)
06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...
- pat05-图2. Saving James Bond - Easy Version (25)
05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...
- Saving James Bond - Hard Version
07-图5 Saving James Bond - Hard Version(30 分) This time let us consider the situation in the movie &q ...
- PAT Saving James Bond - Easy Version
Saving James Bond - Easy Version This time let us consider the situation in the movie "Live and ...
随机推荐
- GIT VI操作汇总
在Git Bash Here中用命令行 pull\push\merge 代码,如果存在冲突或者自动合并时,会自动进入VI界面 1.按下 ESC 键,退出编辑模式,切换到命令模式. 2.输入 :wq , ...
- 使用Supervisord软件管理go服务进程
一.介绍Supervisord软件1.什么是Supervisord?Supervisord是一个非常优秀的进程管理工具,使用Python开发.它可以在类UNIX系统的方式让用户来准确地监视和控制后台一 ...
- python基础--数据结构之字典
字典 特点:无序,键唯一 目录 1.字典的创建 2. .setdefault 的使用 3. 字典中的查找 4.字典中的改 5. 字典中的删除 6. 打印字典的方法 7. 格式化字符串 8. 合并字符 ...
- 记录下hbuilder vue项目打包APP 在IOS上点击延迟的问题
做的项目打包成APP在IOS 上有延迟问题,在安卓下却不会,联想到之前 用IONIC时打包的APP也是 在IOS下有300毫秒延迟问题.所以 只能 认吧. 安装fastclick 插件: npm in ...
- jQuery绑定事件的四种方式:bind、live、delegate、on
1.jQuery操作DOM元素的绑定事件的四种方式 jQuery中提供了四种事件监听方式,分别是bind.live.delegate.on,对应的解除监听的函数分别是unbind.die.undele ...
- 英语orientaljasper鸡血石orientaljasper单词
鸡血石(orientaljasper),是辰砂条带的地开石,因鲜红色似鸡血的辰砂(朱砂)而得名.鸡血石含有辰砂(朱砂).石英.玉髓35%-45%.磁铁矿.赤铁矿6%-12%.辰砂约5%-8%. 鸡血石 ...
- OpenSessionInViewFilter 的配置及替代方案
OpenSessionInViewFilter 的配置及替代方案 博客分类: hibernate OpenSessionInViewFilter 的配置及替代方案 Spring 为我们提供了一个叫做 ...
- struts2启动报错:ERROR com.opensymphony.xwork2.conversion.impl.DefaultConversionPropertiesProcessor - Conversion registration error
[framework] 2019-12-05 11:34:05,441 - org.springframework.web.context.ContextLoader -5352 [RMI TCP C ...
- 【JMeter】压力测试工具的概览与使用
软件工程综合实践第五次个人作业 作业要求:在软件测试章节中,我们介绍了不少VSTS的软件测试工具,请使用一些其他平台上的测试工具,并写博客介绍如何在你的项目中具体使用. 前言: 第一次看到这个作业 ...
- Django rest framework 之版本
一.通过 QueryParameterVersioning 获取版本 通过 QueryParameterVersioning 从 get 请求中获取版本信息: 1.新建 app,名为 api,Proj ...