06-图2 Saving James Bond - Easy Version

一、题目

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:
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:
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

二、分析

  1. 改变坐标原点位置(挪到左下角),使得所有坐标为非负数,以便建立图的邻接矩阵。
  2. 图的抽象,每个坐标点"G[i][j]"都是图的结点,图的边用"G[i][j]=1"来表示(该点有鳄鱼)。
  3. 通过从上到下,从左到右遍历每个结点,如果满足跳跃到鳄鱼的条件,就进行DFS递归,返回能否上岸的信号。
  4. 考虑极端情况:鳄鱼数量N(1~100),James Bond的跳跃距离D(最小为1)。

三、代码

#include <stdio.h>
#include <math.h>
#define MAXN 101
int G[MAXN][MAXN] = { 0 }, Ne;    //图的邻接矩阵
int jumpMax, x = 50, y = 50, answer = 0;	
//jumpMax:跳跃距离;  (x,y):詹姆斯邦德的当前的位置;    answer:是否能上岸

void BuildGraph()           //建图
{
	int i, v1, v2;
	scanf("%d %d", &Ne, &jumpMax);
	for (i = 0; i<Ne; i++)           //图的边
	{
		scanf("%d %d", &v1, &v2);
		v1 = v1 + 50;
		v2 = v2 + 50;
		G[v1][v2] = 1;			//该位置有鳄鱼
	}

}

//判断是否能跳到在(i,j)位置的鳄鱼
int Jump(int i, int j)
{
	int d;
	d = (int)sqrt((double)((i - x)*(i - x) + (j - y)*(j - y)));		//计算(i,j)与(x,y)距离
	if ((i == 50 && j == 50) || (x == 50 && y == 50)) d = d - 15;	//原点的陆地半径为15,所以跳出或跳入原点都减去15
	if (d <= jumpMax) return 1;
	else return 0;
}

//判断是否能跳上岸
int IsSave(int V, int W)
{
	if ((V - jumpMax) <= 0 || (V + jumpMax) >= 100 || (W - jumpMax) <= 0 || (W + jumpMax) >= 100) return 1;
	else return 0;
}

//深度优先搜索
int DFS(int V, int W)
{
	int i, j;
	if (IsSave(V, W)) answer = 1;			//如果这点能够跳上岸,马上结束。
	else
	{
		for (i = 0; i<MAXN; i++)
		{
			for (j = 0; j < MAXN; j++)
			{
				if (G[i][j] && Jump(i, j) && (i != V && j != W))			//该点有鳄鱼 && 能跳过去 && 不能往回跳
				{
					x = i;			//跳到新的鳄鱼,更新007的位置
					y = j;
					answer = DFS(i, j);
					G[i][j] = 0;	//该支路走不通,返回后做标记,不再进入这条支路
					if (answer == 1) break;			//
				}
			}

		}
	}
	return answer;
}
void Save007()
{
	int i, j;
	for (i = 0; i < MAXN; i++)
	{
		for (j = 0; j < MAXN; j++)
		{
			if (G[i][j] && Jump(i, j))			//遍历所有点,遇到鳄鱼且能跳过去就进入
			{
				x = i;
				y = j;
				answer = DFS(i, j);
				G[i][j] = 0;
			}

			if (answer == 1) break;				//双重循环,用两个break跳出
		}
		if (answer == 1) break;
	}
	if (answer == 1) printf("Yes");
	else printf("No");
}


int main()
{
	BuildGraph();
	Save007();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值