我是靠谱客的博主 体贴奇迹,最近开发中收集的这篇文章主要介绍07-图5 Saving James Bond - Hard Version (30分),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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 NN (le 100100), the number of crocodiles, and DD, the maximum distance that James could jump. Then NN lines follow, each containing the (x, y)(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)(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

在 easy version 的基础上,如果能跳上岸,问最少跳几下,并且按照跳的顺序给出鳄鱼坐标。

这是无权图的最短路径问题。可以用方法一dijkstra,方法二bfs。


方法一:dijkstra.对第一跳做处理。每一个第一跳dijkstra,如果跳的步少,更新存储鳄鱼下标。如果相同,加入。(用vector很方便)。然后找到第一步跳的最少的。题目保证唯一。从第一步跳的最少的,再一次dijkstra,剩下的就是模板问题了,好好背吧。


方法二:bfs.好好背模板吧。(STL大法好,stack,vector很方便)。


注意:如果能直接一步跳到岸上,就没必要找第一跳能跳到的鳄鱼了.


方法一:

#include<stdio.h>
#include<math.h>
#define inf 9999999
int n,d,flag=0,exist=0,newp;
int mark[101];
int path[101];
int distance[101];
struct node{
int x;
int y;
}stu[101];
double dis(int x,int y){
return sqrt(x*x+y*y);
}
int canjump(int x1,int y1,int x2,int y2){
if(sqrt(pow((x1-x2),2)+pow(y1-y2,2))<=d){
return 1;
}
else{
return 0;
}
}
void init(){
int i;
flag=0;
for(i=0;i<n;i++){
mark[i]=0;
path[i]=-1;
distance[i]=inf;
}
}
void dijkstra(int s){
mark[s]=1;
distance[s]=1;//从第一跳开始 !
path[s]=-2;
int i;
newp=s;
while(1){
if(50-abs(stu[newp].x)<=d||50-abs(stu[newp].y)<=d){
distance[newp]++;//还要跳到岸上!
flag=1;exist=1;break;
}
for(i=0;i<n;i++){
if(mark[i]==0&&canjump(stu[newp].x,stu[newp].y,stu[i].x,stu[i].y)){
if(distance[newp]+1<distance[i]){
distance[i]=distance[newp]+1;
path[i]=newp;
}
}
}
int min=inf;
for(i=0;i<n;i++){
if(mark[i]==0&&distance[i]<inf){
if(distance[i]<min){
min=distance[i];
newp=i;
}
}
}
mark[newp]=1;
if(min==inf){
break;
}
}
}
int main(){
int i;
scanf("%d %d",&n,&d);
for(i=0;i<n;i++){
scanf("%d %d",&stu[i].x,&stu[i].y);
}
if(d>=50-7.5){
printf("1n");return 0;//最小N,一步跳到岸 错了这个点
}
int firstjump[n];//第一跳下标
int numfirst=0;
for(i=0;i<n;i++){
if(d+7.5>=dis(stu[i].x,stu[i].y)){
firstjump[numfirst++]=i;
}
}
int min=inf,index;
int store[n],cou=0;
for(i=0;i<numfirst;i++){
init();
dijkstra(firstjump[i]);
if(flag){//找到路径
if(distance[newp]<min){
min=distance[newp];
cou=0;
store[cou++]=firstjump[i];
}
else if(distance[newp]==min){
store[cou++]=firstjump[i];
}
}
}
if(exist==0){
printf("0");return 0;
}
min=inf;
int indexmin;//如果最短路径相同,则距离源点最近的点唯一(题目保证)
for(i=0;i<cou;i++){
int index=store[i];
if(min>dis(stu[index].x,stu[index].y)-7.5){
min=dis(stu[index].x,stu[index].y)-7.5;
indexmin=store[i];
}
}
init();//别忘了初始化 !!!!
dijkstra(indexmin);
printf("%dn",distance[newp]);
cou=0;
while(1){
store[cou++]=newp;
newp=path[newp];
if(newp==-2){
break;
}
}
for(i=cou-1;i>=0;i--){
printf("%d %dn",stu[store[i]].x,stu[store[i]].y);
}
} 

方法二:

#include<stdio.h>//无权图 bfs也可以
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<algorithm>
#define inf 9999999
using namespace std;
int head;
int n,d,flag,flag2=0;
int inq[101];
int path[101];
int layer[101];
struct node{
int x;
int y;
}stu[101];
double dis(int x1,int y1,int x2,int y2){
return sqrt(pow(x1-x2,2)+pow(y1-y2,2));
}
void init(){
int i;
flag=0;
for(i=0;i<n;i++){
inq[i]=0;
path[i]=-1;
layer[i]=0;
}
}
void bfs(int s){
int i;
inq[s]=1;
path[s]=-2;
queue<int>q;
q.push(s);
layer[s]=1;
while(!q.empty()){
head=q.front();
q.pop();
if(50-abs(stu[head].x)<=d||50-abs(stu[head].y)<=d){
layer[head]++;flag=1;flag2=1;break;
}
for(i=0;i<n;i++){
if(inq[i]==0&&dis(stu[head].x,stu[head].y,stu[i].x,stu[i].y)<=d){
path[i]=head;
layer[i]=layer[head]+1;
inq[i]=1;
q.push(i);
}
}
}
}
int main(){
int i;
scanf("%d %d",&n,&d);
for(i=0;i<n;i++){
scanf("%d %d",&stu[i].x,&stu[i].y);
}
if(d>=50-7.5){
printf("1");return 0;
}
int firstjump[n];
int coufirst=0;
for(i=0;i<n;i++){//寻找第一跳
if(d+7.5>=dis(stu[i].x,stu[i].y,0,0)){
firstjump[coufirst++]=i;
}
}
if(coufirst==0){
printf("0");return 0;
}
int min=inf;
vector<int>coumin;
for(i=0;i<coufirst;i++){
init();
bfs(firstjump[i]);
if(flag==1){
if(layer[head]<min){
min=layer[head];
coumin.clear();
coumin.push_back(firstjump[i]);
}
else if(layer[head]==min){
coumin.push_back(firstjump[i]);
}
}
}
if(coumin.empty()) {
printf("0");return 0;
}
min=inf;
int minindex;
for(i=0;i<coumin.size();i++){
int index=coumin[i];
if(min>dis(stu[index].x,stu[index].y,0,0)-7.5){
min=dis(stu[index].x,stu[index].y,0,0)-7.5;
minindex=index;
}
}
init();
bfs(minindex);
printf("%dn",layer[head]);
stack<int>s;
do{
s.push(head);
head=path[head];
}while(head!=-2);
while(!s.empty()){
int temp=s.top();
s.pop();
printf("%d %dn",stu[temp].x,stu[temp].y);
}
}


最后

以上就是体贴奇迹为你收集整理的07-图5 Saving James Bond - Hard Version (30分)的全部内容,希望文章能够帮你解决07-图5 Saving James Bond - Hard Version (30分)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部