PAT : Data Structures and Algorithms (English)7-10 Saving James Bond - Easy Version
#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遍历即可~