• CCF1078奇怪的电梯


    这是一道dfs搜索题。(noi的题库测试数据有些水)

    已知每一层的步数,有两个方向(上下),求解到达终点的最少操作数。拿到这个题就发现是一个Dfs,于是便套了模板写代码。Wa了三次才AC。核心是:1.最早搜到结果的就是最少的操作数2每次有两个方向来搜索a[x]步,再判断新楼层是否存在即可。

    1.注意审题,注意无解的判断

    2.细节运算绝对不可以疏忽,不要“重复利用”

    3.搜索找好递归结束的条件

    4.搜索题找好儿子的枚举方向

    5.注意dfs第一个搜到的并不是最少的权,bfs是这样的


    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #define N 100001
    using namespace std;
    int a[N];
    int book[N];
    int minn=N;
    int n;
    int start,b;
    bool judge1(int x,int y){
        if(x+y>n) return false;
        else return true;
    }
    bool judge2(int x,int y){
        if(x-y<0) return false ;
        else return true;
    }
    bool judge3(int x){
        if(book[x]==0) return true;
        else return false;
    }
    bool flag=false;
    void dfs(int x,int step){
        if(x==b){
            minn=min(step,minn);
            flag=true;
        }
        else if(step<=minn){
                if(judge1(x,a[x])&&judge3(a[x]+x)) {//上
                    book[x]=1;
                    dfs(x+a[x],step+1);
                    book[x]=0;
                }
                if(judge2&&judge3(x-a[x])){//下
                    book[x]=1;
                    dfs(x-a[x],step+1);
                    book[x]=0;
                }
            }
        }
    int main(){
        cin>>n;
        cin>>start>>b;
        for(int i=1;i<=n;i++){
            cin>>a[i]; 
        }
        dfs(start,0);
        if(flag==false) cout<<-1;
        else cout<<minn;
        return 0;
    }
    待到oi十一月,我花开后百花杀。
  • 相关阅读:
    Introduction to debugging neural networks
    Faster R-CNN教程
    最长递增子序列
    321. Create Maximum Number 解题方法详解
    Ubuntu安装opencv with cuda
    转载:LeetCode:5Longest Palindromic Substring 最长回文子串
    64. Minimum Path Sum
    322. Coin Change
    148. Sort List
    微信浏览器禁止页面下拉查看网址(不影响页面内部scroll)
  • 原文地址:https://www.cnblogs.com/china-mjr/p/11232290.html
Copyright © 2020-2023  润新知