ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

07-图5 Saving James Bond - Hard Version (30 分) 被虐到哭的一道题

2021-05-23 20:03:03  阅读:193  来源: 互联网

标签:10 虐到 12 Saving int James 30 crocodile data


题面:

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
    AC代码  
#include <iostream>
#include <queue>
#include <stack>

using namespace std;

typedef struct{
    int x,y;
    bool ifNearBank;
    bool ifConnected;
    bool ifVisited;
}crocodile;

crocodile data[120];
int n;
int d;
int times=0;
int minTimes=20000;
int path[120];
int minpath[120];
int minStartPointList[120];
int stNum=0;

int bPath[120];//记录上一个是谁
int bLong[120];//记录终点离原点的距离

bool connectable(int a, int b){
    int dis = (data[a].x - data[b].x)*(data[a].x - data[b].x) + (data[a].y - data[b].y)*(data[a].y - data[b].y);
    if(dis<=d*d) return true;
    else return false;
}

void copyTime(){
    for(int i=0; i<minTimes; i++)
        minpath[i] = path[i];
}

void dfs(int pos, bool &flag){
    data[pos].ifVisited = true;
    path[times] = pos;
    times++;

    if(data[pos].ifNearBank){
        flag = true;
        if(times<minTimes){
            minTimes = times;
            copyTime();
        }
    }
    for(int i=0; i<n; i++){
        if(!data[i].ifVisited && connectable(i, pos))
            dfs(i, flag);
    }
    times--;
}

void bfs(int pos, bool &flag){
    queue<int> q;
    q.push(pos);
    data[pos].ifVisited = true;
    if(data[pos].ifNearBank){
        flag = true;
        return;
    }
    while(!q.empty()){
        pos = q.front();
        q.pop();
        for(int i=0; i<n; i++){
            if(!data[i].ifVisited && connectable(i, pos)){
                data[i].ifVisited = true;
                bPath[i] = pos;
                if(data[i].ifNearBank){
                    flag = true;
                }
                q.push(i);
            }
        }
    }
}

void calHowLong(){
    for(int i=0; i<n; i++){
        if(data[i].ifNearBank&&data[i].ifVisited){
            int j=i;
            int k=1;
            while(bPath[j]!=j){
                j=bPath[j];
                k++;
            }
            bLong[i] = k;
            if(minTimes>k) minTimes=k;
        }
    }
}

int calStartPoint(int p){
    while(bPath[p]!=p){
        p=bPath[p];
    }return p;
}

int cd(int i){
    return data[i].x*data[i].x+data[i].y*data[i].y;
}

int main()
{
    scanf("%d%d", &n, &d);

    if(d>=42.5){
        printf("1\n");
        return 0;
    }

    for(int i=0; i<n; i++){
        scanf("%d%d", &data[i].x, &data[i].y);
        if(cd(i)<=56.25||data[i].x>=50||data[i].x<=-50||data[i].y>=50||data[i].y<=-50){
            n--;
            i--;
            continue;
        }
        if(cd(i)<=(7.5+d)*(7.5+d)){
            data[i].ifConnected = true;
            bPath[i]=i;
            minStartPointList[stNum] = i;
            for(int j=stNum; j>0; j--){
                if(cd(minStartPointList[j]) < cd(minStartPointList[j-1])){
                    int temp;
                    temp = minStartPointList[j]; minStartPointList[j] = minStartPointList[j-1]; minStartPointList[j-1] = temp;
                }
            }
            stNum++;
        }
        else
            data[i].ifConnected = false;
        if(data[i].x+d>=50||data[i].x-d<=-50||data[i].y+d>=50||data[i].y-d<=-50){
            data[i].ifNearBank = true;
            bLong[i]=20000;
        }
        else
            data[i].ifNearBank = false;
        data[i].ifVisited = false;
    }
    bool ifEscape = false;
    for(int i=0; i<stNum; i++){
        times = 0;
        bfs(minStartPointList[i], ifEscape);
    }
    if(ifEscape){
//        calStartPoint();
        calHowLong();
        int finalPoint=-1;
        for(int i=0; i<n; i++){
            if(minTimes == bLong[i]){
                if(finalPoint>=0){
                    if(cd(calStartPoint(i))<cd(calStartPoint(finalPoint))){
                        finalPoint = i;
                    }
                }else finalPoint = i;
            }
        }
        stack<int> fuck;
        fuck.push(finalPoint);
        while(bPath[finalPoint]!=finalPoint){
            finalPoint=bPath[finalPoint];
            fuck.push(finalPoint);
        }
        printf("%d\n", minTimes+1);
        while(!fuck.empty()){
            printf("%d %d\n", data[fuck.top()].x, data[fuck.top()].y);
            fuck.pop();
        }

    }
    else printf("0\n");

    return 0;
}

 

 题解心得:代码写的很乱,暂时不想调整了,A了就行。   踩了的坑点: 1.之前的easy version用了dfs,但是这道题是不能用dfs的!!!dfs一条路是走到黑,有可能找不到那条最短的路径(可能把能走最短的路径绕远走掉,导致最短路径走不了)。 2.bfs记录路径,我的方法是记录每一个顶点的父顶点,最后通过可以一步到达岸边的那些顶点回溯,将回溯的过程入栈再出栈即为所求的路径。 3.暂时先写这么多。这道题写了我一个下午加半个晚上,现在脑壳疼痛,出去跑会步。。

标签:10,虐到,12,Saving,int,James,30,crocodile,data
来源: https://www.cnblogs.com/wstnl/p/14801967.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有