-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path07-图5 Saving James Bond - Hard Version.c
165 lines (144 loc) · 3.49 KB
/
07-图5 Saving James Bond - Hard Version.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* 队列定义开始 */
#define MaxSize 101
#define ERROR -1
typedef int Position;
struct QNode {
int *Data; /* 存储元素的数组 */
Position Front, Rear; /* 队列的头、尾指针 */
};
typedef struct QNode *Queue;
Queue CreateQueue()
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (int *)malloc(MaxSize * sizeof(int));
Q->Front = Q->Rear = 0;
return Q;
}
void DestoryQueue( Queue Q )
{
if (Q->Data) free(Q->Data);
free(Q);
}
int IsFull( Queue Q )
{
return ((Q->Rear+1)%MaxSize == Q->Front);
}
void Enqueue( Queue Q, int X )
{
if ( IsFull(Q) ) return;
else {
Q->Rear = (Q->Rear+1)%MaxSize;
Q->Data[Q->Rear] = X;
}
}
int IsEmpty( Queue Q )
{
return (Q->Front == Q->Rear);
}
int Dequeue( Queue Q )
{
if ( IsEmpty(Q) ) return ERROR;
else {
Q->Front =(Q->Front+1)%MaxSize;
return Q->Data[Q->Front];
}
}
/* 队列定义结束 */
#define MaxVertexNum 100
#define INF 65535
#define ISLAND_DIAMETER 15/2.0
typedef struct Position Point;
struct Position {
int x;
int y;
};
Point points[MaxVertexNum];
int dist[MaxVertexNum]; // 路径长度
int path[MaxVertexNum]; // 下一个结点
void ReadPoints(int num);
int Distance(Point p1, Point p2); //求两点间距离
int Jump(Point p1, Point p2, int dis); // 判断能否跳跃过去
int isSafe(Point p, int dis); // 是否能跳上岸
void PrintPath(int ans);
void Save007(int num, int dis); // 其实就是一个BFS函数
int main()
{
int num, dis;
scanf("%d %d", &num, &dis);
ReadPoints(num);
Save007(num, dis);
return 0;
}
void ReadPoints(int num)
{
int i;
for (i = 0; i < num; ++i) {
scanf("%d %d", &(points[i].x), &(points[i].y));
}
}
int Distance(Point p1, Point p2)
{
return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
}
int Jump(Point p1, Point p2, int dis)
{
return Distance(p1, p2) <= dis * dis;
}
int isSafe(Point p, int dis)
{
return ((abs(p.x) + dis) >= 50 || (abs(p.y) + dis) >= 50);
}
void PrintPath(int ans)
{
printf("%d\n", dist[ans] + 1);
for (; ans != -1; ans = path[ans]) {
printf("%d %d\n", points[ans].x, points[ans].y);
}
}
void Save007(int num, int dis)
{
int i, cur_dis, pIndex, ans;
Queue Q;
Point center;
center.x = 0; center.y = 0;
if (isSafe(center, dis + ISLAND_DIAMETER)) { // 如果从岛上就能直接跳上岸
printf("1\n");
return;
}
for (i = 0; i < num; ++i) { // 初始化辅助数组
dist[i] = INF;
path[i] = -1;
}
Q = CreateQueue();
for (i = 0; i < num; ++i) {
if (isSafe(points[i], dis)) { // 将所有能上岸的入队
dist[i] = 1;
Enqueue(Q, i);
}
}
cur_dis = 1; ans = -1;
while (!IsEmpty(Q)) {
pIndex = Dequeue(Q);
if (dist[pIndex] == cur_dis + 1 && ans != -1) break; // 已经遍历完当前层
if (Jump(center, points[pIndex], dis + ISLAND_DIAMETER)) {
if (ans == -1 || Distance(center, points[pIndex]) < Distance(center, points[ans]))
ans = pIndex;
}
else {
for (i = 0; i < num; ++i) {
if (Jump(points[pIndex], points[i], dis) && (dist[pIndex] + 1 < dist[i])) {
dist[i] = dist[pIndex] + 1;
path[i] = pIndex;
Enqueue(Q, i);
}
}
}
cur_dis = dist[pIndex];
}
if (ans == -1) printf("0\n");
else PrintPath(ans);
DestoryQueue(Q);
}