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<cstdio> #include<queue> #include<stack> #include<cmath> #include<iostream> #include<algorithm> using namespace std; const int maxn = 110; const int minLen = 50 - 15.0/2; struct Point { int x,y; }point[maxn]; int path[maxn] = {0}; bool vis[maxn] = {0}; int n,d; void BFS(); void init(int b[]); bool cmp(int x,int y); int FirstJump(int v); bool isSafe(int v); bool Jump(int v1,int v2); int main() { scanf("%d%d",&n,&d); for (int i = 0; i < n; i++) { scanf("%d%d",&point[i].x,&point[i].y); } if (d >= minLen) { printf("1\n"); } else { BFS(); } return 0; } void BFS() { int b[maxn]; init(b); sort(b,b+n,cmp); queue<int> q; int last; int tail; int step = 2; for (int i = 0; i < n; i++) { if(FirstJump(b[i])) { q.push(b[i]); vis[b[i]] = true; last = b[i]; } } while(!q.empty()) { int now = q.front(); q.pop(); if (isSafe(now)) { int k =1; stack<int> s; cout << step << endl; while (k < step) { s.push(now); now = path[now]; k++; } while (!s.empty()) { now = s.top(); s.pop(); cout << point[now].x << " " << point[now].y << endl; } return; } for (int i = 0; i < n; i++) { if (!vis[i] && Jump(now,i)) { q.push(i); vis[i] = true; tail = i; path[i] = now; } } if (last == now) { last = tail; step++; } } if (q.empty()) { cout << "0" << endl; } } void init(int b[]) { for (int i = 0; i < n; i++) { b[i] = i; } } bool cmp(int x,int y) { return FirstJump(x) < FirstJump(y); } int FirstJump(int v) { int dis = pow(point[v].x,2) + pow(point[v].y,2); int dis_Jump = pow(15.0/2 + d,2); if (dis <= dis_Jump) { return dis; } else { return 0; } } bool isSafe(int v) { int dis_safe = 50 - d; return abs(point[v].x) >= dis_safe || abs(point[v].y) >= dis_safe; } bool Jump(int v1,int v2) { int dis_x = pow(point[v1].x-point[v2].x, 2); int dis_y = pow(point[v1].y-point[v2].y, 2); if (dis_x + dis_y <= d*d) { return true; } else { return false; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)