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 ...
随机推荐
- 使用Django创建一个后端是SQLSERVER的简单系统(一)Django连接SQLServer数据库
window环境下Django连接SQLSERVER, 1.创建项目: 2.创建application: 3.使用pycharm打开项目,如下图: 4.配置虚拟环境: 安装Django\pyodbc\ ...
- C# Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
一.问题描述 在做C# 的 Guid 转换时,出现这个问题:Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-x ...
- Ubuntu16.04启动xtion pro live报错
william@william-System-Product-Name:~$ roslaunch openni2_launch openni2.launch ... logging to /home/ ...
- Perfect service(树形dp)
Perfect service(树形dp) 有n台机器形成树状结构,要求在其中一些机器上安装服务器,使得每台不是服务器的计算机恰好和一台服务器计算机相邻.求服务器的最小数量.n<=10000. ...
- Spring MVC那点事儿
自问自答 1 Spring MVC的启动原理? spring mvc是基于ioc容器的,因此需要先创建IOC容器,才能创建对应的spring mvc执行环境. IOC容器是通过ContextLoade ...
- Oracle表连接(转)
表之间的连接 Join是一种试图将两个表结合在一起的谓词,一次只能连接2个表,表连接也可以被称为表关联.在后面的叙述中,我们将会使用”row source”来代替”表”,因为使用row source更 ...
- Hadoop集群配置免密SSH登录方法
Hadoop集群包含1个主节点和3个从节点,需要实现各节点之间的免密码登录,下面介绍具体的实现方法. 一.Hadoop集群环境 二.免密登录原理 每台主机authorized_keys文件里面包含的主 ...
- 帝都Day4(1)——还是dp
其实是day4 一.洛谷P1018 乘积最大 f[i][j]表示前i个数 切成j块 用f[i][j]而不用f[i][j][k](i到j切成k块)呢? Luogu1043 前缀和(好算一段里的数的和)+ ...
- SSM+Druid的搭建
SSM+druid开发配置 工程目录 1.先从pom文件开始吧 <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...
- Git 通过https向码云推送项目