目录
题目:输入格式输出格式输入样例输出样例
算法BFS代码实现ListComponent:把第一次可以跳上的结点,分别视为一个尝试集合Read函数:读入数据同时剔除无用数据ShortUnWeight (BFS)函数其他函数:
Dijkstra代码实现Save007函数:把第一次可以跳上的结点,分别视为一个**尝试集合**ShortestPath:Dijkstra算法处理dist和path,返回以K为离岸结点的第一个上岸结点其他函数:
习题讲解视频
题目:
This time let us consider the situation in the movie “Live and Let Die” in which James Bond, the world’s most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape – he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head… Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot). Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him a shortest path to reach one of the banks. The length of a path is the number of jumps that James has to make.
输入格式
Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the (x,y) location of a crocodile. Note that no two crocodiles are staying at the same position.
输出格式
For each test case, if James can escape, output in one line the minimum number of jumps he must make. Then starting from the next line, output the position (x,y) of each crocodile on the path, each pair in one line, from the island to the bank. If it is impossible for James to escape that way, simply give him 0 as the number of jumps. If there are many shortest paths, just output the one with the minimum first jump, which is guaranteed to be unique.
输入样例
17 15
10 -21
10 21
-40 10
30 -50
20 40
35 10
0 -10
-25 22
40 -40
-30 30
-10 22
0 11
25 21
25 10
10 10
10 35
-30 10
输出样例
4
0 11
10 21
10 35
算法
BFS算法代码,需要将PTA编译器切换为C(clang 6.0.1); 否则容易报错,还没找到问题
BFS
把第一次可以跳上的结点,分别视为尝试集合在尝试集合内做BFS,成功上岸返回层数(Layer)和路径path,否则返回错误每次将返回的层数(Layer),与最小层数(MinLaye)比较;小于时,更新最小层数(MinLaye)和打印路径(PrintPath);等于时 Judge(PrintPath[0] , i ),比较之前最小首结点与当前首结点距离坐标(0,0)位置,小于时更新
代码实现
int main()
{
scanf("%d %lf",&N
,&D
);
Read(&N
);
if(D
+diameter
/2>50){
printf("1");
}else{
ListComponent(N
);
}
return 0;
}
ListComponent:把第一次可以跳上的结点,分别视为一个尝试集合
void ListComponent()
{
int i
,j
;
int PrintPath
[MAXVERTEXNUM
];
int path
[MAXVERTEXNUM
];
int Layer
=-1,MinLayer
=MAXVERTEXNUM
-1;
for(i
=0;i
<N
;i
++){
if(JumpFirst(i
)){
Layer
=ShortUnWeight(i
,path
);
if(MinLayer
>Layer
||(MinLayer
==Layer
&&Judge(PrintPath
[0],i
))){
MinLayer
=Layer
;
if(MinLayer
==0)PrintPath
[0]=i
;
for(j
=0;j
<=MinLayer
;j
++){
PrintPath
[j
]=path
[j
];
}
}
}
}
if(MinLayer
==MAXVERTEXNUM
-1){
printf("0");
}else{
if(MinLayer
==0){
printf("2\n");
int t
=PrintPath
[0];
printf("%d %d",Graph
[t
][0],Graph
[t
][1]);
}else{
printf("%d\n",MinLayer
+2);
for(i
=0;i
<=MinLayer
;i
++){
int t
=PrintPath
[i
];
printf("%d %d",Graph
[t
][0],Graph
[t
][1]);
if(i
!=MinLayer
){
printf("\n");
}
}
}
}
}
Read函数:读入数据同时剔除无用数据
void Read()
{
int i
;
int x
,y
;
double d
;
double dt
=pow(diameter
/2,2);
int count
=0;
for(i
=0;i
<N
;i
++){
scanf("%d %d",&x
,&y
);
d
=pow(x
,2)+pow(y
,2);
if(d
>dt
){
if((x
>-50&&x
<50)&&(y
>-50&&y
<50)){
Graph
[count
][0]=x
;
Graph
[count
++][1]=y
;
}
}
}
N
=count
;
}
ShortUnWeight (BFS)函数
int ShortUnWeight(int v
,int path
[])
{
int i
;
for(i
=0;i
<N
;i
++){
path
[i
]=-1;
}
if(IsSafe(v
)){
return 0;
}
int dist
[MAXVERTEXNUM
];
int Queue
[MAXVERTEXNUM
];
int rear
=-1,head
=-1;
for(i
=0;i
<N
;i
++){
dist
[i
]=-1;
}
dist
[v
]=0;
Queue
[rear
++]=v
;
int w
=-1;
int flag
=0;
int s
=-1;
while(rear
>head
){
int t
=Queue
[head
++];
for(w
=0;w
<N
;w
++){
if(dist
[w
]==-1&&Jump(t
,w
)){
dist
[w
]=dist
[t
]+1;
path
[w
]=t
;
Queue
[rear
++]=w
;
if(IsSafe(w
)){
flag
=1;
s
=w
;
break;
}
}
}
if(flag
==1)break;
}
if(flag
==1){
int Layer
=dist
[s
];
int stack
[MAXVERTEXNUM
];
int top
=-1;
do{
stack
[++top
]=s
;
s
=path
[s
];
}while(s
!=-1);
int count
=0;
while(top
>-1){
path
[count
++]=stack
[top
--];
}
return Layer
;
}else{
return MAXVERTEXNUM
;
}
}
其他函数:
JumpFirst(第一跳检查)IsSafe(当前节点是否可以上岸)Jump(int V,int W)是否可以从V跳到WJudge(int a,int b)检查两个首结点哪一个距离岸边更近
#define MAXVERTEXNUM 200
#define diameter 15
#define distination 50
double D
=0;
int N
=0;
int Graph
[MAXVERTEXNUM
][2];
int JumpFirst(int i
)
{
double x
=Graph
[i
][0];
double y
=Graph
[i
][1];
double distance
=pow(x
,2.0)+pow(y
,2.0);
if(distance
<=pow(diameter
/2.0+D
,2.0)){
return 1;
}else{
return 0;
}
}
int IsSafe(int V
)
{
double x
=Graph
[V
][0];
double y
=Graph
[V
][1];
x
=(x
<0?-x
:x
);
y
=(y
<0?-y
:y
);
if(x
+D
>=distination
||y
+D
>=distination
){
return 1;
}else{
return 0;
}
}
int Jump(int V
,int W
)
{
double x1
=Graph
[V
][0];
double y1
=Graph
[V
][1];
double x2
=Graph
[W
][0];
double y2
=Graph
[W
][1];
double distance
=pow(x1
-x2
,2.0)+pow(y1
-y2
,2.0);
if(distance
<=pow(D
,2.0)){
return 1;
}else{
return 0;
}
}
int Judge(int a
,int b
)
{
double x1
=Graph
[a
][0];
double y1
=Graph
[a
][1];
double x2
=Graph
[b
][0];
double y2
=Graph
[b
][1];
double d1
=pow(x1
,2)+pow(y1
,2);
double d2
=pow(x2
,2)+pow(y2
,2);
if(d2
<d1
){
return 1;
}else{
return 0;
}
}
Dijkstra
把第一次可以跳上的结点,分别视为一个尝试集合,返回第一个上岸结点answer;记录返回的最小上岸结点对应的minDist;根据minDist和path[answer]输出
path和dist在尝试集合中共用。当遇到重合的结点时,对比dist值,如果可以更小则更新;如果相等则利用Source(int a,int b,int path[])函数回溯当前结点的离岸首结点,对比首结点离岸距离,再确定是否更新。
代码实现
int main()
{
scanf("%d %d",&N
,&D
);
int dist
[N
];
int path
[N
];
Initialize(dist
,path
);
Save007(dist
,path
);
return 0;
}
Save007函数:把第一次可以跳上的结点,分别视为一个尝试集合
void Save007(int dist
[],int path
[])
{
int i
=0;
int answer
=-1;
int minAnswer
=-1;
int minDist
=Infinite
;
double distance
=Radius
+D
;
if(distance
>=50.0){
printf("1");
}else{
for(i
=0;i
<N
;i
++){
if(FirstJump(G
,i
,D
)){
answer
=ShortestPath(i
,dist
,path
);
if(minDist
==dist
[answer
]){
if(Source(minAnswer
,answer
,path
)){
minAnswer
=answer
;
minDist
=dist
[answer
];
}else{
continue;
}
}else{
minAnswer
=answer
;
minDist
=dist
[answer
];
}
}
}
if(minAnswer
!=-1){
printf("%d\n",dist
[minAnswer
]+1);
int stack
[100];
int top
=0;
stack
[top
++]=minAnswer
;
while(path
[minAnswer
]!=-1){
stack
[top
++]=path
[minAnswer
];
minAnswer
=path
[minAnswer
];
}
while(top
>0){
int i
=stack
[--top
];
printf("%d %d\n",G
[i
].x
,G
[i
].y
);
}
}else{
printf("0");
}
}
}
ShortestPath:Dijkstra算法处理dist和path,返回以K为离岸结点的第一个上岸结点
int ShortestPath(int k
,int dist
[],int path
[]){
dist
[k
]=1;
path
[k
]=-1;
int answer
=-1;
if(Issafe(k
)){
answer
= k
;
}else{
int Q
[N
];
int head
=0;
int rear
=0;
Q
[rear
++]=k
;
while(head
!=rear
){
int temp
=Q
[head
++];
int i
=0;
for(i
=0;i
<N
;i
++){
if(IsAround(temp
,i
)){
if(dist
[i
]>dist
[temp
]+1){
dist
[i
]=dist
[temp
]+1;
path
[i
]=temp
;
Q
[rear
++]=i
;
if(Issafe(i
)){
answer
= i
;
break;
}
}else if(dist
[i
]==dist
[temp
]+1&&Source(i
,temp
,path
)){
dist
[i
]=dist
[temp
]+1;
path
[i
]=temp
;
Q
[rear
++]=i
;
if(Issafe(i
)){
answer
= i
;
break;
}
}
}
}
if(answer
!=-1){
break;
}
}
}
return answer
;
}
其他函数:
Initialize:初始化bool FirstJump(int i) 第一跳是否能跳到 i 结点bool Issafe(int k) k 结点能否上岸bool IsAround(int k,int i) 从 k 结点是否能跳上 i 结点bool Source(int a,int b,int path[]) 通过path 找到 结点 a 和 结点b的离岸结点,如果结点b 的离岸结点更近则真,否则假
#define Infinite 65535
#define Radius 7.5
int N
=0,D
=0;
typedef struct{
int x
;
int y
;
}Node
;
Node G
[102];
void Initialize(int dist
[],int path
[])
{
int i
=0;
for(i
=0;i
<N
;i
++){
scanf("%d %d",&G
[i
].x
,&G
[i
].y
);
if(G
[i
].x
>=50||G
[i
].x
<=-50){
G
[i
].x
=Infinite
;
}
if(G
[i
].y
>=50||G
[i
].y
<=-50){
G
[i
].y
=Infinite
;
}
dist
[i
]=Infinite
;
path
[i
]=-1;
}
}
bool
FirstJump(int i
)
{
double distance
=Radius
+D
;
double Dis
=pow(G
[i
].x
,2)+pow(G
[i
].y
,2);
if(Dis
<=pow(distance
,2)&&Dis
>pow(Radius
,2)){
return true
;
}else{
return false
;
}
}
bool
Issafe(int k
)
{
int x
=G
[k
].x
;
int y
=G
[k
].y
;
if(x
<0){
x
=-x
;
}
if(y
<0){
y
=-y
;
}
if(x
+D
>=50||y
+D
>=50){
return true
;
}else{
return false
;
}
}
bool
IsAround(int k
,int i
)
{
if(k
==i
)return false
;
int kx
=G
[k
].x
;
int ky
=G
[k
].y
;
int ix
=G
[i
].x
;
int iy
=G
[i
].y
;
double distance
=pow(kx
-ix
,2)+pow(ky
-iy
,2);
if(distance
<=pow(D
,2)){
return true
;
}else{
return false
;
}
}
bool
Source(int a
,int b
,int path
[])
{
while(path
[a
]!=-1){
a
=path
[a
];
}
while(path
[b
]!=-1){
b
=path
[b
];
}
double disA
=pow(G
[a
].x
,2)+pow(G
[a
].y
,2);
double disB
=pow(G
[b
].x
,2)+pow(G
[b
].y
,2);
if(disB
<=disA
){
return true
;
}else{
return false
;
}
}