• 洛谷P1135 奇怪的电梯 BFS例题


    好,这是一道黄题。几个月前(2017.10.29)的我拿了可怜的20分。

    这是当年的蒟蒻代码

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <cstdlib>
     6 using namespace std;
     7 
     8 int n,b,c;
     9 int a[201];
    10 
    11 int xiuxi(int now,int k)
    12 {
    13     if(k>200) return -1;
    14     int aa=9999,bb=9999;
    15     if(now == c) return k;
    16 
    17     if(now+a[now]<=n)
    18     {
    19         aa=xiuxi(now+a[now],k+1);
    20     }
    21     if(now-a[now]>0)
    22     {
    23         bb=xiuxi(now-a[now],k+1);
    24     }
    25     ///return ???;
    26 }
    27 
    28 int main()
    29 {
    30     scanf ("%d%d%d",&n,&b,&c);
    31     for(int i=1;i<=n;i++)
    32     {
    33         scanf ("%d",&a[i]);
    34     }
    35 
    36     int ans=xiuxi(b,0);
    37     printf("%d",ans);
    38     return 0;
    39 }
    蒟蒻代码

    首先,头文件瞎加#(滑稽),然后,搜索的时候没有处理好无限循环,只能靠200那个上限来return,导致了TLE。xiuxi函数写的一塌糊涂。也没有确定是最小值就输出,导致了个WA。我也懒得改了。

    今天(2018.02.23)重新看一看这个,就想到了预处理之后反着BFS。其实正着BFS也行。不知道我为什么脑子灵光一闪选择了反着。。

    先交了一次,RE+WA 60分。先随手把几个数组开大到400,然后在BFS里解决了一个判断是否空队列,莫名A了。

    这是AC代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 using namespace std;
     4 ///P1135
     5 int n,A,B,x;
     6 
     7 int p[402],ptop,ptail=1;
     8 bool vis[402];
     9 int step[402];
    10 
    11 struct floor{
    12     int top,from[202];
    13 }f[202];
    14 
    15 void bfs(int now)
    16 {
    17     for(int i=1;i<=f[now].top;i++)
    18     {
    19         if(vis[f[now].from[i]]) continue;
    20         vis[f[now].from[i]]=1;
    21         p[++ptop]=f[now].from[i];
    22         step[f[now].from[i]]=step[now]+1;
    23         if(f[now].from[i]==A) return;
    24     }
    25     if(ptop==ptail-1) return;
    26     ptail++;
    27     bfs(p[ptail-1]);
    28     return;
    29 }
    30 
    31 int main()
    32 {
    33     scanf ("%d%d%d",&n,&A,&B);
    34     for(int i=1;i<=n;i++)
    35     {
    36         scanf ("%d",&x);
    37         if(i-x>=1) f[i-x].from[++f[i-x].top]=i;
    38         if(i+x<=n) f[i+x].from[++f[i+x].top]=i;
    39     }
    40     memset(step,-1,sizeof(step));
    41 
    42     vis[B]=true;
    43     step[B]=0;
    44     p[++ptop]=B;
    45 
    46     bfs(B);
    47 
    48     printf("%d",step[A]);
    49     return 0;
    50 }
    AC代码

    手写队列,应该很好理解。可以拿来当BFS模板。这次去北京除了学了很多新知识以外,还意外的(意料之中的)提高了代码实现能力#(滑稽)。

    那么就这样吧。晚安,11015.

     1 #include <cstdio>
     2 #include <queue>
     3 const int N = 210;
     4 
     5 int n, b, a, k[N];
     6 bool vis[N];
     7 
     8 struct Sta {
     9     int floor, step;
    10     Sta(int f, int s) {
    11         this->floor = f;
    12         this->step = s;
    13     }
    14 };
    15 
    16 
    17 void BFS() {
    18     std::queue<Sta> Q;
    19     Q.push(Sta(a, 0));
    20     vis[a] = 1;
    21     while(!Q.empty()) {
    22         Sta s = Q.front();
    23         Q.pop();
    24         if(s.floor == b) {
    25             printf("%d", s.step);
    26             return;
    27         }
    28         int p = s.floor + k[s.floor];
    29         if(p > 0 && p <= n && !vis[p]) {
    30             vis[p] = 1;
    31             Q.push(Sta(p, s.step + 1));
    32         }
    33         p = s.floor - k[s.floor];
    34         if(p > 0 && p <= n && !vis[p]) {
    35             vis[p] = 1;
    36             Q.push(Sta(p, s.step + 1));
    37         }
    38     }
    39     printf("-1");
    40     return;
    41 }
    42 
    43 int main() {
    44     scanf("%d%d%d", &n, &a, &b);
    45 
    46     for(int i = 1; i <= n; i++) {
    47         scanf("%d", &k[i]);
    48     }
    49 
    50     BFS();
    51 
    52     return 0;
    53 }
    2018儿童节特供:全新AC代码,仅需12分钟!
  • 相关阅读:
    Luogu p2672 推销员 (贪心)
    HDU 1548 A strange lift (暴力搜索)
    HDU 1258 Sum it up (dfs)
    HDU 1087 FatMouse and Cheese (记忆化搜索)
    Beautiful Numbers(牛客)
    E. Product Oriented Recurrence(矩阵快速幂+欧拉降幂)
    Super A^B mod C FZU
    bitset入门 简单瞎搞题
    D. Dirty Deeds Done Dirt Cheap(思维)
    大家一起来数二叉树吧(DP)
  • 原文地址:https://www.cnblogs.com/huyufeifei/p/8462915.html
Copyright © 2020-2023  润新知