首页 新闻 会员 周边

奇怪的电梯C++

0
悬赏园豆:5 [已解决问题] 解决于 2020-09-26 11:40

#include<iostream>
using namespace std;

const int MAXN = 200 + 5;
int N, A, B;
bool vis[MAXN];        //    标记数组 
int updown[] = { 0, 1, -1};        //方向数组 (上、下) 
int floor[MAXN];    //每层楼上可以移动的数字 
struct flow
{
    int K;        // 所在楼层可以移动的数字 
    int f;        // 所在楼层 
    int step;    // 已走步数 
};
struct flow queue[MAXN];

void bfs(int a, int b) // 开搜 ! 
{
    int head = 0, tail = 1, t;    // 头, 尾, 移动后所在的楼层 
    queue[1].f = a;                // 以下为初始化 
    queue[1].step = 0;
    queue[1].K = floor[a];
    vis[a] = true;
    while(head < tail)        
    {
        head++;        
        for(int i = 1; i <= 2; i++)        // 枚举 上、下 两个方向 
        {
            t = queue[head].f + updown[i] * queue[head].K;        // 楼层移动 
            if(t >= 1 && t <= N && vis[t] == false)
            {
                tail++;         
                vis[t] = true;
                queue[tail].K = floor[t];
                queue[tail].f = t;        
                queue[tail].step = queue[head].step + 1;
                if(t == b) 
                {
                    cout << queue[tail].step << endl;
                    return;    
                }    
            }
        }
    }
    
    return;
}
int main()
{
    cin >> N >> A >> B;
    for(int i = 1; i <= N; i++)
    {
        cin >> floor[i];
    }
    bfs(A, B);
    
    return 0;
 }

这道题样例能过,但是只得了60分,不知道咋回事,求教,谢谢!

goalltheway的主页 goalltheway | 初学一级 | 园豆:121
提问于:2020-09-17 19:50
< >
分享
最佳答案
0

你的我研究下

答案代码:

#include <bits/stdc++.h>
using namespace std;
struct node
{
    int now;
    int cnt; 
}q,p;   

int vis[400010],n,x,y,a[200000];   

int main()
{
    queue<node>line;   
    cin>>n>>x>>y;  
    for (int i = 1; i <= n; i++)
        cin>>a[i];
    memset(vis,0,sizeof(vis));    
    p.now =  x;   
    p.cnt = 0;  
//  p.step = 1;
    line.push(p);   
    vis[p.now] = 1;   
    while (!line.empty())     
        {
            p = line.front(); 
            if (p.now == y)   
                {
                    cout<<p.cnt;   
                    return 0;    
                }
            p.cnt++;     
            if (p.now < y)       
                {
                    q = p;   
                    q.now += a[q.now];
                    if  (vis[q.now] == 0)    
                        {    
                            line.push(q); 
                            vis[q.now] = 1;
                        }       
                }        
            if (p.now - a[p.now] >= 1)
                {
                    q = p;            //给工具赋值
                    q.now -= a[q.now];     //进行操作
                    if (vis[q.now] == 0)          //操作的意义和上面的一样
                        {                 
                            line.push(q);
                            vis[q.now] = 1;
                        }
                }
            line.pop();
        }
    cout<<-1;
    return 0;
}
收获园豆:5
xmcword | 菜鸟二级 |园豆:211 | 2020-09-24 17:32

感谢

goalltheway | 园豆:121 (初学一级) | 2020-09-26 11:40

@goalltheway:
我知道了
你是不是没有考虑0的情况

假如输入
10 1 1
5 5 5 5 5 5 5 5 5 5
正确输出:
0
你的没有输出

xmcword | 园豆:211 (菜鸟二级) | 2020-09-26 20:03

@xmcword: 是的,-1也忘了,审题问题

goalltheway | 园豆:121 (初学一级) | 2020-09-26 20:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册