Saving James Bond

来源:互联网 发布:淘宝外贸原单江苏南通 编辑:程序博客网 时间:2024/06/05 14:35

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 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 1510 -2110 21-40 1030 -5020 4035 100 -10-25 2240 -40-30 30-10 220 1125 2125 1010 1010 35-30 10

Sample Output 1:

40 1110 2110 35

Sample Input 2:

4 13-12 1212 12-12 -1212 -12

Sample Output 2:

0

#include<iostream>

#include<vector>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
#define Maxnodenum 101
#define nolimitmax 100000
int flag=0;//为了标志第一次拓展外层

vector<double> dist(Maxnodenum,nolimitmax);//为了记住跳到每个点的步数

vector<int> path(Maxnodenum,-1);//为了记录路径
struct vertex{
int x;
int y;
};
struct graph{
int Nv;
int jump;
vertex G[Maxnodenum];
};
using Graph=graph*;
Graph BuildGraph(){
int v,x,y;
Graph gra=new graph();
cin>>gra->Nv>>gra->jump;
gra->G[0].x=gra->G[0].y=0;
    for(v=1;v<=gra->Nv;v++)
    {
    cin>>gra->G[v].x>>gra->G[v].y;
}
return gra;
}
double distance(vertex n1,vertex n2){
return sqrt((n1.x-n2.x)*(n1.x-n2.x)+(n1.y-n2.y)*(n1.y-n2.y));
}
int saved(Graph gra,int v){
if(gra->G[v].x>=50-gra->jump||gra->G[v].x<=-50+gra->jump||gra->G[v].y>=50-gra->jump||gra->G[v].y<=-50+gra->jump)
{while(path[v]!=-1){ 
    
    if(path[v]==0) { return 1;}
    v=path[v];        

 }  
    return 0;
}
bool point(Graph gra,int v){
int x=gra->G[v].x; int y=gra->G[v].y;
if(x*x+y*y<=7.5*7.5||x>=50||x<=-50||y>=50||y<=-50)
{return false;}
else {return true;
}
}
void save(Graph gra){
if(gra->jump>=50){
cout<<1; return;
}
    dist[0]=0; int v=0,v1;
    queue<int> q;
    q.push(v);
    while(!q.empty()){
    v=q.front(); q.pop();
if(flag==0){
for(v1=1;v1<=gra->Nv;v1++){
    if(point(gra,v1)&&distance(gra->G[0],gra->G[v1])-7.5<=gra->jump&&path[v1]==-1)
    { q.push(v1); dist[v1]=dist[v]+1; path[v1]=v;}
}}
    flag++;
    if(flag!=0){
    for(v1=1;v1<=gra->Nv;v1++)
    if(point(gra,v1)&&distance(gra->G[v],gra->G[v1])<=gra->jump&&path[v1]==-1)
{ q.push(v1); dist[v1]=dist[v]+1; path[v1]=v;}}
}
    int minstep=10000,laststep,v2;
    stack<int> s;
    for(v=1;v<=gra->Nv;v++){
    if(saved(gra,v)){
    if(dist[v]<minstep){
    minstep=dist[v];
    laststep=v;
}else if(dist[v]==minstep){
v2=v;
while(path[v2]!=0)
v2=path[v2];
    v1=laststep;
while(path[v1]!=0)
    v1=path[v1];
laststep=distance(gra->G[0],gra->G[v2])<distance(gra->G[0],gra->G[v1])?v:laststep;
}}}
if(minstep!=10000){
v=laststep; minstep++;
cout<<minstep<<endl;
while(path[v]!=-1){
s.push(v);
v=path[v];
}
while(!s.empty()){
v=s.top();
cout<<gra->G[v].x<<" "<<gra->G[v].y<<endl;
s.pop();
}
}else 
cout<<0<<endl;
}
int main(){
Graph gra=BuildGraph();
save(gra);
return 0;
}
原创粉丝点击