zoukankan      html  css  js  c++  java
  • POJ 3278 Catch That Cow

    http://poj.org/problem?id=3278

    BFS

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <queue>
     5 using namespace std;
     6 int N,K;
     7 bool visited_flag[100002]={false};
     8 bool check(int x)
     9 {
    10     if(0<=x&&x<=100000)
    11         return true;
    12     return false;
    13 }
    14 void bfs()
    15 {
    16     queue<int> Q;
    17     Q.push(N);
    18     Q.push(0);
    19     visited_flag[N]=true;
    20     int cur_pos;
    21     int cur_deep;
    22     while(!Q.empty()) {
    23         cur_pos=Q.front();
    24         Q.pop();
    25         cur_deep=Q.front();
    26         Q.pop();
    27         if(cur_pos==K) {
    28             printf("%d\n",cur_deep);
    29             return ;
    30         }
    31         if(check(cur_pos-1)&&!visited_flag[cur_pos-1]) {
    32             Q.push(cur_pos-1);
    33             Q.push(cur_deep+1);
    34             visited_flag[cur_pos-1]=true;
    35         }
    36         if(check(cur_pos+1)&&!visited_flag[cur_pos+1]) {
    37             Q.push(cur_pos+1);
    38             Q.push(cur_deep+1);
    39             visited_flag[cur_pos+1]=true;
    40         }
    41         if(check(2*cur_pos)&&!visited_flag[cur_pos*2]) {
    42             Q.push(cur_pos*2);
    43             Q.push(cur_deep+1);
    44             visited_flag[cur_pos*2]=true;
    45         }
    46     }
    47 }
    48 int main()
    49 {
    50     while(scanf("%d%d",&N,&K)!=EOF) {
    51         memset(visited_flag,false,sizeof(visited_flag));
    52         bfs();
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    BNU Online Judge-29140
    HDU-1022-Train Problem I
    HDU-1312-Red and Black
    BNU Online Judge-34978-汉诺塔
    BNU Online Judge-34976-数细菌
    BNU Online Judge-34973-Liserious战队
    HDU-1010-Tempter of the Bone
    HDU-1518-Square
    thinkphp笔记
    1210. 连号区间数
  • 原文地址:https://www.cnblogs.com/yangce/p/2938674.html
Copyright © 2011-2022 走看看