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 (≤), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the ( 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 ( 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 <stack> using namespace std; typedef struct{ int x,y; bool ifNearBank; bool ifConnected; bool ifVisited; }crocodile; crocodile data[120]; int n; int d; int times=0; int minTimes=20000; int path[120]; int minpath[120]; int minStartPointList[120]; int stNum=0; int bPath[120];//记录上一个是谁 int bLong[120];//记录终点离原点的距离 bool connectable(int a, int b){ int dis = (data[a].x - data[b].x)*(data[a].x - data[b].x) + (data[a].y - data[b].y)*(data[a].y - data[b].y); if(dis<=d*d) return true; else return false; } void copyTime(){ for(int i=0; i<minTimes; i++) minpath[i] = path[i]; } void dfs(int pos, bool &flag){ data[pos].ifVisited = true; path[times] = pos; times++; if(data[pos].ifNearBank){ flag = true; if(times<minTimes){ minTimes = times; copyTime(); } } for(int i=0; i<n; i++){ if(!data[i].ifVisited && connectable(i, pos)) dfs(i, flag); } times--; } void bfs(int pos, bool &flag){ queue<int> q; q.push(pos); data[pos].ifVisited = true; if(data[pos].ifNearBank){ flag = true; return; } while(!q.empty()){ pos = q.front(); q.pop(); for(int i=0; i<n; i++){ if(!data[i].ifVisited && connectable(i, pos)){ data[i].ifVisited = true; bPath[i] = pos; if(data[i].ifNearBank){ flag = true; } q.push(i); } } } } void calHowLong(){ for(int i=0; i<n; i++){ if(data[i].ifNearBank&&data[i].ifVisited){ int j=i; int k=1; while(bPath[j]!=j){ j=bPath[j]; k++; } bLong[i] = k; if(minTimes>k) minTimes=k; } } } int calStartPoint(int p){ while(bPath[p]!=p){ p=bPath[p]; }return p; } int cd(int i){ return data[i].x*data[i].x+data[i].y*data[i].y; } int main() { scanf("%d%d", &n, &d); if(d>=42.5){ printf("1\n"); return 0; } for(int i=0; i<n; i++){ scanf("%d%d", &data[i].x, &data[i].y); if(cd(i)<=56.25||data[i].x>=50||data[i].x<=-50||data[i].y>=50||data[i].y<=-50){ n--; i--; continue; } if(cd(i)<=(7.5+d)*(7.5+d)){ data[i].ifConnected = true; bPath[i]=i; minStartPointList[stNum] = i; for(int j=stNum; j>0; j--){ if(cd(minStartPointList[j]) < cd(minStartPointList[j-1])){ int temp; temp = minStartPointList[j]; minStartPointList[j] = minStartPointList[j-1]; minStartPointList[j-1] = temp; } } stNum++; } else data[i].ifConnected = false; if(data[i].x+d>=50||data[i].x-d<=-50||data[i].y+d>=50||data[i].y-d<=-50){ data[i].ifNearBank = true; bLong[i]=20000; } else data[i].ifNearBank = false; data[i].ifVisited = false; } bool ifEscape = false; for(int i=0; i<stNum; i++){ times = 0; bfs(minStartPointList[i], ifEscape); } if(ifEscape){ // calStartPoint(); calHowLong(); int finalPoint=-1; for(int i=0; i<n; i++){ if(minTimes == bLong[i]){ if(finalPoint>=0){ if(cd(calStartPoint(i))<cd(calStartPoint(finalPoint))){ finalPoint = i; } }else finalPoint = i; } } stack<int> fuck; fuck.push(finalPoint); while(bPath[finalPoint]!=finalPoint){ finalPoint=bPath[finalPoint]; fuck.push(finalPoint); } printf("%d\n", minTimes+1); while(!fuck.empty()){ printf("%d %d\n", data[fuck.top()].x, data[fuck.top()].y); fuck.pop(); } } else printf("0\n"); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!