07-图5 Saving James Bond - Hard Version (最短路径)(bfs)

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 (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, 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 (x,y) 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

大意:有一个湖,其为100*100的面积,中间有一个“直径”为15的岛,007在这个岛上,周围都是有n条鳄鱼,007跳跃距离为d,通过重复调到鳄鱼头上来跳出这个湖,求最短跳跃次数和路径

思路:本次用的是BFS,当然了,肯定还是有其他方法的

反思:这个是直径直径直径,我一直以为是半径,害我找半天bug,心塞了

附上AC代码:

#include <cstdio>   
#include <iostream> 
#include <algorithm> 
#include <cmath> 
#include <cstdlib> 
#include <cstring> 
#include <vector> 
#include <list> 
#include <map> 
#include <stack> 
#include <queue> 
using namespace std; 
#define ll long long
struct ey
{
	double x,y;
};
bool cmp(ey a,ey b)
{
	return a.x*a.x+a.y*a.y < b.x*b.x+b.y*b.y;
}
ey positon[105];
bool visi[105];
int path[105];
int n,d;
void bfs()
{
	queue<int> q;
	int s = 0;
	int sum = 1;
	int flag = 0;
	for(int i = 0;i < n;i++)
		if(positon[i].x*positon[i].x+positon[i].y*positon[i].y <= (d+7.5)*(d+7.5))
		{
			q.push(i);
			visi[i] = 1;
			s++;
		}
	while(!q.empty())
	{
		ey temp = positon[q.front()];
		if(temp.x+d >= 50 || temp.x - d <= -50||temp.y+d>=50||temp.y-d<=-50)
		{
			sum++;
			flag = 1;
			break;
		}
		for(int i = 0;i < n;i++)
			if(!visi[i]&&(positon[i].x-temp.x)*(positon[i].x-temp.x)+(positon[i].y-temp.y)*(positon[i].y-temp.y) <= d*d)
			{
				path[i] = q.front();
				visi[i] = 1;
				q.push(i);
			}
		q.pop();
		s--;
		if(s == 0)
		{
			sum++;
			s = q.size();
		}
	}
	if(flag)
	{
		cout << sum <<endl;
		int t = q.front();
		int a[105];
		int i = 0;
		for(i = 0;t != -1;i++)
		{
			a[i] = t;
			t = path[t];
		}
		while(i--)
			cout << positon[a[i]].x << ' '<<positon[a[i]].y <<endl;
	}
	else
		cout << 0 <<endl;

}
int main() 
{
	while(cin >> n >> d)
	{
		memset(visi,0,sizeof(visi));
		memset(path,-1,sizeof(path));
		for(int i = 0;i < n;i++)
			cin >> positon[i].x >> positon[i].y;
		sort(positon,positon+n,cmp);
		if(d >= 35)
			cout << 1 <<endl;
		else if(n == 0)
			cout << 0 <<endl;
		else
			bfs();
	}
    //cout << "AC" <<endl; 
    return 0; 
} 

猜你喜欢

转载自blog.csdn.net/MMMMMMMW/article/details/80376220