基础实验6-2.3-拯救007-编程题
程序员文章站
2022-06-07 20:48:23
...
解题代码
#include<stdio.h>
#include<math.h>
#define MAXN 100
#define DIA 15
#define SIDE 100
typedef enum { false, true } bool;
bool CanJumpToSide(int index);
bool FirstJump(int index);
bool CanJump(int index1, int index2);
bool DFS(int index);
struct Data {
int x, y;
}A[MAXN];
int B[MAXN], N, D;
int main()
{
int i, flag = 0;
scanf("%d %d", &N, &D);
for (i = 0; i < N; i++) scanf("%d %d", &A[i].x, &A[i].y);
if (CanJumpToSide(-1)) flag = 1;
else {
for (i = 0; i < N; i++) {
if (!B[i] && FirstJump(i)) {
B[i] = 1;
if (DFS(i)) flag = 1;
}
}
}
if (flag) printf("Yes");
else printf("No");
return 0;
}
bool CanJumpToSide(int index) {
if (index == -1) {
if ((double)DIA / 2 + D >=(double) SIDE / 2) return true;
else return false;
}
else {
int x = A[index].x;
int y = A[index].y;
if ((x >= SIDE / 2 - D )||( x <= D - SIDE / 2 )||( y >= SIDE / 2 - D )||( y <= D - SIDE / 2)) return true;
else return false;
}
}
bool FirstJump(int index) {
double distance = sqrt(pow(A[index].x, 2) + pow(A[index].y, 2));
if (distance <= (double)DIA / 2 + D) return true;
else return false;
}
bool CanJump(int index1, int index2) {
int x1 = A[index1].x, y1 = A[index1].y;
int x2 = A[index2].x, y2 = A[index2].y;
double distance = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
if (distance <= D) return true;
else return false;
}
bool DFS(int index) {
if (CanJumpToSide(index)) return true;
for (int i = 0; i < N; i++) {
if (!B[i] && CanJump(i, index)) {
B[i] = 1;
if (DFS(i)) return true;
}
}
return false;
}
测试结果
问题整理
1.还是DFS的return的问题阻碍了输出,在递归函数中最重要的便是return的定义和循环终止条件。
上一篇: 二叉树的前序遍历(非递归)
下一篇: php阻止页面后退的方法分享