zoukankan      html  css  js  c++  java
  • 07-图5 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 (≤), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the ( 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 ( 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
    //参考 
    #include<cstdio>
    #include<iostream>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    const int maxn = 101;
    const int minLen = 50 - 15/2;
    struct Pointer{
        int x,y;
    }point[maxn];
    int path[maxn] = {-1};
    bool visited[maxn] = {false};
    int n,m;
    
    
    bool Jump(int u,int v){
        int d1 = pow(point[u].x - point[v].x,2);
        int d2 = pow(point[u].y - point[v].y,2);
        int r = m * m;
        if(r >= d1 + d2) return true;
        else return false;
    }
    
    int firstJump(int v){
        int d1 = pow(point[v].x,2);
        int d2 = pow(point[v].y,2);
        int r = (m+7.5)*(m+7.5);
        if(r >= d1 + d2) return d1+d2;
        else return 0;
    }
    
    bool isSafe(int x) { /* 判断从当前点能否跳到岸上 */
        if ((point[x].x - m <= -50) || (point[x].x + m >= 50) || (point[x].y - m <= -50) || (point[x].y + m >= 50))
            return true;
        return false;
    }
    
    bool cmp(int x,int y){
        return firstJump(x) < firstJump(y);
    }
    
    void BFS() { /* 用bfs来判断最少要踩几个小鳄鱼才能上岸 */
        int b[101];
        queue<int>q; 
        /* 将第一步能踩到的鳄鱼按距离从小到大的顺序进队列~ 因为输出结果要保证在踩的鳄鱼数量相等的情况下 输出第一步距离最短的~~*/
        for (int i = 0; i < n; i++) {
            b[i] = i;
        }
        sort(b, b + n, cmp); /* 按照第一步的距离排序~~~ */
        int last;
        for (int i = 0; i < n; i++) {
            if (firstJump(b[i])) { /* 能跳上去! */
                q.push(b[i]);
                visited[b[i]] = true; /* 指向当前层数最后一个数~ */
                last = b[i];
            }
        }
        int step = 2;  /* 记录最少要跳跃的次数 */
        int tail; 
        while (!q.empty()) {
            int p = q.front();
            q.pop();
            if (isSafe(p)) {
                int k = 1;
                stack<int> s;
                cout << step << endl;
                while (k < step) {
                    //cout << point[p].x << " " << point[p].y << endl;
                    s.push(p);
                    p = path[p];
                    k++;
                }
                while (!s.empty()) {
                    p = s.top();
                    s.pop();
                    cout << point[p].x << " " << point[p].y << endl;
                }
                return;
            }
            for (int i = 0; i < n; i++) {
                if (!visited[i] && Jump(p, i)) { /* 没踩过并且能跳到 */
                    q.push(i);
                    path[i] = p; /* 记得当前进队节点的父节点~ */
                    visited[i] = true;
                    tail = i; /* 指向下一层的最后一个元素 */
                }
            }
            if (last == p) { /* 即将进入下一层~ */
                step += 1;
                last = tail;
            }
        }
        if (q.empty()) { /* 如果队列为空  说明没跳出去啊~ */
            cout << "0" << endl;
        }
    }
    
    int main(){
        scanf("%d%d",&n,&m);    
        for(int i = 0; i < n; i++){
            scanf("%d%d",&point[i].x,&point[i].y);
        }
        if(m >= minLen){
            printf("1
    ");
            return 0;
        }
        BFS();
        return 0;
    }

    参考上面代码,自己调试

    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<stack>
    using namespace std;
    
    const int maxn = 110;
    const int INF = 1000000000;
    const double ISLAND_RADIUS = 15.0/2;
    const double SQUARE_SIZE = 100.0/2;
    
    struct Point{
        int x,y;
    }p[maxn];
    
    int n,d;
    int d1[maxn];
    int path[maxn] = {0};
    bool vis[maxn] = {0};
    
    void init();
    bool cmp(int a,int b);
    int FirstJump(int v);
    void BFS();
    bool isSave(int v);
    bool Jump(int v1,int v2);
    
    int main(){
        //init();
        scanf("%d%d",&n,&d);
        for(int i = 0; i < n; i++){
            scanf("%d%d",&p[i].x,&p[i].y);
            //if(FirstJump(i)) printf("%d
    ",i);
        } 
        if(d >= SQUARE_SIZE){
            printf("1
    ");
            return 0;
        }
        BFS();
    }
    
    void BFS(){
        int dis[maxn];
        queue<int> q; 
        for(int i = 0; i < n; i++){
            dis[i] = i;
        }
        sort(dis,dis+n,cmp);
        
    //    for(int i = 0;  i < n; i++){
    //        printf("%d %d %d 
    ",dis[i],p[dis[i]].x,p[dis[i]].y);
    //    } 
    
        int last; //point the last point in the current level
        for(int i = 0; i < n; i++){
            if(FirstJump(dis[i])){
                //printf("%d %d %d
    ",dis[i],p[dis[i]].x,p[dis[i]].y);
                q.push(dis[i]);
                vis[dis[i]] = true;
                last = dis[i];
            }
        }
        int step = 2;
        int tail;
         while(!q.empty()){
             int now = q.front();
             q.pop();
             if(isSave(now)){
                 int k = 1;
                 stack<int> s;
                 printf("%d
    ",step);
                 while(k < step){
                     //printf("%d ",now);
                     s.push(now);
                     now = path[now];
                     k++;
                 }
                 while(!s.empty()){
                     now = s.top();
                     s.pop();
                     printf("%d %d
    ",p[now].x,p[now].y);
                 }
                 return;
            }
              for(int i = 0; i < n; i++){
                  if(!vis[i] && Jump(now,i)){
                      //printf("%d 
    ",i);
                      q.push(i);
                      vis[i] = true;
                      path[i] = now;
                      tail = i;
                      //printf("
    %d ",i);
                }
             }
             if(last == now){
                 step += 1;
                 last = tail;
            }
        }
        if(q.empty()){
            printf("0
    ");
        }
    }
    
    bool cmp(int u,int v){
        //return FirstJump(d1[a]) < FirstJump(d1[b]); 
        return FirstJump(u) < FirstJump(v);
        //double d1 = pow(p[v].x,2) + pow(p[v].x,2);
        //double d2 = pow(p[u].y,2) + pow(p[v].y,2);
        //return d1 < d2;
    }
    
    int FirstJump(int v){
        int d1 = pow(p[v].x,2);
        int d2 = pow(p[v].y,2);
        int r = pow(d+ISLAND_RADIUS,2); //the most distance James can jump
        if(d1 + d2 <= r){
            return d1+d2;
        }else{
             return 0;
        }
    }
    
    bool isSave(int v){
         if((abs(p[v].x) + d >= SQUARE_SIZE) ||(abs(p[v].y) + d >= SQUARE_SIZE)) return true;
         else return false;
    }
    
    bool Jump(int u,int v){
        int d1 = pow(p[u].x - p[v].x,2);
        int d2 = pow(p[u].y - p[v].y,2);
        int r = d * d;
        if(d1 + d2 <= r) return true;
        else return false;
    }
    
    void init(){
        for(int i = 0; i < n; i++){
            d1[i] = i;
        }
    }
  • 相关阅读:
    调用其他类的方法
    CString中 format、trimLeft和trimright、trim 和FindOneOf用法
    GetAsyncKeyState()& 0x8000
    C++打开剪切板,获取剪切板数据
    CString比较不区分大小写
    C++ string中find() 用法
    CString数组和CStringArray
    nested exception is java.io.FileNotFoundException: Could not open ServletContext resource
    SQLPlus获取oracle表操作SQL
    XShell实现服务器端文件的上传下载
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/10831731.html
Copyright © 2011-2022 走看看