pta编程题19 Saving James Bond 2
其它pta数据结构编程题请参见:pta
和简单版本不同的是,简单版本只需判断能否到达岸边,而这个版本要求求出最少跳数的路径。
简单版本用dfs实现,而这道题用BFS实现。
注意:
岛半径为7.5,而不是15。另外注意一步跳到岸边的情况。
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
const int maxInt = ; int N, D;
struct point
{
int x, y;
}G[]; struct queue
{
int arr[];
int head = ;
int tail = ;
}; void enQueue(int i, queue& q);
int deQueue(queue& q);
bool isEmpty(queue q); bool firstJump(int i);
bool jump(int i, int j);
bool toBank(int i);
bool bfs(int s, vector<int>& p);
bool shorter(vector<int> path1, vector<int> path2); int main()
{
int i;
cin >> N >> D;
for (i = ; i < N; i++)
cin >> G[i].x >> G[i].y; bool first = true;
vector<int> path, minPath;
for (i = ; i < N; i++)
{
if (firstJump(i) && bfs(i, path))
{
if (first) //第一个可达到岸边的鳄鱼
{
minPath = path;
first = false;
}
else if (shorter(path, minPath))
minPath = path;
}
} if (first) cout << ; //岸边不可达
else if (7.5 + D >= ) //直接跳到岸边
cout << ;
else {
cout << minPath.size() + << endl;
for (i = minPath.size() - ; i >= ; i--)
{
int id = minPath[i];
cout << G[id].x << " " << G[id].y << endl;
}
}
return ;
} bool firstJump(int i)
{
bool a = pow(G[i].x, ) + pow(G[i].y, ) <= pow((7.5 + D), );
return a;
} bool jump(int i, int j)
{
return pow(G[i].x - G[j].x, ) + pow(G[i].y - G[j].y, ) <= D * D;
} bool toBank(int i)
{
int x = G[i].x, y = G[i].y;
return (x <= D - || x >= - D || y <= D - || y >= - D);
} bool shorter(vector<int> path1, vector<int> path2)
{
if (path1.size() != path2.size())
return path1.size() < path2.size();
else {
int a = path1[path1.size() - ];
int b = path2[path2.size() - ];
return pow(G[a].x, ) + pow(G[a].y, ) <= pow(G[b].x, ) + pow(G[b].y, );
}
} bool bfs(int s, vector<int>& p)
{
queue q;
enQueue(s, q);
bool marked[] = {};
int pathTo[];
marked[s] = true;
pathTo[s] = -; bool success = false;
int v, w;
while (!isEmpty(q))
{
v = deQueue(q);
if (toBank(v)) //可以到岸边
{
success = true;
break;
}
for (w = ; w < N; w++)
{
if (!marked[w] && jump(v, w))
{
enQueue(w, q);
marked[w] = true;
pathTo[w] = v;
}
}
} if (!success) return false;
vector<int> vec;
while (v != -)
{
vec.push_back(v);
v = pathTo[v];
}
p = vec;
return true;
} void enQueue(int i, queue& q)
{
q.tail = (q.tail + ) % ;
q.arr[q.tail] = i;
} int deQueue(queue& q)
{
q.head = (q.head + ) % ;
return q.arr[q.head];
} bool isEmpty(queue q)
{
return q.head == q.tail;
}
pta编程题19 Saving James Bond 2的更多相关文章
- pta 编程题16 Saving James Bond - Easy Version
其它pta数据结构编程题请参见:pta 题目 主要用到了深度优先搜索. #include <iostream> using namespace std; struct Vertex { i ...
- 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 ...
- Saving James Bond(dijk)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1245 Saving James Bond Time Limit: 6000/3000 MS (Java ...
- pat06-图4. Saving James Bond - Hard Version (30)
06-图4. Saving James Bond - Hard Version (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...
- 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 ...
- Saving James Bond - Easy Version (MOOC)
06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...
- Saving James Bond - Hard Version
07-图5 Saving James Bond - Hard Version(30 分) This time let us consider the situation in the movie &q ...
- 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 ...
随机推荐
- PS2018学习笔记(30-35节)
30-35:万能的钢笔-制图抠图必学-part(1-6) # 本节知识点: 钢笔工具 贝塞尔曲线 绘图方式 光标状态认识 路径 形状 形状工具 矢量蒙版 # 本节段落表: 钢笔工具知识 直线绘制知识 ...
- ProtoBuf练习
环境设置 项目地址 https://github.com/silvermagic/ProtoBufDev.git 操作系统 64位 Fedora 24 安装protobuf $ git clone h ...
- Unity3D -- shader常用函数和变量
最近在学习Unity Shader,写Shader的时候总是忘记Unity为我们提供的函数.变量怎么写的,这里整理一下,方便自己查阅,也提供给网友,学习Shader不易. 1.函数 float3 Wo ...
- KONG -- 配置 service 并添加 key-auth
默认情况下, KONG 监听下面几个端口: 8000 这个端口用于监听客户端的 HTTP 请求,并转发给上游服务 8443 这个端口用于监听客户端的 HTTPS 请求,并转发给上游服务 800 ...
- 浅谈UML——九种图(一)
前言 学UML将近两个星期了,对UML有了一定的了解,学过的没学过的都知道UML中最最最核心的部分要数那九个图了.浅谈UML九种图. 实例 1.用例图: 什么是用例?描绘一个系统外在可见的需求情况,是 ...
- Android代码笔记
1. 如何监听Android的短信收发,自动填充验证码? getContentResolver().registerContentObserver(Uri.parse(SMS_URI_ALL), tr ...
- java 调用SAP RFC函数错误信息
RFC接口调用SAP如果有异常会通过com.sap.mw.jco.JCO$Exception: 抛出异常 在开发中遇到的异常有如下 用户名密码可能是错误或者用户无权限,确认用户,必要时联系SAP负责人 ...
- Angular输入框内按下回车会触发其它button的点击事件的解决方法
方法:给button按钮添加type=“botton”属性
- CF987B High School: Become Human 数学
题意翻译 题目大意 输入一个 xxx ,一个 yyy ,求是 xyx^yxy 大还是 yxy^xyx 大. (1≤x,y≤109)(1≤x,y≤10^9)(1≤x,y≤109) 输入输出格式 输入格式 ...
- mathjax;latex
\lfloor $\lfloor$ \rfloor $\rfloor$ \sum_{i=1}^{n} $\sum_{i=1}^{n}$ \mu $\mu$ \mid $\mid$ \Leftright ...