P1135 奇怪的电梯

奇怪的电梯

题目背景

感谢 @yummy 提供的一些数据。

题目描述

呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第 i i i 层楼( 1 ≤ i ≤ N 1 \le i \le N 1iN)上有一个数字 K i K_i Ki 0 ≤ K i ≤ N 0 \le K_i \le N 0KiN)。电梯只有四个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如: 3 , 3 , 1 , 2 , 5 3, 3, 1, 2, 5 3,3,1,2,5 代表了 K i K_i Ki K 1 = 3 K_1=3 K1=3 K 2 = 3 K_2=3 K2=3,……),从 1 1 1 楼开始。在 1 1 1 楼,按“上”可以到 4 4 4 楼,按“下”是不起作用的,因为没有 − 2 -2 2 楼。那么,从 A A A 楼到 B B B 楼至少要按几次按钮呢?

输入格式

共二行。

第一行为三个用空格隔开的正整数,表示 N , A , B N, A, B N,A,B 1 ≤ N ≤ 200 1 \le N \le 200 1N200 1 ≤ A , B ≤ N 1 \le A, B \le N 1A,BN)。

第二行为 N N N 个用空格隔开的非负整数,表示 K i K_i Ki

输出格式

一行,即最少按键次数,若无法到达,则输出 -1

样例 #1

样例输入 #1

5 1 5
3 3 1 2 5

样例输出 #1

3

提示

对于 100 % 100 \% 100% 的数据, 1 ≤ N ≤ 200 1 \le N \le 200 1N200 1 ≤ A , B ≤ N 1 \le A, B \le N 1A,BN 0 ≤ K i ≤ N 0 \le K_i \le N 0KiN

本题共 16 16 16 个测试点,前 15 15 15 个每个测试点 6 6 6 分,最后一个测试点 10 10 10 分。

没完全剪枝的code:

#include<iostream>
#include<algorithm>
using namespace std;
int n;//有n层楼
int A;//从第A层开始
int B;//目标是到第B层
int q[100010];//每一层可以走到的地方
int res=1e9;
void dfs(int x,int cnt){//x是当前所在的楼层,cnt是按了cnt次按钮
    if(cnt>=res) return ;
    if(x==B){
        res=min(res,cnt);
        return ;
    }
    if(x+q[x]<=n){
        dfs(x+q[x],cnt+1);
    }
    if(x-q[x]>0){
        dfs(x-q[x],cnt+1);
    }
}
int main(){
    cin>>n>>A>>B;
    for(int i=1;i<=n;i++) cin>>q[i];
    dfs(A,0);
    if(res==1e9){
        cout<<-1;
        return 0;
    }
    cout<<res;
    return 0;
}

剪枝后但还会tle六个数据的code:

由于每层楼走一次和每层楼走好多次然后绕回去的方案要好,所以可以实现像组合型枚举一样的方案,每个楼层只走一次,并设置一个数组来标记它。

#include<iostream>
#include<algorithm>
using namespace std;
int n;//有n层楼
int A;//从第A层开始
int B;//目标是到第B层
int q[100010];//每一层可以走到的地方
int res=1e9;
bool st[100010];
void dfs(int x,int cnt){//x是当前所在的楼层,cnt是按了cnt次按钮
    if(cnt>=res) return ;
    if(x==B){
        res=min(res,cnt);
        return ;
    }
    st[x]=true;
    //上
    if(x+q[x]<=n&&!st[x+q[x]]){
        st[x+q[x]]=true;
        dfs(x+q[x],cnt+1);
        st[x+q[x]]=false;
    }
    //下
    if(x-q[x]>0&&!st[x-q[x]]){
        st[x-q[x]]=true;
        dfs(x-q[x],cnt+1);
        st[x-q[x]]=false;
    }
}
int main(){
    cin>>n>>A>>B;
    for(int i=1;i<=n;i++) cin>>q[i];
    dfs(A,0);
    if(res==1e9){
        cout<<-1;
        return 0;
    }
    cout<<res;
    return 0;
}

剪枝正解的code:

本题可以定义一个ans[N]数组来存储每次到s层时按了几次按钮,当按按钮次数这次比上次还多时,之后的情况就可以pass掉了。(将ans[N]数组初始化为无穷,如果是无穷则代表电梯到达不了,返回-1,如果不是则返回ans[B])

#include<iostream>
#include<cstring>//memset函数
using namespace std;
const int N = 205;
int n;
int ans[N];
int A;
int B;
int dis[N]={0};
void dfs(int s,int t)
{
    ans[s]=t;
    if(s+dis[s]<=n&&t+1<ans[s+dis[s]]) dfs(s+dis[s],t+1);
    if(s-dis[s]>0&&t+1<ans[s-dis[s]]) dfs(s-dis[s],t+1);
}
int main()
{
    memset(ans,0x3f,sizeof(ans));
    cin>>n>>A>>B;
    for(int i=1;i<=n;i++) cin>>dis[i];
    dfs(A,0);
    if(ans[B]!=0x3f3f3f3f) cout<<ans[B];
    else cout<<-1;
    return 0;
}
  • 18
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值