zoukankan      html  css  js  c++  java
  • hdoj 2717 Catch That Cow

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717

    解题思路:BFS 搜索

     1 ///////////////////////////////////////////////////////////////////////////
     2 //problem_id: hdoj 2717
     3 //user_id: SCNU20102200088
     4 ///////////////////////////////////////////////////////////////////////////
     5 
     6 #include <algorithm>
     7 #include <iostream>
     8 #include <iterator>
     9 #include <iomanip>
    10 #include <cstring>
    11 #include <cstdlib>
    12 #include <string>
    13 #include <vector>
    14 #include <cstdio>
    15 #include <cctype>
    16 #include <cmath>
    17 #include <queue>
    18 #include <stack>
    19 #include <list>
    20 #include <set>
    21 #include <map>
    22 using namespace std;
    23 
    24 ///////////////////////////////////////////////////////////////////////////
    25 typedef long long LL;
    26 const double PI=acos(-1.0);
    27 
    28 const int x4[]={-1,0,1,0};
    29 const int y4[]={0,1,0,-1};
    30 const int x8[]={-1,-1,0,1,1,1,0,-1};
    31 const int y8[]={0,1,1,1,0,-1,-1,-1};
    32 
    33 typedef int T;
    34 T max(T a,T b){ return a>b? a:b; }
    35 T min(T a,T b){ return a<b? a:b; }
    36 ///////////////////////////////////////////////////////////////////////////
    37 
    38 ///////////////////////////////////////////////////////////////////////////
    39 //Add Code:
    40 int n,k,ans;
    41 bool flag[200005];
    42 
    43 struct Node{
    44     int id,step;
    45     Node(int i,int s):id(i),step(s){}
    46 };
    47 
    48 void BFS(){
    49     flag[n]=1;
    50     if(n==k){
    51         ans=0;
    52         return ;
    53     }
    54     Node node(n,0);
    55     queue<Node> q;
    56     q.push(node);
    57     while(!q.empty()){
    58         Node temp=q.front();
    59         q.pop();
    60         int id[]={temp.id-1,temp.id+1,temp.id*2},step=temp.step+1;
    61         for(int p=0;p<3;p++){
    62             if(p==0 && (id[p]<0 || flag[id[p]])) continue;
    63             if(p>0 && (temp.id>k || flag[id[p]])) continue;
    64             flag[id[p]]=1;
    65             if(id[p]==k){
    66                 ans=step;
    67                 while(!q.empty()) q.pop();
    68                 return ;
    69             }
    70             Node res(id[p],step);
    71             q.push(res);
    72         }
    73     }
    74 }
    75 ///////////////////////////////////////////////////////////////////////////
    76 
    77 int main(){
    78     ///////////////////////////////////////////////////////////////////////
    79     //Add Code:
    80     while(scanf("%d%d",&n,&k)!=EOF){
    81         BFS();
    82         memset(flag,0,sizeof(flag));
    83         printf("%d
    ",ans);
    84     }
    85     ///////////////////////////////////////////////////////////////////////
    86     return 0;
    87 }
    88 
    89 ///////////////////////////////////////////////////////////////////////////
    90 /*
    91 Testcase:
    92 Input:
    93 5 17
    94 Output:
    95 4
    96 */
    97 ///////////////////////////////////////////////////////////////////////////
  • 相关阅读:
    利用Python爬取疫情数据并使用可视化工具展示
    接口与多态
    defer 延迟调用
    Spring缓存注解
    SpringAOP
    Mybatis动态SQL语句
    SpringMVC
    Spring Boot入门
    vue中使用JS实现倒计时功能
    springboot使用aop注解在controller层打印请求和响应报文
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3282921.html
Copyright © 2011-2022 走看看