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

分析

一开始没注意到中心小岛直径15,测试点2和3一直过不了,哭。
这道题和上一题的做法差不多,换汤不换药。
具体步骤:

  1. 判断哪些点是可以相连的,生成图
  2. 寻找第一个可以跳跃的点,开始DFS
  3. 当判断出遍历到的点可以跳到岸边时,结束程序

代码

#include
#include
#include
#include
#define MAXN 105
using namespace std;
int visited[MAXN];
int n,m,k;
typedef struct GNode *PtrToGNode;
struct GNode{
	int v;
	int e;
	int g[MAXN][MAXN];
};
struct point{
	int x;
	int y;
}p[MAXN];
typedef PtrToGNode MGraph;
MGraph CreateGraph(){
	int v,w;
	MGraph G;
	G=(MGraph)malloc(sizeof(struct GNode));
	G->v=n;
	G->e=0;
	for(v=0;vg[v][w]=0;
	return G;
}
int isSave(int a,int b){//寻找可跳点
	int c=(p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y);
	if(sqrt(c)<=m)return 1;
	else return 0;
}
MGraph BuildGraph(){
	MGraph G;
	int x,y,k;
	G=CreateGraph();
	for(int i=0;i>x>>y;
		p[i].x=x;p[i].y=y;
	}
	for(int i=0;ig[i][j]&&isSave(i,j)){
			G->g[i][j]=1;
			G->g[j][i]=1;	
			}
		}
	}
	return G;
}
void DFS(MGraph G,int v){
	if(k==1)return;
	visited[v]=1;
	if(p[v].x<=m-50||p[v].x>=50-m||p[v].y<=m-50||p[v].y>=50-m)//结束程序的条件 
	 {k=1;return;}
	for(int i=0;ig[v][i]&&!visited[i])
			DFS(G,i);
	}
}
void Save007(MGraph G){
	for(int i=0;i>n>>m;
	k=0;
	G=BuildGraph();
	memset(visited,0,sizeof(visited));
	Save007(G);
	return 0;
}

你可能感兴趣的:(06-图2 Saving James Bond - Easy Version (25分))