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; 
} 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章