06-图2 Saving James Bond

来源:互联网 发布:埃尔金贝勒数据 编辑:程序博客网 时间:2024/06/05 23:48
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 NNN (≤100\le 100≤100), the number of crocodiles, and DDD, the maximum distance that James could jump. Then NNN lines follow, each containing the (x,y)(x, y)(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:
//1.#include <stdio.h>#include <stdlib.h>#include <string.h>#define INF 0#define MAXN 105int d, flag, visit[MAXN], n;typedef struct GNode* Graph;struct GNode {    int Ne, Nv;    int Data[MAXN][MAXN];};typedef struct ENode* Edge;struct ENode {    int V1, V2;};Graph CreateGraph(int n) {    int i, j;    Graph G = (Graph)malloc(sizeof(struct GNode));    G->Nv = n;    G->Ne = 0;    for(i = 0; i < n; i++)        for(j = 0; j < n; j++)            G->Data[i][j] = INF;    return G;}int distance(int x1, int y1, int x2, int y2) {    return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);}void InsertEdge(Graph G, Edge E) {    G->Data[E->V1][E->V2] = G->Data[E->V2][E->V1] = 1;}int IsEdge(Graph G, int v, int w) {    return G->Data[v][w];}void DFS(Graph G, int s) {    int i = 0;    if(s == n+1) flag = 1;    visit[s] = 1;    for(i = 0; i <= n+1; i++) {        if(IsEdge(G, s, i) && !visit[i])            DFS(G, i);    }}void PrintGraph(Graph G) {    int i, j;    for(i = 0; i <= n+1; i++) {        for(j = 0; j <= n+1; j++) {            printf("%3d", G->Data[i][j]);        }        printf("\n");    }}int Judge(int n, int x, int y) {    if(x < 0) x = -x;    if(y < 0) y = -y;    if(n-x <= d || n-y <= d) return 1;    else return 0;}int main() {    int x[MAXN], y[MAXN], i, j;    Graph G;    Edge E = (Edge)malloc(sizeof(struct ENode));    scanf("%d%d", &n, &d);    memset(visit, 0, sizeof(visit));    G = CreateGraph(n+2);    for(i = 1; i <= n; i++) {        scanf("%d%d", &x[i], &y[i]);        if(Judge(50,x[i], y[i])) {            E->V1 = n+1; E->V2 = i; InsertEdge(G, E);        }        if(distance(x[i], y[i], 0, 0) <= (d+15)*(d+15)) {            E->V1 = 0; E->V2 = i; InsertEdge(G, E);        }    }    for(i = 1; i <= n; i++) {        for(j = 1; j <= n; j++) {            if(distance(x[i], y[i], x[j], y[j]) <= d*d)            { E->V1 = i; E->V2 = j; InsertEdge(G, E); }        }    }    //PrintGraph(G);    DFS(G, 0);    if(flag) printf("Yes\n");    else printf("No\n");    return 0;}//2.#include <stdio.h>#include <string.h>#include <stdlib.h>#define MAX 105int visit[MAX];int n, m;typedef struct VNode Vertex;struct VNode {    int X, Y;}V[MAX];int FirstJump(int v) {    int x = V[v].X, y = V[v].Y, D;    D = x*x + y*y;    if(D <= (15+m)*(15+m)) return 1;    else return 0;}int Jump(int v, int w) {    int x1, x2, y1, y2, D;    x1 = V[v].X;    x2 = V[w].X;    y1 = V[v].Y;    y2 = V[w].Y;    D = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);    return D <= m*m ? 1 : 0;}int IsSafe(int v) {    int x, y;    x = V[v].X;    y = V[v].Y;    if(x < 0) x = -x;    if(y < 0) y = -y;    if(50-x <= m || 50-y <= m) return 1;    else return 0;}int DFS(int v) {    int w;    int ans = 0;    visit[v] = 1;    if(IsSafe(v)) ans = 1;    for(w = 0; w < n; w++) {        if(Jump(v, w) && !visit[w]) {            ans = DFS(w);            if(ans) break;        }    }    return ans;}void Save007() {    int i, ans;    for(i = 0; i < n; i++) {        if(!visit[i] && FirstJump(i))            ans = DFS(i);        if(ans) break;    }    if(ans) printf("Yes\n");    else printf("No\n");}int main() {    int i;    memset(visit, 0, sizeof(visit));    scanf("%d%d", &n, &m);    for(i = 0; i < n; i++)        scanf("%d%d", &V[i].X, &V[i].Y);    Save007();    return 0;}/*感觉第二种方法有点难想。与其返回BFS的值,不如设置一个全局变量flag。心得:DFS的变式,图的定义是抽象的,并非要是点,如同这题可以将外围及中心的区域都抽象成点,只需稍微改动一下连通的定义。还有,并非一定要建图。*/



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

原创粉丝点击