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
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
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;
cin >> n >> jump;
vector<int>path(n + 1, -1), spath; //创建一个动态数组并初始化为-1,和一个堆栈将路径倒出
cr[0][0] = 0; cr[0][1] = 0; //从1开始存储坐标数据
for (i = 1; i <= n; i++)
cin >> cr[i][0] >> cr[i][1];
if (jump + D / 2 >= 50) //直接可以跳上岸边
flag = 1;
else flag = bfs(v, path);
if (!flag)cout << "0";
else {
while (v) {
spath.push_back(v);
v = path[v];
}
cout << spath.size() + 1;
for (i = spath.size() - 1; i >= 0; i--)
cout << cr[spath[i]][0] << cr[spath[i]][1];
}
return 0;
}
//v是开始的结点 ,但是值最后成了尾结点(传的引用)
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);
}
加粗样式
看错题意直接将错就错,用dj帮007找了一手最短路径,还是得用bfs,dj不太行。
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int n, jumpD;
bool flag = false;
const int inf = 1000001;
int ed;
struct Node
{
int x, y;
int tag;
}node[101];
int G[101][101],vis[101], vec[101];
double d[101];
double countdis(int i, int j)
{
double dis = sqrt(pow(node[i].x - node[j].x, 2) +
pow(node[i].y - node[j].y, 2));
return dis;
}
bool jumpJudge(int i, int j)
{
double dis = sqrt(pow(node[i].x - node[j].x, 2) +
pow(node[i].y - node[j].y, 2));
if (dis <= jumpD)
return true;
else
return false;
}
bool isBorder(int i)
{
int dis1 = abs(node[i].x) + jumpD;
int dis2 = abs(node[i].y) + jumpD;
if (dis1 >= 50 || dis2 >= 50)
return true;
return false;
}
void dj(int i)
{
fill(d, d + 101, inf);
d[i] = 0;
for (int j = 0; j <= n; ++j)
{
int u = -1, mindis = inf;
for (int k = 0; k <= n; ++k)
{
if (vis[k] == false &&d[k] < mindis)
{
u = k;
mindis = d[k];
}
}
if (u == -1)
return;
vis[u] = true;
for (int v = 0; v <= n; v++)
{
if (vis[v] == false && jumpJudge(u, v))
{
if (d[u] + countdis(u, v) < d[v])
{
d[v] = d[u] + countdis(u, v);
vec[v] = u;
}
}
}
}
}
void output(int i)
{
if (i == 0)
{
cout << "0 0" << endl;
return;
}
output(vec[i]);
cout << node[i].x <<" "<< node[i].y << endl;
}
void findminLength()
{
int min = inf, u = -1;
for (int i = 1; i <= n; ++i)
{
if (isBorder(i) && d[i] < min)
{
min = d[i];
u = i;
}
}
if (u >= 0)
{
cout << "最短距离是" << d[16] << endl;
output(16);
}
else
cout << "未找到可以跳出的路径" << endl;
}
int main()
{
cin >> n >> jumpD;
node[0].x = node[0].y = 0;
node[0].tag = 1;
for (int i = 1; i <= n; ++i)
{
cin >> node[i].x >> node[i].y;
node[i].tag = 0;
}
dj(0);
findminLength();
return 0;
}
尼玛的相同跳步要第一跳距离最短,还以为距离最短,头炸了,就看第一跳先按距离排个序放入队列中,有点无语子
#include<iostream>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
int n, jumpD;
bool flag = false;
const int inf = 1000001;
int ed;
struct Node
{
int x, y;
}node[101];
int G[101][101],vis[101], vec[101];
double d[101];
double countdis(int i, int j)
{
double dis = sqrt(pow(node[i].x - node[j].x, 2) +
pow(node[i].y - node[j].y, 2));
return dis;
}
bool jumpJudge(int i, int j)
{
double dis = sqrt(pow(node[i].x - node[j].x, 2) +
pow(node[i].y - node[j].y, 2));
if (dis <= jumpD)
return true;
else
return false;
}
bool isBorder(int i)
{
int dis1 = abs(node[i].x) + jumpD;
int dis2 = abs(node[i].y) + jumpD;
if (dis1 >= 50 || dis2 >= 50)
return true;
return false;
}
bool cmp(int i, int j)
{
return pow(node[i].x, 2) + pow(node[i].y, 2)< pow(node[j].x, 2) + pow(node[j].y, 2);
}
int bfs()
{
vector<int>vv;
for (int i = 0; i <= n; i++)
{
vv.push_back(i);
}
sort(vv.begin()+1, vv.end(), cmp);
queue<int>q;
q.push(0);
vis[0] = true;
for (int i = 1; i <= n; ++i)
{
int v = vv[i];
if (countdis(0, v) > 7.5 && countdis(0,v)<=jumpD+7.5)
{
q.push(v);
vis[v] = 1;
vec[v] = 0;
}
}
while (!q.empty())
{
int u = q.front();
q.pop();
if (isBorder(u))
return u;
for (int i = 1; i <= n; ++i)
{
if (vis[i] == false && jumpJudge(u, i))
{
vis[i] = true;
vec[i] = u;
q.push(i);
}
}
}
return -1;
}
int count(int ed)
{
int n = 1;
while (ed != 0)
{
ed = vec[ed];
n++;
}
return n;
}
void output(int i)
{
if (i == 0)
{
return;
}
output(vec[i]);
cout << node[i].x <<" "<< node[i].y << endl;
}
int main()
{
cin >> n >> jumpD;
node[0].x = node[0].y = 0;
for (int i = 1; i <= n; ++i)
{
cin >> node[i].x >> node[i].y;
}
int ed=bfs();
if (ed == -1)
cout << "0" << endl;
else
{
cout << count(ed) << endl;
output(ed);
}
return 0;
}