07-图5 Saving James Bond
Posted 柚子z
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了07-图5 Saving James Bond相关的知识,希望对你有一定的参考价值。
题面:
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; }
以上是关于07-图5 Saving James Bond的主要内容,如果未能解决你的问题,请参考以下文章
07-图5 Saving James Bond - Hard Version(30 分)
PTA 07-图5 Saving James Bond - Hard Version (30分)
Saving James Bond - Easy Version 原创 2017年11月23日 13:07:33
05-图2. Saving James Bond - Easy Version (25)