【C/C++】06-图2 Saving James Bond

来源:互联网 发布:centos arp清理 编辑:程序博客网 时间:2024/05/18 02:30

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.


题目源于:https://pintia.cn/problem-sets/900290821590183936/problems/916508881217581056

思路分析:图的遍历的问题,通过跳跃距离建立各点之间的联系来建边,跳跃的第一步要考虑岛的半径。

#include <iostream>#include <cmath>using namespace std;#define vertex int#define WeightType int#define MaxData 101struct GNode {int Ne;int Nv;WeightType G[MaxData][MaxData];};typedef struct GNode * Graph;struct PNode {int x;int y;};typedef struct PNode * Point;struct ENode {vertex v1, v2;WeightType weight;};typedef struct ENode * Edge;Graph CreateGraph(int size);void InsertEdge(Graph map, Edge e);bool SaveJamesBond();int distance(struct PNode p1, struct PNode p2);//bool Save007(int d, int num, Point p, Graph map, bool* visited);bool DFS(int d, int num, Point p, Graph map, bool* visited);int main(){bool isSaved = SaveJamesBond();if (isSaved) cout << "Yes";else cout << "No";    return 0;}Graph CreateGraph(int size) {Graph m = new struct GNode;m->Nv = size;m->Ne = 0;for (int i = 0; i <= size; i++)for (int j = 0; j <= size; j++)m->G[i][j] = 0;return m;}void InsertEdge(Graph map, Edge e) {map->G[e->v1][e->v2] = e->weight;map->G[e->v2][e->v1] = e->weight;}bool SaveJamesBond() {Graph map = new struct GNode;//鳄鱼条数int nv; cin >> nv;//鳄鱼坐标Point head = new struct PNode[nv+1];//是否访问过bool * visited = new bool[nv + 1];for (int i = 0; i < nv + 1  ; i++)visited[i] = false;map = CreateGraph(nv);int d;//跳跃距离dcin  >> d;head[0].x = 0; head[0].y = 0; visited[0] = false;for (int i = 1; i <= nv; i++) {cin >> head[i].x >> head[i].y;}//第一跳,构建从湖心小岛可以跳到的第一条鳄鱼的所有路径for (int i = 1; i <= nv; i++) {Edge e=new struct ENode;if ((d + 15) >= distance(head[0], head[i])) {e->v1 = 0; e->v2 = i; e->weight = distance(head[0], head[i]);InsertEdge(map, e);}}if ((d + 15) >= 50)return true;//构建当JAMES在第i条鳄鱼头上时,可以跳到的邻近鳄鱼的所有路径for (int i = 1; i <= nv; i++) {for (int j = i + 1; j <= nv; j++) {Edge e= new struct ENode;if (d >= distance(head[i], head[j])) {e->v1 = i; e->v2 = j; e->weight = distance(head[i], head[j]);InsertEdge(map, e);}}}bool isSaved = false;isSaved = DFS(d,0, head, map, visited);return isSaved;}bool DFS(int d,int num, Point p, Graph map, bool* visited) {visited[num] = true;if (abs(p[num].x) + d >= 50 || abs(p[num].y) + d >= 50) return true;for (int i = 0; i <= map->Nv; i++) {if (!visited[i] && map->G[num][i] > 0) {if( DFS(d, i, p, map, visited)) return true;}}//for (int i = 0; i < map->Nv; i++)if (visited[i] == false)return false;return false;}int distance(struct PNode p1, struct PNode p2) {int dist = sqrt((p2.x - p1.x)*(p2.x - p1.x) + (p2.y - p1.y)*(p2.y - p1.y));return dist;}


原创粉丝点击