06-图2 Saving James Bond - Easy Version
分数 25
作者 陈越
单位 浙江大学
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 whether or not he can escape.
Input Specification:
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.
Output Specification:
For each test case, print in a line “Yes” if James can escape, or “No” if not.
Sample Input 1:
14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12
Sample Output 1:
Yes
Sample Input 2:
4 13
-12 12
12 12
-12 -12
12 -12
Sample Output 2:
No
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
C++ (g++)
思路:
本题亮点,用结构体表示图。
先放陈越姥姥的讲解视频,在这儿~
bfs算法改进:
如果能到达岸边,直接返回true
否则置访问标志,然后对该结点进行深度优先遍历。
设置人为0,鳄鱼为1~n,岸边为n+1;
//人与岸边
岸边-小岛半径与d比较
//人与鳄鱼
人与鳄鱼的距离与d比较
//鳄鱼与鳄鱼
鳄鱼与鳄鱼的距离与d比较
//鳄鱼与岸边
鳄鱼与岸边距离的绝对值与d比较
AC代码:
#include<bits/stdc++.h>
using namespace std;
int G[300][300];
int vis[300];
struct location{
int x;
int y;
}loc[100];
float dic(location a,location b){
return pow(pow(a.x-b.x,2)+pow(a.y-b.y,2),0.5);
}
bool DFS(int cur,int n){
if(cur==n+1) return true;
vis[cur]=1;
bool flag=false;
int i;
for(i=1;i<=n+1;i++){
if(!vis[i]&&G[cur][i]){
flag=DFS(i,n);
if(flag) return true;
}
}
if(i==n+2) return false;
}
int main(){
memset(vis,0,sizeof(vis));
int n,d;
cin>>n>>d;
//设置人为0,鳄鱼为1~n,岸边为n+1
loc[0].x=loc[0].y=0;
//人与岸边,可不可以直接跳过去
if(50-7.5<=d){
cout<<"Yes"<<endl;
return 0;
}
//鳄鱼与人的边
for(int i=1;i<=n;i++){
cin>>loc[i].x>>loc[i].y;
if(dic(loc[0],loc[i])-7.5<=d)
G[0][i]=G[i][0]=1;
}
//鳄鱼与鳄鱼
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(dic(loc[i],loc[j])<=d||i==j)
G[i][j]=G[j][i]=1;
}
}
//鳄鱼与岸边
for(int i=1;i<=n;i++){
if(abs(abs(loc[i].x)-50)<=d||abs(abs(loc[i].y)-50)<=d)
G[i][n+1]=G[n+1][i]=1;
}
if(DFS(0,n))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}