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 ...
随机推荐
- 【mysql--sql语句小问题总结】
1:查询过滤条件不为空: <select id="getActivityByIdAndEx" resultMap="BaseResultMap" > ...
- Android APK反编译技巧全讲解
导言:在我们安卓开发当中,我们不仅需要掌握基础的开发技能,也需要掌握软件的安全技能,这样才可以让我们的软件能够成为一款能够真正可以进行发布的软件,同时也可以让自己的核心技术不会被别人所盗取. 首先我们 ...
- ProtoBuf练习(一)
基础数据类型 protobuf语言的基础字段类型相当于C++语言的基础类型 工程目录结构 $ ls proto/ TFixed.proto TInt.proto TScalar.proto TStr. ...
- 【leetcode 106. 从中序与后序遍历序列构造二叉树】解题报告
前往 中序,后序遍历构造二叉树, 中序,前序遍历构造二叉树 TreeNode* build(vector<int>& inorder, int l1, int r1, vector ...
- 浮点数与快速log2
请先于浮点数的文章:http://blog.jobbole.com/86371/ 先贴一张关于float和double的图: float: double: 快速log2长这样: int flog2(f ...
- 洛谷P1912 [NOI2009]诗人小G(决策单调性)
传送门 题解 决策单调性是个啥……导函数是个啥……这题解讲的是啥……我是个啥…… //minamoto #include<iostream> #include<cstdio> ...
- iOS回顾笔记( 01 )-- XIB和纯代码创建应用的对比
header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...
- 记一次工作中的小BUG
今天在调试代码的时候总是遇到一个bug,百思不得其解!先上bug图 我用的webapi 集成的swagger,错误提示是路由名称冲突,可我仔细检查了下并没有冲突的路由地址啊!于是上网查找资料,有位网友 ...
- Jmeter_前端RSA加密下的登陆模拟_引用js文件实现(转)
在一次项目实战中,前端登录使用了RSA加密,使用LoadRunner压测的第一步,就是模拟用户登录,可惜loadRunner11并不能录制前端的加密过程,并且安装的LR是基于C语言版,网络上关于RSA ...
- Linux 查询服务器序列号命令
1.查看服务器型号:dmidecode | grep 'Product Name' 2.查看主板的序列号:dmidecode |grep 'Serial Number' 3.查看系统序列号:dmi ...