PAT : Data Structures and Algorithms (English)7-10 Saving James Bond - Easy Version

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/belous_zxy/article/details/87937516
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
const auto MaxIndex = 100;
struct pa
{
    int x, y;
};
pa Q[MaxIndex];
bool book[MaxIndex];
int d, cnt;
bool checkdistance(int x, int y, int R)
{
    if (x * x + y * y <= R * R)
        return true;
    return false;
}
bool DFS(int k)
{
    book[k] = true;
    if ((Q[k].x >= 50 - d) || (Q[k].x <= -50 + d) || (Q[k].y >= 50 - d) || (Q[k].y <= -50 + d))
        return true;
    for (int i = 0; i < cnt; ++i)
    {
        if (book[i])
            continue;
        if (checkdistance(Q[i].x - Q[k].x, Q[i].y - Q[k].y, d))
        {
            if (DFS(i))
                return true;
        }
    }
    return false;
}
int getdigit(void)
{
    int temp{0}, symbol{1};
    char ch;
    while (1)
    {
        ch = getchar();
        if (ch == ' ' || ch == '\n' || ch == EOF)
            return temp * symbol;
        if (ch == '-')
            symbol = -1;
        else
            temp = temp * 10 + ch - '0';
    }
}
int main(int argc, char *argv[])
{
    cnt = getdigit();
    d = getdigit();
    if (d > 21)
    {
        printf("Yes\n");
        return EXIT_SUCCESS;
    }
    for (int i = 0; i < cnt; ++i)
        Q[i] = {getdigit(), getdigit()};
    for (int i = 0; i < cnt; ++i)
        if (checkdistance(Q[i].x, Q[i].y, 15 + d))
            if (DFS(i))
            {
                printf("Yes\n");
                return EXIT_SUCCESS;
            }
    printf("No\n");
    return EXIT_SUCCESS;
}

结构体数组存储,DFS遍历即可~

END

猜你喜欢

转载自blog.csdn.net/belous_zxy/article/details/87937516