-
Notifications
You must be signed in to change notification settings - Fork 0
/
saving james bound.cpp
125 lines (119 loc) · 2.42 KB
/
saving james bound.cpp
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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MaxVertexNum 100
struct Coordinate
{
int x;
int y;
}Vertex[MaxVertexNum + 1];//CurrentPositionom 1~101
int VertexNum;
int JumpRange;
int Visited[MaxVertexNum + 1] = {};
int Path[MaxVertexNum + 1] = { -1 };
int DirectlyToBank(int a)
{
if (Vertex[a].x >= 50 - JumpRange || Vertex[a].y >= 50 - JumpRange || Vertex[a].x <= -50 + JumpRange || Vertex[a].y <= -50 + JumpRange)return 1;
else return 0;
}
int InRange(int a, int b)
{
if (pow(Vertex[a].x - Vertex[b].x, 2) + pow(Vertex[a].y - Vertex[b].y, 2) <= pow(JumpRange, 2))return 1;
return 0;
}
int FirstJump(int a)
{
if (pow(Vertex[a].x, 2) + pow(Vertex[a].y, 2) <= pow(JumpRange + 7.5, 2))return pow(Vertex[a].x, 2) + pow(Vertex[a].y, 2);
return 0;
}
void Jump()
{
int step = 2;
int firststep[MaxVertexNum + 1];
int queue[MaxVertexNum + 1] = { -1 };
int Front = 0;
int Rear = 0;
int stack[MaxVertexNum + 1];
int Top = 0;
int i;
int MinFS = 10E8;
for (i = 0; i < VertexNum; i++)
{
firststep[i] = FirstJump(i);
}
int Last;
int min = 0;
while (1)
{
for (i = 0; i < VertexNum; i++)
{
if (MinFS > firststep[i] && !Visited[i] && firststep[i])
{
MinFS = firststep[i];
min = i;
}
}
if (MinFS == 10E8)break;
Visited[min] = 1;
MinFS = 10E8;
queue[Rear] = min;
Last = Rear++;
}
for (int i = 0; i < MaxVertexNum; i++)
{
Visited[i] = 0;
}
int Tail = 0;
while (Front != Rear)
{
int CurrentPosition = queue[Front++];
if (DirectlyToBank(CurrentPosition))
{
printf("%d\n", step + 1);
for (int i = 0; i < step; i++)
{
stack[Top++] = CurrentPosition;
CurrentPosition = Path[CurrentPosition];
}
for (int i = Top - 1; i >= 0; i--)
{
printf("%d %d\n", Vertex[stack[i]].x, Vertex[stack[i]].y);
}
Top = 0;
return;
}
for (int i = 0; i < VertexNum; i++)
{
if (!Visited[i] && InRange(CurrentPosition, i))
{
queue[Rear++] = i;
Path[i] = CurrentPosition;
Visited[i] = 1;
Tail = i;
}
}
if (Last == CurrentPosition)
{
step++;
Last = Tail;
}
}
printf("0\n");
return;
}
int main()
{
scanf("%d %d", &VertexNum, &JumpRange);
for (int i = 1; i < VertexNum; i++)
{
scanf("%d %d", &Vertex[i].x, &Vertex[i].y);
}
if (JumpRange > 42.5)
{
printf("1\n");
return 0;
}
Jump();
system("pause");
return 0;
}