题意:bfs
解题思路:建图bfs,要特判一下能否直接跳。
解题思路:
1 // File Name: b.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月12日 星期日 20时03分02秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<queue> 24 #include<ctime> 25 #define LL long long 26 27 using namespace std; 28 int t; 29 vector<int>mp[10005]; 30 int n; 31 int d,yd; 32 int dis(double x1,double y1,double x2,double y2) 33 { 34 double ans = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); 35 if(ans <= d ) 36 return 1; 37 return 0 ; 38 } 39 struct node{ 40 int x, y; 41 }p[10005]; 42 int vis[10005]; 43 int ok ; 44 struct qunode{ 45 int num ,step ; 46 qunode(){} 47 qunode(int _num,int _step) 48 { 49 num = _num; 50 step = _step ; 51 } 52 }; 53 int bfs() 54 { 55 ok = 0 ; 56 queue<qunode> qu; 57 qu.push(qunode(0,0)); 58 int mians = yd; 59 /*for(int i = 1;i <= n;i ++) 60 { 61 for(int j = 0 ;j<mp[i].size();j ++) 62 printf("%d ",mp[i][j]); 63 printf("****** "); 64 }*/ 65 while(!qu.empty()) 66 { 67 qunode tmp = qu.front(); 68 //printf("%d ",tmp.num); 69 qu.pop(); 70 mians = min(mians,yd-p[tmp.num].y); 71 72 if(tmp.num == n+1) 73 { 74 ok =1; 75 return tmp.step; 76 } 77 for(int i = 0 ;i < mp[tmp.num].size(); i ++) 78 { 79 if(!vis[mp[tmp.num][i]]) 80 { 81 // printf("%d to %d ",tmp.num,mp[tmp.num][i]); 82 vis[mp[tmp.num][i]] = 1; 83 qu.push(qunode(mp[tmp.num][i],tmp.step+1)); 84 } 85 } 86 87 } 88 return mians; 89 } 90 int main(){ 91 scanf("%d",&t); 92 while(t--){ 93 ok = 0 ; 94 memset(vis,0,sizeof(vis)); 95 for(int i = 0;i <= 10000;i ++) 96 mp[i].clear(); 97 98 scanf("%d %d %d",&yd,&n,&d); 99 for(int i = 1;i <= n;i ++) 100 { 101 scanf("%d %d",&p[i].x,&p[i].y); 102 } 103 if(yd <= d ) 104 mp[0].push_back(n+1); 105 for(int i = 1;i <= n;i ++ ) 106 { 107 if(p[i].y <= d) 108 { 109 mp[0].push_back(i); 110 } 111 if(yd - p[i].y <= d) 112 { 113 mp[i].push_back(n+1); 114 } 115 for(int j = i+1;j <= n;j ++) 116 { 117 if(dis(p[i].x,p[i].y,p[j].x,p[j].y)) 118 { 119 mp[i].push_back(j) ; 120 mp[j].push_back(i); 121 } 122 } 123 } 124 int tt = bfs(); 125 if(ok) 126 printf("YES %d ",tt); 127 else printf("NO %d ",tt); 128 } 129 return 0; 130 }