概述
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
BFS:
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<double, double> node;
node c[128];
node O(0, 0);
node shortest_path[128];
int N, D;
int ans = 1e9;
double distance(node a, node b)
{
double x = a.x - b.x;
double y = a.y - b.y;
return sqrt(x * x + y * y);
}
int cmp(int a, int b)
{
return distance(c[a], O) < distance(c[b], O);
}
void bfs(int start)
{
int vis[128]={0};
int pre[128] = {0};
int last = start, cnt = 1;
queue<int>q;
q.push(start);
while(!q.empty())
{
int cur = q.front();
q.pop();
if(c[cur].x - D <= -50 || c[cur].x + D >= 50 || c[cur].y - D <= -50 || c[cur].y + D >= 50)
{
if(cnt < ans)
{
ans = cnt;
for(int i = 0; i < cnt; i++)
{
shortest_path[i] = c[cur];
cur = pre[cur];
}
}
return;
}
for(int i = 0; i < N; i++)
{
if(!vis[i] && distance(c[i], c[cur]) <= D)
{
vis[i] = 1;
pre[i] = cur;
q.push(i);
}
}
if(cur == last)
{
cnt++;
last = q.back();
// 当前队尾,下一层的最后一个点
}
}
}
int main()
{
//
system("chcp 65001");
cin.tie(0);
cout.tie(0);
//
freopen("C:/Users/zhaochen/Desktop/input.txt", "r", stdin);
cin >> N >> D;
if(D >= 50 - 7.5)
{
cout << 1;
return 0;
}
vector<int>start;
for(int i = 0; i < N; i++)
{
cin >> c[i].x >> c[i].y;
if(distance(c[i], O) - 7.5 <= D)
{
start.push_back(i);
}
}
sort(start.begin(), start.end(), cmp);
for(int i = 0; i < start.size(); i++)
{
bfs(start[i]);
}
if(ans < 1e9)
{
cout << ans + 1;
for(int i = ans - 1; i >= 0; i--)
{
cout << endl << shortest_path[i].x << " " << shortest_path[i].y;
}
}
else
cout << 0;
return 0;
}
最后
以上就是无辜火龙果为你收集整理的浙大数据结构07-图5 Saving James Bond - Hard Version_bfs的全部内容,希望文章能够帮你解决浙大数据结构07-图5 Saving James Bond - Hard Version_bfs所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复