07-图5 Saving James Bond
来源:互联网 发布:深度软件安装管理器 编辑:程序博客网 时间:2024/05/21 08:36
//不用单独定义节点,而是用二维数组代替。 二维数组的内部是一维数组,x坐标,y坐标,flag是否被访问过 //3个向量数组,first(n+1),path(n+1,-1),dist(n+1),path初始化为-1,表示未访问过//path要定义为全局使用 #include<iostream> #include<cmath> //abs和pow#include<algorithm> //sqrt#include<vector>#include<queue>#define MAXSIZE 101 const double R=7.5; using namespace std; //以下三项是全局变量,免得BFS每次都重新传入double vertex[MAXSIZE][3];int n, v;//v是pop出来的 double m; bool flag=false;vector<int> path(n+1,-1); bool cmp(int i,int j){return pow(vertex[i][0],2)+pow(vertex[i][1],2) < pow(vertex[j][0],2)+pow(vertex[j][1],2);} bool BFS(){ queue<int> q; vector<int> first(n+1); int i,fir,firstdist;//1、对第一跳的处理 for(int i=0;i<=n;i++) first[i]=i;sort(first.begin(),first.end(),cmp);/*for(int i=0;i<=n;i++){cout<<first[i]<<" ";}cout<<endl;return 1; */for(int i=1;i<=n;i++){fir=first[i];firstdist=sqrt( pow(vertex[fir][0],2)+pow(vertex[fir][1],2) );if( firstdist>R && firstdist<=R+m ){ //能跳出中心圆盘,但又不会跳上岸 q.push(fir); path[fir]=0; vertex[fir][2]=1;//表示访问过 }} //2、循环出队-进队 while(!q.empty()){ v=q.front(); q.pop(); if((fabs(vertex[v][0]) + m >= 50 || fabs(vertex[v][1]) + m >= 50)){//每次跳跃前判断是否能直接上岸 abs和fabs??? //cout<<"v="<<v<<endl; flag=true; break; }else{ for(int i=1;i<=n;i++){ if( vertex[i][2]==0 && pow(vertex[v][0]-vertex[i][0],2)+pow(vertex[v][1]-vertex[i][1],2)<=pow(m,2) ){ q.push(i); path[i]=v; //cout<<"path["<<i<<"]="<<v<<" "; vertex[i][2]=1;//表示访问过 } } } }return flag;} int main(){ //freopen("input.txt","r",stdin); cin>>n>>m; vertex[0][0]=0; vertex[0][1]=0;vertex[0][2]=0; for(int i=1;i<=n;i++){ ////1~n,0为原点 cin>>vertex[i][0]>>vertex[i][1]; vertex[i][2]=0;} if(R+m>=50) flag=true;//直接从岛跨上岸 else flag=BFS(); if(!flag) cout<<"0"<<endl;else{vector<int> dist;while(v){//cout<<"v="<<v<<" ";dist.push_back(v);v=path[v];}cout<<dist.size()+1<<endl;//最后一步直接跳上岸 for(int i=dist.size()-1;i>=0;i--){ //注意:dist.size()-1而不是dist.size() cout<<vertex[dist[i]][0]<<" "<<vertex[dist[i]][1]<<endl;} } return 0; }
阅读全文
1 0
- 07-图5 Saving James Bond
- 07-图5 Saving James Bond
- 07-图5 Saving James Bond
- 07-图5 Saving James Bond
- 07-图5 Saving James Bond
- 07-图5 Saving James Bond
- 07-图5 Saving James Bond
- 07-图5 Saving James Bond
- 07-图5 Saving James Bond - Hard Version
- MOOC PTA 07-图5 Saving James Bond - Hard Version
- 07-图5 Saving James Bond - Hard Version
- 07-图5 Saving James Bond - Hard Version
- 07-图5 Saving James Bond - Hard Version
- 07-图5 Saving James Bond - Hard Version (30分)
- 5-10 Saving James Bond
- 07-图4. Saving James Bond - Hard Version (30)
- 06-图2 Saving James Bond
- 06-图2 Saving James Bond
- 功耗优化
- 【Shell】记录shell的应用例子
- 新型机器学习算法:正则化理解
- Face Recognition
- 水仙花数(for循环写法)
- 07-图5 Saving James Bond
- 线段树求周长 HDU 1177
- jmeter3.0 以上生成报告
- arcgis for android 地图上画框划线
- [CF538F]A Heap of Heaps
- 木桶排序-扑克牌
- 安装jupyter notebook方法
- 设计模式:简单工厂
- arduino教程