Elevator Trouble

10 篇文章 0 订阅
3 篇文章 0 订阅
                                                                                             Elevator Trouble
You are on your way to your first job interview as a program tester, and you are already late. The interview is in a skyscraper and you are currently in floor s, where you see an elevator. Upon entering the elvator, you learn that it has only two buttons, marked "UP u" and "DOWN d". You conclude that the UP-button takes the elevator u floors up (if there aren't enough floors, pressing the UP-botton does nothing, or at least so you assume), whereas the DOWN-button takes you d stories down (or none if there aren't enough). Knowing that the interview is at floor g, and that there are only f floors in the building, you quickly decide to write a program that gives you the amount of button pushes you need to perform. If you simply cannot reach the correct floor, your program halts with the message "use the stairs".
Given input f, s, g, u and d (floors, start, goal, up, down), find the shortest sequence of button presses you must press in order to get from s to g, given a building of f floors, or output "use the stairs" if you cannot get from s to g by the given elevator.

 

Input

The input will consist of one line, namely f s g u d, where 1 <= s, g <= f <= 1000000 and 0 <= u, d <= 1000000. The floors are one-indexed, i.e. if there are 10 stories, s and g be in [1, 10].

Output

You must reply with the minimum numbers of pushes you must make in order to get from s to g, or outputuse the stairs if it is impossible given the configuration of the elvator.

Sample Input

Sample Input 1
10 1 10 2 1

Sample Input 2
100 2 1 1 0

Sample Output

Sample Output 1
6

Sample Output 2
use the stairs

Source

Nordic Collegiate Programming Contest 2011



本题可以模拟过,不过我也不知道是不是水过去的...
模拟的话 可以先判定二元方程是否有解 再去模拟
还有u=0 和d=0的时候分别特判
代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int   f,s,g,u,d;
    while(scanf("%d%d%d%d%d",&f,&s,&g,&u,&d)!=EOF)
    {
        if(s==g)
        {
            printf("0\n");
            continue;
        }
        if(u==0&&d!=0)
        {
            if((s-g)>0&&(s-g)%d==0)
            printf("%d\n",(s-g)/d);
            else
            printf("use the stairs\n");
            continue;
        }
        else if(d==0&&u!=0)
        {
            if((g-s)>0&&(g-s)%u==0)
            printf("%d\n",(g-s)/u);
            else
            printf("use the stairs\n");
            continue;
        }
        else if(d==0&&u==0)
        {
            printf("use the stairs\n");
            continue;
        }
        int num=gcd(u,d);
        if((g-s)%num!=0)
        {
            printf("use the stairs\n");
            continue;
        }
        int counts=0;
        while(s!=g)
        {
            if(s+u>f&&s-d<1)
            break;
            if(s+u<=f&&s<=g)
            s+=u;
            else if(s-d<1&&s+u<=f)
            s+=u;
            else
            s-=d;
            counts++;
        }
        if(s==g)
        printf("%d\n",counts);
        else
        printf("use the stairs\n");
    }
}

第二种方法就是bfs了,比赛的时候为什么就没想到了,哎
引以为鉴
代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int hashs[1000005];
int main()
{
    int   f,s,g,u,d;
    while(scanf("%d%d%d%d%d",&f,&s,&g,&u,&d)==5)
    {
        queue<int> stacks;
        if(s==g)
        {
            printf("0\n");
            continue;
        }
        memset(hashs,0,sizeof(hashs));
        stacks.push(s);
        int ok=0;
        int up,down,num;
        while(!stacks.empty())
        {
             num=stacks.front();
             stacks.pop();
             up=num+u;
             down=num-d;
            if(up<=f&&up!=s&&hashs[up]==0)
            stacks.push(up), hashs[up]=hashs[num]+1;
            if(down>=1&&down!=s&&hashs[down]==0)
            stacks.push(down),hashs[down]=hashs[num]+1;
            if(up==g)
            {
                ok=1;
                break;
            }
            if(down==g)
            {
                ok=2;
                break;
            }
        }
        if(ok==1)
        printf("%d\n",hashs[up]);
        else if(ok==2)
        printf("%d\n",hashs[down]);
        else
        printf("use the stairs\n");
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值