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 (), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the ( 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
//程序有问题,提交之后只有16’


#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<functional>
using namespace std;
//ifstream inFile("C:\\Users\\DELL\\Desktop\\in.txt", ios::in);
const int MaxVertexNum = 100;
typedef int Vertex;
typedef int WeightType;
typedef int DatatType;



//******************************************************************************点的结构体
struct point {
	int x;
	int y;
	int index;
	point(int index, int x, int y)
	{
		this->index = index;
		this->x = x;
		this->y = y;
	}
};


//边的定义
typedef struct ENode *PtrToENode;
struct ENode {
	Vertex V1, V2;
	WeightType Weight;
};
typedef PtrToENode Edge;

//邻接点的定义
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode {
	WeightType Weight;
	Vertex AdjV;
	PtrToAdjVNode Next;
};

//顶点表头结点的定义
typedef struct VNode {
	PtrToAdjVNode FirstEdge;
	DatatType Data;
}AdjList[MaxVertexNum];

typedef struct GNode* PtrToGNode;
struct GNode {
	int Nv;
	int Ne;
	AdjList G;
};
typedef  PtrToGNode LGraph;

LGraph CreateGraph(int VertexNum)
{

	LGraph Graph;



	Graph = new GNode;
	Graph->Ne = 0;
	Graph->Nv = VertexNum;

	for (int i = 0; i<Graph->Nv; ++i) {
		Graph->G[i].FirstEdge = nullptr;
		Graph->G[i].Data = i;
	}

	return Graph;



}


void InsertEdge(LGraph Graph, ENode Edge)
{
	PtrToAdjVNode NewNode = new AdjVNode;

	NewNode->AdjV = Edge.V2;
	NewNode->Weight = Edge.Weight;
	NewNode->Next = Graph->G[Edge.V1].FirstEdge;
	Graph->G[Edge.V1].FirstEdge = NewNode;


	PtrToAdjVNode NewNode1 = new AdjVNode;
	NewNode1->AdjV = Edge.V1;
	NewNode1->Weight = Edge.Weight;
	NewNode1->Next = Graph->G[Edge.V2].FirstEdge;
	Graph->G[Edge.V2].FirstEdge = NewNode1;

}

double Distance(point p1, point p2)
{
	return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}
LGraph Build(Vertex V, int MaxDistance)
{







	LGraph Graph;
	ENode E;
	Graph = CreateGraph(V + 2);
	//*************************************************************输入鳄鱼的坐标
	vector<point> Vec;
	Vec.push_back(point(0, 0, 0));
	for (int i = 1; i <= V; ++i) {
		int x, y;
		cin >> x >> y;
		Vec.push_back(point(i, x, y));
	}
	//**************************************************************************


	//****************************************************************建立图
	for (int i = 0; i<V; ++i) {
		for (int j = i + 1; j <= V; ++j) {
			if (Distance(Vec[i], Vec[j]) <= MaxDistance) {
				E.V1 = i;
				E.V2 = j;
				E.Weight = 0;
				InsertEdge(Graph, E);
			}

		}
		//************************************************检验四边是否可以达到
		if (((Vec[i].x + 50) <= MaxDistance) || (50 - Vec[i].x) <= MaxDistance || 50 - Vec[i].y <= MaxDistance || Vec[i].y + 50 <= MaxDistance) {
			E.V1 = i;
			E.V1 = V + 1;
			E.Weight = 1;
			InsertEdge(Graph, E);
		}


	}

	return Graph;

}
void Print(LGraph Graph)
{
	if (Graph->Nv != 0) {
		for (int i = 0; i<Graph->Nv; ++i) {
			PtrToAdjVNode It = Graph->G[i].FirstEdge;
			cout << Graph->G[i].Data << "   ";
			while (It) {
				cout << Graph->G[It->AdjV].Data << "	";
				It = It->Next;
			}
			cout << endl;
		}
	}
}
void Visit(Vertex V)
{
	cout << V << " ";
}

/* Visited[]为全局变量,已经初始化为false */
void DFS(LGraph Graph, bool* Visited, Vertex V, int &a)
{   /* 以V为出发点对邻接表存储的图Graph进行DFS搜索 */
	if (V == Graph->Nv - 1) {
		a = 1;
		return;
	}
	PtrToAdjVNode W;

	//Visit(V); /* 访问第V个顶点 */
	Visited[V] = true; /* 标记V已访问 */

	for (W = Graph->G[V].FirstEdge; W; W = W->Next) /* 对V的每个邻接点W->AdjV */
		if (!Visited[W->AdjV])    /* 若W->AdjV未被访问 */
			DFS(Graph, Visited, W->AdjV, a);    /* 则递归访问之 */
}
void BFS(LGraph Graph, bool* Visited, int index)
{
	queue<PtrToAdjVNode> Q;
	Visited[index] = true;
	Visit(index);
	PtrToAdjVNode P = new AdjVNode;
	P->AdjV = index;
	P->Next = nullptr;
	Q.push(P);
	while (Q.size()) {
		int a = Q.front()->AdjV;
		PtrToAdjVNode it = Graph->G[Q.front()->AdjV].FirstEdge;
		Q.pop();
		while (it) {
			if (Visited[it->AdjV] == false) {
				Q.push(it);
				Visited[it->AdjV] = true;
				Visit(it->AdjV);
			}
			it = it->Next;
		}
	}
}
void PrintBFS(LGraph Graph, bool* Visited)
{
	//cout << "广度优先搜索" << endl;
	for (int i = 0; i<Graph->Nv; ++i) {
		if (Visited[i] == false) {
			cout << "{ ";
			BFS(Graph, Visited, i);
			cout << "}" << endl;
		}

	}
}
void PrintDFS(LGraph Graph, bool* Visited)
{
	int m = 0;
	//cout << "深度优先搜索" << endl;
	for (int i = 0; i<Graph->Nv; ++i) {
		if (Visited[i] == false) {
			cout << "{ ";
			DFS(Graph, Visited, i, m);
			cout << "}" << endl;
		}
	}
}
PtrToAdjVNode Sort(PtrToAdjVNode P)
{
	vector<int> a;
	while (P) {
		a.push_back(P->AdjV);
		P = P->Next;
	}
	sort(a.begin(), a.end());
	delete P;
	PtrToAdjVNode tem = nullptr, now = nullptr;
	for (size_t i = 0; i<a.size(); ++i) {
		if (P == nullptr) {
			P = new AdjVNode;
			P->AdjV = a[i];
			P->Next = nullptr;
			now = P;
		}
		else {
			tem = new AdjVNode;
			tem->AdjV = a[i];
			now->Next = tem;
			tem->Next = nullptr;
			now = tem;
		}
	}
	return P;
}
void LSort(LGraph Graph)
{
	for (int i = 0; i<Graph->Nv; ++i) {
		Graph->G[i].FirstEdge = Sort(Graph->G[i].FirstEdge);
	}
}
int main()
{
	int Vertex, MaxDistance;
	cin >> Vertex >> MaxDistance;
	LGraph Graph = Build(Vertex, MaxDistance);
	LSort(Graph);
	//Print(Graph);
	bool Visited[MaxVertexNum], Visited1[MaxVertexNum];
	for (int i = 0; i<Graph->Nv; ++i) {
		Visited[i] = false;
		Visited1[i] = false;
	}
	int a = 0;
	DFS(Graph, Visited1, 0, a);
	if (a == 1) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
	//PrintBFS(Graph, Visited);
	//cout << endl;


	delete Graph;
	//inFile.close();
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值