1. 程式人生 > >Saving James Bond

Saving James Bond

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

P1:

#include<iostream>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
#define MinLen 42.5
struct Point {
    int x;
    int y;
    bool visited;
};
Point O;
int N, D;
vector<Point> s;
int path[100]={-1};
double Distance(Point a, Point b)
{
    return sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2));
}
bool success(Point t)
{
    if ((50 - abs(t.x)) <= D || (50 - abs(t.y)) <= D) 
        return true;                
    return false;
}
int calDist(Point a,Point b){   //計算兩點之間的距離
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int main()
{
    int i;
    Point p;
    queue <int> q;
    cin >> N >> D;
    if (D >= MinLen) {
        cout << '1' << endl;
        return 0;
    }
    for (i = 0; i < N; i++) {
        cin >> p.x >> p.y;
        p.visited = false;
        s.push_back(p);
    }
    int last=0;
    for (i = 0; i < N; i++) {
        if (Distance(s[i], O) <= (D + 7.5))     //簡單數學思維,或者說尋找第一步可上crocodile
            {
                q.push(i);
                s[i].visited=true;
                last=i;
            }
    }
    int step=2;
    int tail;
    while(!q.empty()){
            int element=q.front();
            q.pop();
            if(success(s[element])){
                int k=1;
                stack <int> crocodial;
                printf("%d\n",step);
                while(k<step){
                    k++;
                    crocodial.push(element);   //從最後一條鱷魚到第一條魚開始壓棧
                    element=path[element];   //上一條鱷魚
                }
                while(!crocodial.empty()){
                    element=crocodial.top();     //出棧
                    crocodial.pop();
                    printf("%d %d\n",s[element].x, s[element].y);
                }
                return 0;   //直接跳出
            }
            for(int i=0;i<N;i++){
                if((!s[i].visited)&&calDist(s[element],s[i])<=D*D){
                    q.push(i);
                    path[i]=element;
                    s[i].visited=true;
                    tail=i;
                }
            }
            if(last==element){
                step++;
                last=tail;
            }
        }
        if (q.empty()) {
        cout << '0' << endl;
        return 0;
    }
    return 0;
}