07-图4. Saving James Bond - Hard Version (30)

本题测试点5是从小岛范围内可以直接跳到岸边……

测试点4是验证步数第一跳最小的情况,刚开始没有考虑回溯,所以错了……

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int inf=1<<30;
struct node
{
    double x,y;
} a[100+5];
int n,vis[100+5],b[100+5],ans[100+5],cnt;
double d,ansfirst,nowfirst;
int dis(node d1,node d2)
{
    if(d*d<(d1.x-d2.x)*(d1.x-d2.x)+(d1.y-d2.y)*(d1.y-d2.y)) return 0;
    return 1;
}
int first(int d1)
{
    if(sqrt(a[d1].x*a[d1].x+a[d1].y*a[d1].y)>d+7.5) return 0;
    else return 1;
}

double first1(int d1)
{
    return sqrt(a[d1].x*a[d1].x+a[d1].y*a[d1].y)-7.5;
}
int safe(node d1)
{
    if(d1.x>=50-d) return 1;
    if(d1.y>=50-d) return 1;
    if(d1.x<=-50+d) return 1;
    if(d1.y<=-50+d) return 1;
    return 0;
}
void dfs(int d1,int now)
{
    int i;
    if(safe(a[d1]))
    {
        //printf("%d %.2f\n",now,nowfirst);
        if(now<cnt)
        {
            for(i=0; i<now; i++)
                ans[i]=b[i];
            cnt=now;
            ansfirst=nowfirst;
        }
        else if(now==cnt&&ansfirst>nowfirst)
        {
            for(i=0; i<now; i++)
                ans[i]=b[i];
            cnt=now;
            ansfirst=nowfirst;
        }
        return ;
    }
    else
    {
        for(i=1; i<=n; i++)
        {
            if(!vis[i]&&dis(a[d1],a[i]))
            {
                vis[i]=1;
                b[now]=i;
                dfs(i,now+1);
                vis[i]=0;
            }
        }
    }
    return;
}
int main()
{
    int i;
    while(~scanf("%d%lf",&n,&d))
    {
        a[0].x=a[0].y=0;
        for(i=1; i<=n; i++)
        {
            scanf("%lf%lf",&a[i].x,&a[i].y);
        }
        if(d+7.5>=50)
        {
            printf("1\n");
            return 0;

        }
        ansfirst=(double)inf;
        cnt=inf;
        memset(ans,0,sizeof(ans));
        for(i=1; i<=n; i++)
        {
            memset(vis,0,sizeof(vis));
            if(!vis[i]&&first(i))
            {
                nowfirst=first1(i);
                if(safe(a[i]))
                {
                    if(ansfirst>nowfirst)
                    {
                        ans[0]=i;
                        cnt=1;
                        ansfirst=nowfirst;
                    }
                }
                vis[i]=1;
                memset(b,0,sizeof(b));
                b[0]=i;
                dfs(i,1);
            }
        }
        if(cnt==inf) printf("0\n");
        else
        {
            printf("%d\n",cnt+1);
            for(i=0; i<cnt; i++)
            {
                printf("%.0f %.0f\n",a[ans[i]].x,a[ans[i]].y);
            }
        }
    }
    return 0;
}
时间: 2024-10-28 15:17:46

07-图4. Saving James Bond - Hard Version (30)的相关文章

PTA 07-图5 Saving James Bond - Hard Version (30分)

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 lan

7-11 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 crocodile

06-图4. Saving James Bond - Hard Version (30)

该题的关键是题干最后一句:如果有不同的路线都是最短路,那么输出第一跳最短的路线.解决方法是把湖心小岛周围一圈的节点(即距离湖心小岛小于D的鳄鱼)看作起点,对这些起点依次调用无权最短路算法,找出路径最短(且相同)的那些起点,然后比较从湖心小岛到它们的距离,输出最小的距离所在的路径即可. #include <iostream> #include <vector> #include <queue> #include <stack> #include <cma

05-2. Saving James Bond - Easy Version (25)

05-2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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

PAT Saving James Bond - Easy Version

Saving James Bond - Easy Version 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

05-图2. Saving James Bond - Easy Version (25)

05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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 capture

pat05-图2. Saving James Bond - Easy Version (25)

05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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 capture

Saving James Bond - Hard Version(PAT)

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

Saving James Bond - Easy Version (MOOC)

06-图2 Saving James Bond - Easy Version (25 分) 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