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
#include <iostream>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const double D = 15.0;
double jump, cr[101][2];
int n;
bool cmp(int i, int j);
int bfs(int &v, vector<int>& path);
int main() {
int i, v = 0, flag = 0;
scanf("%d%lf", &n, &jump);
vector<int> path(n + 1, -1), spath;
cr[0][0] = 0;cr[0][1] = 0;
for (i = 1;i <= n;i++)
scanf("%lf%lf", &cr[i][0], &cr[i][1]);
if (jump + D / 2 >= 50) //从岛上可以直接到岸
flag = 1;
else flag = bfs(v, path);
if (!flag) printf("0");
else {
while (v) {
spath.push_back(v);
v = path[v];
}
printf("%d\n", spath.size() + 1);
for (i = spath.size() - 1; i >= 0; i--)
printf("%.0f %.0f\n", cr[spath[i]][0], cr[spath[i]][1]);
}
return 0;
}
int bfs(int &v, vector<int>& path) {
vector<int> dist(n + 1, -1), first(n + 1);
queue<int> q;
double firstdis;
int flag = 0, i;
dist[v] = 0;
for (i = 0;i < n + 1;i++)
first[i] = i;
//多条路径相同时输出第一跳距离最小,则第一跳的节点入队列之前先从小到大排队
sort(first.begin(), first.end(), cmp);
for (i = 1;i < n + 1;i++) {
v = first[i];
firstdis = sqrt(pow(cr[v][0], 2) + pow(cr[v][1], 2));
if (firstdis > D / 2 && firstdis <= jump + D / 2) {
q.push(v);
dist[v] = 1;
path[v] = 0;
}
}
while (!q.empty()) {
v = q.front();
q.pop();
if ((fabs(cr[v][0]) + jump >= 50 || fabs(cr[v][1]) + jump >= 50)) {
flag = 1;
break;
}
else {
for (i = 1;i < n + 1;i++) {
if (dist[i] == -1 &&
(sqrt(pow(cr[v][0] - cr[i][0], 2) + pow(cr[v][1] - cr[i][1], 2)) <= jump)) {
dist[i] = dist[v] + 1;
path[i] = v;
q.push(i);
}
}
}
}
return flag;
}
bool cmp(int i, int j) {
return pow(cr[i][0], 2) + pow(cr[i][1], 2) < pow(cr[j][0], 2) + pow(cr[j][1], 2);
}