ACdream HUT新生摸底训练赛 B - 娜娜梦游仙境系列——跳远女王 bfs

题意: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("******\n");
 64     }*/
 65     while(!qu.empty())
 66     {
 67         qunode tmp = qu.front(); 
 68         //printf("%d\n",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\n",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\n%d\n",tt);
127        else printf("NO\n%d\n",tt);
128     }
129 return 0;
130 }
View Code

 

转载于:https://www.cnblogs.com/zyue/p/4423040.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值