本文主要是介绍06-图2 Saving James Bond - Easy Version (25分),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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
#include<iostream>
#include<cmath>
using namespace std;struct Node {int x,y;int flag;
};struct MNode {int Vertex[100][100];Node t[100];
};typedef struct MNode* MGraph;
MGraph Create(int n);
int DFS(MGraph Graph,int Root,bool Visited[],int D,int N);int main() {int N,D,x,y;double num;cin>>N>>D;bool Visited[N]= {false};MGraph Graph=Create(N);for(int i=0; i<N; i++) {x=Graph->t[i].x;y=Graph->t[i].y;num=sqrt(x*x+y*y);if(num-7.5<=D)Graph->t[i].flag=1;}for(int i=0; i<N-1; i++)for(int j=i+1; j<N; j++) {x=Graph->t[i].x-Graph->t[j].x;y=Graph->t[i].y-Graph->t[j].y;num=sqrt(x*x+y*y);if(num-D<=0) {Graph->Vertex[i][j]=1;Graph->Vertex[j][i]=1;}}int count=0;for(int i=0;i<N;i++){if(Graph->t[i].flag){count+=DFS(Graph,i,Visited,D,N);}}if(count>0)printf("Yes");else printf("No");//
}MGraph Create(int n) {MGraph Graph=(MGraph)malloc(sizeof(struct MNode));for(int i=0; i<n; i++) {cin>>Graph->t[i].x>>Graph->t[i].y;Graph->t[i].flag=0;for(int j=0; j<n; j++)Graph->Vertex[i][j]=0;}return Graph;
}int DFS(MGraph Graph,int Root,bool Visited[],int D,int N) {Visited[Root]=true;int count=0;if(Graph->t[Root].x<=D-50||Graph->t[Root].x>=50-D)return 1;if(Graph->t[Root].y<=D-50||Graph->t[Root].y>=50-D)return 1;for(int i=0; i<N; i++) {if(!Visited[i]&&Graph->Vertex[Root][i]>0)count+=DFS(Graph,i,Visited,D,N);}return count;
}
#include<bits/stdc++.h>
using namespace std;
struct Node {double x, y;double distant;
};
int N;
int D, G[105][105];
Node Dist[105];
bool visit[105] = {false};void Init(int i, int j);
int DFS(int i);int main() {cin >> N >> D;int X, Y;int flag = 0;fill(G[0], G[0] + 105*105, 200);for (int i = 0; i < N; i++) {cin >> X >> Y;Dist[i].x = X;Dist[i].y = Y;Dist[i].distant = sqrt(X * X + Y * Y);}for (int i = 0; i < N - 1; i++) {for (int j = i + 1; j < N; j++) {Init(i, j);}}for (int i = 0; i < N; i++) {if (Dist[i].distant <= D + 7.5 && !visit[i]) {flag = DFS(i);if (flag == 1) {cout << "Yes";break;}}}if (!flag) cout << "No";return 0;
}void Init(int i, int j) {int x = Dist[i].x - Dist[j].x;int y = Dist[i].y - Dist[j].y;G[i][j] = G[j][i] = sqrt(x * x + y * y);
}int DFS(int i) {if ((Dist[i].x + D >= 50) || (Dist[i].x - D <= -50) || (Dist[i].y + D >= 50) || (Dist[i].y - D <= -50)) return 1;visit[i] = true;for (int j = 0; j < N; j++) {if (G[i][j] <= D && !visit[j]) {int temp = DFS(j);if (temp == 1) return 1;}}return 0;
}
这篇关于06-图2 Saving James Bond - Easy Version (25分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!