拯救007(DFS)

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 whether or not he can escape.

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, print in a line “Yes” if James can escape, or “No” if not.

Sample Input 1:
14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12
Sample Output 1:
Yes
Sample Input 2:
4 13
-12 12
12 12
-12 -12
12 -12
Sample Output 2:
No

这一次让我们考虑一下电影“生活和让死”中的情况,其中詹姆斯邦德是世界上最着名的间谍,被一群毒贩抓获。他被送到一个充满鳄鱼的湖中心的一小块土地上。他在那里表演了最大胆的逃跑行动 - 他跳到最近的鳄鱼头上!在动物意识到发生了什么事之前,詹姆斯再次跳到下一个大头上…最后他到达了银行,最后一条鳄鱼咬了他(实际上这个特技演员被大嘴抓住,几乎没有用他的特厚靴子逃脱)。

假设湖面是100乘100平方。假设湖的中心位于(0,0),东北角位于(50,50)。中央岛是一个以(0,0)为中心的圆盘,直径为15.许多鳄鱼都在湖中的不同位置。考虑到每只鳄鱼的坐标和詹姆斯可以跳跃的距离,你必须告诉他是否可以逃脱。

输入规格
每个输入文件包含一个测试用例。每个案例都以包含两个正整数N(≤100),鳄鱼数量和D,詹姆斯可以跳跃的最大距离的行开头。然后是N行,每行包含鳄鱼的(x,y)位置。请注意,没有两条鳄鱼停留在同一位置。

输出规格:
对于每个测试用例,如果詹姆斯可以逃脱,则在“Yes”行中打印,否则打印为“No”。

样本输入1:
14 20
25-15
-25 28
8 49
29 15
-35 -2
5 28
27-29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12
样本输出1:
Yes
样本输入2:
4 13
-12 12
12 12
-12 -12
12-12
样本输出2:
No

题解:
1、用结构体数组存储坐标即可;
2、由于小岛有半径7.5,所以第一次跳跃要特殊考虑,用到了firstJump()函数,接下来dfs;
3、搜索下一个点时先判断是不是可以跳到岸上,可以跳上去则直接return 1,否则dfs后面没有被访问过的点,若dfs的结果可以到岸上,则直接break;
4、dfs时需要判断是否访问过并且可以从当前点跳到下一个点,;
5、两点间的距离:d >= sqrt((x1-x2)(x1-x2)+(y1-y2)(y1-y2)),即dd >= (x1-x2)(x1-x2)+(y1-y2)*(y1-y2)。

参考代码:

#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1010;
int d, n;
bool vis[N];
struct Pos
{
    int X;
    int Y;
}pos[N];

int firstJump(int a)
{
    int x = pos[a].X;
    int y = pos[a].Y;
    if((d + 7.5)*(d + 7.5) >= (x*x + y*y)) return 1;
    return 0;
}

int isSafe(int a)
{
    int x = abs(pos[a].X);
    int y = abs(pos[a].Y);
    //若步长大于离岸边的距离,则安全
    if(d >= 50 - x || d >= 50 - y) return 1;
    return 0;
}

int Jump(int a,int b)
{
    int x1 = pos[a].X, y1 = pos[a].Y;
    int x2 = pos[b].X, y2 = pos[b].Y;
    //若步长大于两点间距离,则能跳过去
    if(d*d >= (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) return 1;
    return 0;
}

int dfs(int a)
{
    int res = 0;
    vis[a] = true;

    if(isSafe(a)) return 1;

    for(int i = 0; i < n; i++)
    {
        //若没有被访问并且可以跳到下一个点
        if(!vis[i] && Jump(a, i)) res = dfs(i);
        //若可以提前跳到岸边则break;
        if(res) break;
    }
    return res;
}

void save007()
{
    int res = 0;
    //找第一个跳点
    for(int i = 0; i < n; i++)
    {
        if(firstJump(i)) res = dfs(i);
        //可以走到岸边,break
        if(res) break;
    }
    if(res) cout << "Yes" <<endl;
    else cout << "No" <<endl;
}

int main()
{
    cin >> n >> d;
    for(int i = 0; i < n; i++)
    {
        cin >> pos[i].X >> pos[i].Y;
    }

    save007();

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值