PAT 数据结构 06-图4. Saving James Bond - Hard Version (30)

来源:互联网 发布:js可以控制浏览器比例 编辑:程序博客网 时间:2024/05/22 16:05

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 1510 -2110 21-40 1030 -5020 4035 100 -10-25 2240 -40-30 30-10 220 1125 2125 1010 1010 35-30 10
Sample Output 1:
40 1110 2110 35
Sample Input 2:
4 13-12 1212 12-12 -1212 -12
Sample Output 2:
0
在简单版本(上上篇博客)的基础上进行修改。
由于要输出路径,BFS将每个点push入队列时,要记录其前驱结点。这样当我们找到一个可行的终点时,就可以回溯输出序列。
为point结构体添加一个prePoint变量,表示BFS时的前驱结点。
/*2015.7.15cyq*/#include <iostream>#include <vector>#include <math.h>#include <queue>#include <fstream>#include <map>using namespace std;//ifstream fin("case1.txt");//#define cin finstruct point{float x;float y;bool canEscape;int prePoint;//BFS时的前驱结点,便于从终点回溯到起点,用于输出路径};int main(){int N,D;cin>>N>>D;if(D>=50){cout<<"1"<<endl;return 0;}vector<point> vexs(N);vector<vector<bool> > edges(N,vector<bool>(N,false));for(int i=0;i<N;i++){//鳄鱼序号为0到N-1cin>>vexs[i].x>>vexs[i].y;if(fabs(vexs[i].x)+D>=50||fabs(vexs[i].y)+D>=50){vexs[i].canEscape=true;  //标记能一步跳到岸边的点}elsevexs[i].canEscape=false;}for(int i=0;i<N;i++){for(int j=i+1;j<N;j++){float dx=vexs[i].x-vexs[j].x;float dy=vexs[i].y-vexs[j].y;if(dx*dx+dy*dy<=D*D){//标记能够两两连通的点edges[i][j]=true;edges[j][i]=true;}}}//BFSqueue<int> cur,next;vector<int> visited(N,false);map<float,int> firstJump;for(int i=0;i<N;i++){float tmp=vexs[i].x*vexs[i].x+vexs[i].y*vexs[i].y;if(tmp<=(D+7.5)*(D+7.5)){firstJump[tmp]=i;  //用map对第一跳的点的距离进行自动升序排序vexs[i].prePoint=-1;//前驱结点为-1,表示原点}}for(auto it=firstJump.begin();it!=firstJump.end();++it){cur.push((*it).second);//第一跳能跳到的点从小到大入队visited[(*it).second]=true;}bool find=false;vector<int> result;while(!cur.empty()){if(find)break;while(!cur.empty()){int root=cur.front();cur.pop();if(vexs[root].canEscape){while(vexs[root].prePoint!=-1){result.push_back(root);root=vexs[root].prePoint;}result.push_back(root);find=true;break;}for(int i=0;i<N;i++){if(edges[root][i]&&!visited[i]){next.push(i);visited[i]=true;vexs[i].prePoint=root;//每个结点入队时都要记录其前驱结点}}}swap(cur,next);}int n=result.size();if(n==0)cout<<"0"<<endl;else{cout<<n+1<<endl;for(int i=n-1;i>=0;i--)cout<<vexs[result[i]].x<<" "<<vexs[result[i]].y<<endl;}return 0;}

0 0
原创粉丝点击