概述
PTA 07-图5 Saving James Bond - Hard Version
原题链接
This time let us consider the situation in the movie “Live and Let Die” in which James Bond, the world’s most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape – he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head… Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).
Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him a shortest path to reach one of the banks. The length of a path is the number of jumps that James has to make.
Input Specification:
Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the (x,y) location of a crocodile. Note that no two crocodiles are staying at the same position.
Output Specification:
For each test case, if James can escape, output in one line the minimum number of jumps he must make. Then starting from the next line, output the position (x,y) of each crocodile on the path, each pair in one line, from the island to the bank. If it is impossible for James to escape that way, simply give him 0 as the number of jumps. If there are many shortest paths, just output the one with the minimum first jump, which is guaranteed to be unique.
Sample Input 1:
17 15
10 -21
10 21
-40 10
30 -50
20 40
35 10
0 -10
-25 22
40 -40
-30 30
-10 22
0 11
25 21
25 10
10 10
10 35
-30 10
结尾无空行
Sample Output 1:
4
0 11
10 21
10 35
结尾无空行
Sample Input 2:
4 13
-12 12
12 12
-12 -12
12 -12
Sample Output 2:
0
思路
在前一个程序上进行修改(感觉这题多了很多细节要注意)
需要注意的地方:
(1)特判岸上的鳄鱼和岛上的->不计入
(2)当有很多个最短路时,选择第一跳距离最短的
引入FirstJump()函数,对first[]进行修改,利用map反向求i
(3)特判可以一步上岸
Code
#include<iostream>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<cmath>
#define MaxSize 102
#define radius 7.5
using namespace std;
int NumberofVertex,D;
map<int,int>mp;
struct CroNode{
int x;
int y;
}cro[MaxSize];
int path[MaxSize];//记录最短路径
int dist[MaxSize];//记录最短路径值
int last;//记录最后一个位置
int first[MaxSize];
//能否到达,有没有边
bool Distance(int v1,int v2){
int maxdis=D;
if(v1==0){
maxdis+=radius;//第一跳特判
}
double dis=sqrt(pow(cro[v1].x-cro[v2].x,2)+pow(cro[v1].y-cro[v2].y,2));
bool res=dis>maxdis?false:true;
return res;
}
//判断这个点的下一步能不能直接结束循环
bool CanEscape(int v){
double Maxdis=D;
if(v==0){//起点在孤岛时的特判
Maxdis=D+radius;
}
if(cro[v].x+Maxdis>=50||cro[v].x-Maxdis<=-50||cro[v].y+Maxdis>=50||cro[v].y-Maxdis<=-50){
return true;
}else{
return false;
}
}
void FirstJump(){
int v1=0;
for(int v2=1;v2<=NumberofVertex;v2++){
first[v2]=sqrt(pow(cro[v1].x-cro[v2].x,2)+pow(cro[v1].y-cro[v2].y,2));
mp[first[v2]]=v2;
}
sort(first+1,first+NumberofVertex+1);
}
//Dijsktra类似BFS
bool Dijsktra(int v){
FirstJump();
queue<int> q;
dist[v]=0;//源头的dist记录为0
for(int i=1;i<=NumberofVertex;i++){
int j=mp[first[i]];
dist[j]=dist[0]+1;
path[j]=0;
q.push(j);
while(!q.empty()){
v=q.front();
q.pop();
for(int i=1;i<=NumberofVertex;i++){
if(Distance(v,i)){//是邻接点
if(dist[i]==-1){//没被记录过
dist[i]=dist[v]+1;
path[i]=v;
if(CanEscape(i)){
last=i;
return true;
break;
}else{
q.push(i);
}
}
}
}
}
}
return false;
}
int main(){
scanf("%d %d",&NumberofVertex,&D);
if(D+radius>=50){//可以一步上岸
printf("1");return 0;
}
cro[0].x=0;
cro[0].y=0;
for(int i=1;i<=NumberofVertex;i++){
int x,y;
scanf("%d %d",&x,&y);
if(x>50||x<-50||y>50||y<-50||sqrt(pow(x,2)+pow(y,2))<=radius){
i--;
}else{
cro[i].x=x;
cro[i].y=y;
}
}
for(int i=0;i<=NumberofVertex;i++){
dist[i]=-1;//初始化dist
}
stack<int> s;
if(Dijsktra(0)){
int temp=last;
int count=0,cur;
while(temp!=0){
count++;
s.push(temp);
temp=path[temp];
}
printf("%dn",count+1);//最后跳上岸算一步
while(!s.empty()){
cur=s.top();
s.pop();
printf("%d %d",cro[cur].x,cro[cur].y);
if(s.size()!=0){
printf("n");
}
}
}else{
printf("0");
}
return 0;
}
最后
以上就是沉静玉米为你收集整理的PTA 07-图5 Saving James Bond - Hard VersionPTA 07-图5 Saving James Bond - Hard Version思路Code的全部内容,希望文章能够帮你解决PTA 07-图5 Saving James Bond - Hard VersionPTA 07-图5 Saving James Bond - Hard Version思路Code所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复