PAT 数据结构 06-图4. Saving James Bond - Hard Version (30)
2015-07-15 20:11
681 查看
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
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
在简单版本(上上篇博客)的基础上进行修改。
由于要输出路径,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 fin struct 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-1 cin>>vexs[i].x>>vexs[i].y; if(fabs(vexs[i].x)+D>=50||fabs(vexs[i].y)+D>=50){ vexs[i].canEscape=true; //标记能一步跳到岸边的点 }else vexs[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; } } } //BFS queue<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; }
相关文章推荐
- 数据结构 树状数组
- 【自考学习】——《数据结构导论》概览
- PAT 数据结构 05-图3. 六度空间 (30)
- 算法导论 第十章:基本数据结构
- 图的存储
- PAT 数据结构 05-图2. Saving James Bond - Easy Version (25)
- VoiceEngine Codec数据结构
- PAT 数据结构 05-图1. List Components (25) 深度搜索DFS和广度搜索BFS
- C语言-数据结构(一)
- 数据结构-图
- 数据结构之---C语言实现图的数组(邻接矩阵)存储表示
- 数据结构之---C语言实现图的邻接表存储表示
- PAT 数据结构 09-散列3. Hashing - Hard Version (30) 拓扑排序
- 从淘宝数据结构来看电子商务中商品属性设计
- 算法的时间复杂度
- 数据结构基础 图的遍历(三) 之 BFS+DFS(非递归实现)
- oracle数据结构sql语句
- PAT 数据结构 09-散列2. QQ帐户的申请与登陆(25)
- 【数据结构】位运算
- PAT 数据结构 09-散列1. Hashing (25)