其它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. 同台电脑 多Git账号同时使用

    前言 有次周末忘记带公司电脑回来,恰好遇到有个问题需要修复,又不想跑公司一趟,于是研究了下如何在自己电脑上同时使用两个 git 账号 正文 1. 首先就和第一次安装 git 时一样,使用 sha算法 ...

  2. 给WPF的MessageBox启用视觉样式

    WPF的MessageBox为什么会这样 我的一个同学跟我说:“WPF不是新一代技术吗?怎么连MessageBox都没WinForm 的好看?” 上图是Windows Forms 的MesssageB ...

  3. Dapper 存储过程、事务等

    接上一篇<Dapper 增删改查> 0.存储过程 create proc p_login ), ), ) output as begin if(exists(select * from U ...

  4. 获取指定日期相关DATENAME和DATEPART数据

    DATENAME和DATEPART有何区别,Insus.NET写成一个函数,可以方便查询与对比: 一个是返回一个字符串,另一个是返回一个整数. SET ANSI_NULLS ON GO SET QUO ...

  5. java IO流部分知识点

    IO流部分 IO流常用的有:字符流.字节流.缓冲流.序列化流.RandomAccessFile类等 1.字节流 FileInputStream/FileOutputStream BufferedInp ...

  6. 设计模式实战研磨 ——第1篇 UML环境搭建

    starUML是开源的基于统一模式语言与模式驱动开发的平台,前身是Plastic,从1996年开始开发.1998年开始,Plastic转变为UML建模工具.2005年改名为StarUML,最新版本St ...

  7. IDEA的database插件无法链接mysql的解决办法(08001错误)

    1.问题 首先先说问题,用navicat链接数据库正常,mysql控制台操作正常,但是用IDEA的数据库插件链接一直报 08001 错误,具体见下图: 错误:Connection to eshop@l ...

  8. P1067 多项式输出(模拟水题)

    题目描述 一元nn次多项式可用如下的表达式表示: 其中,a_ix^iai​xi称为ii次项,a_iai​ 称为ii次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式: ...

  9. idea中导入githup项目

    转载大神: https://blog.csdn.net/m0_37630602/article/details/69950528

  10. 实现一个类似bootstrap的多级下拉菜单

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...