07-图5 Saving James Bond - Hard Version (30分)

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 a shortest path to reach one of the banks. The length of a path is the number of jumps that James has to make.

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, if James can escape, output in one line the minimum number of jumps he must make. Then starting from the next line, output the position ( of each crocodile on the path, each pair in one line, from the island to the bank. If it is impossible for James to escape that way, simply give him 0 as the number of jumps. If there are many shortest paths, just output the one with the minimum first jump, which is guaranteed to be unique.

Sample Input 1:

17 15
10 -21
10 21
-40 10
30 -50
20 40
35 10
0 -10
-25 22
40 -40
-30 30
-10 22
0 11
25 21
25 10
10 10
10 35
-30 10

Sample Output 1:

4
0 11
10 21
10 35

Sample Input 2:

4 13
-12 12
12 12
-12 -12
12 -12

Sample Output 2:

0
//结果有点错误只是能找到最短路径,但不能满足(If there are many shortest paths, just output the one with the minimum first jump, which is guaranteed to be unique.
#include<iostream>
#include<fstream>
#include<vector>
#include<stack>
#include <algorithm>
using namespace std;
ifstream inFile("C:\\Users\\DELL\\Desktop\\in.txt");

struct Position{
	int x, y;
};
//************************************************************************键入所有鳄鱼的位置坐标
vector<Position> EnterPositon(int N)
{
	vector<Position> v;
	Position P;
	while (N--) {
		inFile >> P.x >> P.y;
		v.push_back(P);
	}
	return v;
}
//************************************************************************输出检验所有鳄鱼的位置
void PrintCrocodilesPosition(vector<Position> v)
{
	for (vector<Position>::const_iterator it = v.cbegin(); it != v.cend(); ++it) {
		cout << it->x << "		" << it->y << endl;
	}
}



//***********************************************************************用邻接矩阵建立图
#define MaxVertexNum 102          //最大顶点数设为102
#define INFINITY1 65535            //∞设为双字节无符号整数最大值65535
typedef int Vertex;				  //用顶点下标表示顶点,为整型
typedef double WeightType;        //边的权值设为双精度浮点类型
typedef int DataType;             //顶点储存的数据类型设为整型


//**************************************图结点的定义
typedef struct GNode *PtrToGNode;
struct GNode {
	int Nv;          //顶点数
	int Ne;          //边数
	WeightType G[MaxVertexNum][MaxVertexNum];//邻接矩阵
	DataType Data[MaxVertexNum];
};
typedef PtrToGNode MGraph;     //以邻接矩阵存储的图类型



//*************************************边的定义
typedef struct ENode* PtrToENode;
struct ENode {
	Vertex V1, V2;           //有向边<V1,V2>
	WeightType Weight;      //权重
};
typedef PtrToENode Edge;

//************************************图的初始化
MGraph CreateGraph(int VertexNum)
{
	MGraph Graph = new GNode;
	Graph->Ne = 0;
	Graph->Nv = VertexNum;
	for (int i = 0; i < Graph->Nv; ++i) {
		for (int j = 0; j < Graph->Nv; ++j) {
			Graph->G[i][j] = INFINITY1;
		}
	}
	return Graph;
}
//************************************插入边
void InsretEdge(MGraph Graph, Edge E)
{
	Graph->G[E->V1][E->V2] = E->Weight;
	Graph->G[E->V2][E->V1] = E->Weight;
}


//***************************************计算坐标之间的距离<中心岛,鳄鱼>,<中心岛,岸>,<鳄鱼,鳄鱼>,<鳄鱼,岸>之间的距离
double Distance(const Position& p1, const Position& p2)
{
	return sqrt((p1.x - p2.x)*(p1.x - p2.x) +(p1.y - p2.y)*(p1.y - p2.y));
}
//************************************建立图
MGraph BulidGraph(vector<Position> v,int D)
{

	//中心岛的坐标表示为(0,0)
	//鳄鱼的坐标表示为1-v.size()
	//岸边可以表示为size()+1;


	MGraph Graph = CreateGraph(v.size()+2);     //初始化一个有Nv个顶点但没有边的图
	Edge E = new ENode;
	//1.中心岛与鳄鱼之间的距离
	Position P;       //中心岛坐标
	P.x = P.y = 0;
	for (size_t i = 0; i < v.size(); ++i) {
		double d = Distance(P, v[i]);
		if ( d<= 7.5 + D) {
			E->V1 = 0;
			E->V2 = i + 1;
			E->Weight = d - 7.5;
			InsretEdge(Graph,E);
		}
	}

	//2.中心岛与岸之间的距离为50,不可能
	

	//3.鳄鱼与岸之间的距离
	for (size_t i = 0; i < v.size(); ++i) {
		double d1, d2, d3, d4,MinD;
		d1 = fabs(v[i].x + 50);
		d2 = abs(50 - v[i].x);
		d3 = abs(v[i].y + 50);
		d4 = abs(50 - v[i].y);
		MinD = min(d1, d2);
		MinD = min(MinD, d3);
		MinD = min(MinD, d4);
		if (MinD <= D) {
			E->V1 = i + 1;
			E->V2 = v.size() + 1;
			E->Weight = MinD;
			InsretEdge(Graph, E);
		}
	}


	//0-v.size()-1  全是鳄鱼
	//4.鳄鱼与鳄鱼之间的距离
	for (size_t i = 0; i < v.size(); ++i) {
		for (size_t j = 0; j < v.size(); ++j) {
			if (i != j) {
				double d = Distance(v[i], v[j]);
				if (d <= D) {
					E->V1 = i + 1;
					E->V2 = j + 1;
					E->Weight = d;
					InsretEdge(Graph, E);
				}
			}
		}
	}
	return Graph;
}
//*********************************输出图
void PrinttGraph(MGraph Graph)
{
	for (int i = 0; i < Graph->Nv; ++i) {
		for (int j = 0; j < Graph->Nv; ++j) {
			if (Graph->G[i][j] !=65535)
				cout << Graph->G[i][j] << " " ;
		}
		cout << endl;
	}
}



//**************************************************************Dijksta
#define ERROR -1

Vertex FindMinDist(MGraph Graph, double dist[], bool collected[])
{
	//返回未被收录顶点中dist最小者
	Vertex MinV;
	double MinDist = INFINITY1;
	for (int v = 0; v < Graph->Nv; ++v) {
		if (collected[v] == false && dist[v] < MinDist) {
			//若V未被收录,且dist[V]更小
			MinDist = dist[v];//更新更小距离
			MinV = v;//更新对应顶点
		}
	}
	if (MinDist < INFINITY1) {//找到最小dist
		return MinV;//返回对应的顶点下标
	}
	else {
		return ERROR;
	}
}
bool Dijksta(MGraph Graph, double dist[], int path[], Vertex S)
{
	bool collected[MaxVertexNum];
	for (int i = 0; i < MaxVertexNum;++i) {
		collected[i] = false;
	}
	for (int i = 0; i < Graph->Nv; ++i) {
		dist[i] = Graph->G[S][i];
		path[i] = -1;
	}
	//先将起点收入集合
	dist[S] = 0;
	collected[S] = true;


	while (1) {
		//v=未被收录顶点中dist最小者
		int V = FindMinDist(Graph, dist, collected);
		if (V == ERROR) {
			break;
		}
		collected[V] = true;//收录
		for (int W = 0; W < Graph->Nv; W++) {//对图中的每个节点
			//若W是V的节点,且未被收录
			if (collected[W] == false && Graph->G[V][W] < INFINITY1) {
				if (Graph->G[V][W] < 0) {//若有负边
					return false;//不能正确解决,返回错误标志
				}
				//若收录V使得dist[W]变小
				if (dist[V] + Graph->G[V][W] < dist[W]) {
					dist[W] = dist[V] + Graph->G[V][W];
					path[W] = V;
				}
			}
		}

	}
	return true;
}

int main()
{
	int N;               //the number of crocodiles
	int D;               // the maximum distance that James could jump
	inFile >> N >> D;
	vector<Position> v;  //the positions ofcrocodiles
	v=EnterPositon(N);   //键入所有鳄鱼的位置坐标
	//PrintCrocodilesPosition(v);
	MGraph Graph = BulidGraph(v,D);
	double dist[MaxVertexNum];
	int path[MaxVertexNum];
	Dijksta(Graph, dist, path, 0);
	stack<int> s;
	int index = N + 1;
	while (path[index] != -1) {
		s.push(path[index]);
		index = path[index];
	}
	while (s.size()) {
		cout << v[s.top()-1].x << " " << v[s.top()-1].y << endl;
		s.pop();
	}
	for (int i = 0; i < N + 2; ++i) {
		cout << path[i] << " ";
	}
	cout << endl;
	for (int i = 0; i < N + 2; ++i) {
		cout << dist[i] << " ";
		if (i % 5 == 0) {
			cout << endl;
		}
	}
	//PrinttGraph(Graph);
	inFile.close();
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值