05-圖2. Saving James Bond,05-bond
05-圖2. Saving James Bond - Easy Version (25)時間限制200 ms
記憶體限制65536 kB
代碼長度限制8000 B
判題程式Standard作者CHEN, Yue
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 2025 -15-25 288 4929 15-35 -25 2827 -29-8 -28-20 -35-25 -20-13 29-30 15-35 4012 12
Sample Output 1:
Yes
Sample Input 2:
4 13-12 1212 12-12 -1212 -12
Sample Output 2:
No
#include <stdio.h>#include <math.h>typedef struct {double x, y;}Coord;int reachCenter(double distance, Coord point) {//判斷一隻鱷魚是否能從島上一步跳至return (15 + distance)*(15 + distance) >= point.x*point.x + point.y*point.y;}int reachBetween(double distance, Coord p1, Coord p2) {//判斷兩隻鱷魚間能否一步跳到return distance*distance >= (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);}int reachBank(double distance, Coord p) {//判斷從這個鱷魚頭上能否跳到岸上return p.x <= -50 + distance || p.x >= 50 - distance || p.y >= 50 - distance || p.y <= -50 + distance;}int DFS(double distance, Coord *croc, int v, int *visited, int n) {//遞迴進行深度優先搜尋,找到路徑返回1if (reachBank(distance, croc[v]))return 1;for (int i = 0; i < n; ++i)if (!visited[i] && reachBetween(distance, croc[v], croc[i])) {visited[i] = 1;if (DFS(distance, croc, i, visited, n))return 1;}return 0;}int main() {//freopen("test.txt", "r", stdin);int n;double distance;scanf("%d%lf", &n, &distance);if (distance >= 35) {//如果能直接跳到岸上,直接返回結果printf("Yes\n");return 0;}Coord croc[102];//儲存鱷魚的座標for (int i = 0; i < n; ++i)scanf("%lf%lf", &croc[i].x, &croc[i].y);int visited[100] = {};//節點已訪問標記int flag = 0;//是否已找到路徑標記for (int i = 0; i < n; ++i)if (!visited[i] && reachCenter(distance, croc[i])) {//遍曆能從島上一步跳到的鱷魚visited[i] = 1;if (DFS(distance, croc, i, visited, n)) {//從當前鱷魚開始深度優先搜尋printf("Yes\n");flag = 1;break;}}if (flag == 0)printf("No\n");return 0;}
題目連結:http://www.patest.cn/contests/mooc-ds/05-%E5%9B%BE2
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。