其它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的更多相关文章

  1. pta 编程题16 Saving James Bond - Easy Version

    其它pta数据结构编程题请参见:pta 题目 主要用到了深度优先搜索. #include <iostream> using namespace std; struct Vertex { i ...

  2. 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 ...

  3. Saving James Bond(dijk)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1245 Saving James Bond Time Limit: 6000/3000 MS (Java ...

  4. pat06-图4. Saving James Bond - Hard Version (30)

    06-图4. Saving James Bond - Hard Version (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...

  5. pat05-图2. Saving James Bond - Easy Version (25)

    05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...

  6. PAT Saving James Bond - Easy Version

    Saving James Bond - Easy Version This time let us consider the situation in the movie "Live and ...

  7. Saving James Bond - Easy Version (MOOC)

    06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...

  8. Saving James Bond - Hard Version

    07-图5 Saving James Bond - Hard Version(30 分) This time let us consider the situation in the movie &q ...

  9. 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 ...

随机推荐

  1. 远程桌面连接KVM虚拟机

    问题描述 有些时候,由于网络存在问题,虚拟机无法获取到IP地址,无法使用spice或vnc来连接虚拟机,但是又需要连到虚拟机来排查故障 解决办法 编辑虚拟机配置 设置xml命名空间 <domai ...

  2. 51nod1419 【数学】

    思路: n<=3,就是n. 考虑n>3: 我们可以轻松证明n,n-1这两个数互质: 设gcd(n,n-1)=g,n=g*k1,n-1=g*k2; n-(n-1)=g(k1-k2)=1; 所 ...

  3. rowid去重(转)

    实际应用场景:数据去重--当多条记录主键相同或者多条记录完全一致时,只需要留下一条记录 delete from bal_acctbook_info where rowid not in (select ...

  4. 【转载】GlusterFS六大卷模式說明

    本文转载自翱翔的水滴<GlusterFS六大卷模式說明> GlusterFS六大卷說明 第一,分佈卷 在分布式卷文件被随机地分布在整个砖的体积.使用分布式卷,你需要扩展存储,冗余是重要或提 ...

  5. bzoj4200: [Noi2015]小园丁与老司机(可行流+dp)

    传送门 这该死的码农题…… 题解在这儿->这里 //minamoto #include<iostream> #include<cstdio> #include<cs ...

  6. C#网络编程学习(6)---序列化和反序列化

    1.什么是序列化和反序列化 当客户端和服务器进行远程连接时,互相可以发送各种类型的数据.但都要先把这些对象转换为字节序列,才能在网络上进行传输. 序列化:就是发送方 把对象转换为字节序列的过程. 反序 ...

  7. P3379 【模板】最近公共祖先(LCA)(倍增)

    这题有毒!!!!!!!!!! TM我重新打的板子,然而...... 5分钟打完 debug两小时 我的写法常数太大了 每次DFS都要For去更新F 最后写了快读才A 改: 只处理f[i][0] dfs ...

  8. thinkphp5引入百度编辑器

    在ThinkPHP的模板(html文件)中引入Ueditor  下载ueditor解压至public/static目录 在需要的页面引入js文件 <script type="text/ ...

  9. day23作业详解

    1.题目 2.题目详解 点击查看详细内容 1. 1-1 封装 把功能封装到类中 class Message(object): def email(self):pass def msg(self):pa ...

  10. Active Domain中的用户属性

    /// <summary> /// AD中的属性,没有出现的后续接着补充 /// </summary> public class LdapUserEntryProperty { ...