守望者闪烁题目解析

1.题目概要

恶魔猎手尤迪安野心勃勃.他背叛了暗夜精灵,率深藏在海底的那加企图叛变:守望者在与尤迪安的交锋中遭遇了围杀.被困在一个荒芜的大岛上。为了杀死守望者,尤迪安开始对这个荒岛施咒,这座岛很快就会沉下去,到那时,岛上的所有人都会遇难:守望者的跑步速度,为17m/s,以这样的速度是无法逃离荒岛的。庆幸的是守望者拥有闪烁法术,可在1s内移动60m,不过每次使用闪烁法术都会消耗魔法值10点。守望者的魔法值恢复的速度为4点/s,只有处在原地休息状态时才能恢复。

现在已知守望者的魔法初值M,他所在的初始位置与岛的出口之间的距离S,岛沉没的时间T。你的任务是写一个程序帮助守望者计算如何在最短的时间内逃离荒岛,若不能逃出,则输出守望者在剩下的时间内能走的最远距离。注意:守望者跑步、闪烁或休息活动均以秒(s)为单位。且每次活动的持续时间为整数秒。距离的单位为米(m)。

 

public class Watcher
{
    /**
     * 跑步速度
     */
    private final int runSpeed = 17;
    
    /**
     * 闪烁一次消耗魔法
     */
    private final int blinkConsume = 10;
    
    /**
     * 闪烁一次的距离
     */
    private final int blinkDistance = 60;
    
    /**
     * 原地恢复魔法速度
     */
    private final int recoverSpeed = 4;
    
    /**
     * 魔法值
     */
    private int magic;
    
    /**
     * 逃跑剩余时间
     */
    private int time;
    
    /**
     * 逃跑剩余距离
     */
    private int distance;
    
    /**
     * 构造函数
     */
    public Watcher()
    {
    }
    
    /**
     * @return the distance
     */
    public final int getDistance()
    {
        if (distance < 0)
        {
            distance = 0;
        }
        return distance;
    }
    
    /**
     * @param distance the distance to set
     */
    public final void setDistance(int distance)
    {
        this.distance = distance;
    }
    
    /**
     * @param magic the magic to set
     */
    public final void setMagic(int magic)
    {
        this.magic = magic;
    }
    
    /**
     * @return the magic
     */
    public final int getMagic()
    {
        return magic;
    }
    
    /**
     * @return the time
     */
    public final int getTime()
    {
        return time;
    }
    
    /**
     * @param time the time to set
     */
    public final void setTime(int time)
    {
        this.time = time;
    }
    
    /**
     * startEscape
     */
    public final void startEscape()
    {
        // 如果没结束,则不停的逃跑
        while (!isOver())
        {
            // 尝试闪烁
            if (canBlink())
            {
                blink();
            }
            // 判断是否只能跑步逃了
            else if (needRun())
            {
                run();
            }
            // 原地恢复魔法
            else
            {
                recover();
            }
        }
    }
    
    private final void recover()
    {
        magic += recoverSpeed;
        timing();
    }
    
    /**
     * 计时,调一次方法,走一秒
     */
    private void timing()
    {
        time--;
    }
    
    /**
     * @return 是否已结束
     */
    private boolean isOver()
    {
        return distance <= 0 || time <= 0;
    }
    
    private final void run()
    {
        distance -= runSpeed;
        timing();
        System.out.println("run"); //$NON-NLS-1$
    }
    
    private final void blink()
    {
        distance -= blinkDistance;
        magic -= blinkConsume;
        timing();
        System.out.println("blink"); //$NON-NLS-1$
    }
    
    private final boolean canBlink()
    {
        return isCoolDown() && magic >= blinkConsume;
    }
    
    private boolean isCoolDown()
    {
        return true;
    }
    
    private final boolean needRun()
    {
        if (!hasTimeToWaitForBlinking() || isRunBetter())
        {
            return true;
        }
        
        return false;
    }
    
    /**
     * 跑更好
     * 
     * @return boolean
     */
    private boolean isRunBetter()
    {
        int virtualBlinkDistance = distance;
        int virtualRunDistance = distance;
        int virtualMagic = magic;
        while (virtualBlinkDistance <= 0 || virtualRunDistance <= 0)
        {
            // virtual run
            virtualRunDistance -= runSpeed;
            
            // virtual recover
            virtualMagic += recoverSpeed;
            if (virtualMagic >= blinkConsume)
            {
                // virtual blink
                virtualBlinkDistance -= blinkDistance;
                
                // virtual run
                virtualRunDistance -= runSpeed;
                break;
            }
        }
        
        return virtualRunDistance < virtualBlinkDistance;
    }
    
    private boolean hasTimeToWaitForBlinking()
    {
        int recoverTime = 1;
        int virtualMagic = magic;
        while (virtualMagic < blinkConsume)
        {
            virtualMagic += recoverSpeed;
            recoverTime++;
        }
        
        return recoverTime == 1 ? true : time >= recoverTime;
    }
}

public class MainClass
{
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        Watcher watcher = new Watcher();
        final int s = 61;
        final int t = 3;
        watcher.setDistance(s);
        watcher.setMagic(10);
        watcher.setTime(t);
        watcher.startEscape();
        int time = watcher.getTime();
        if (time > 0)
        {
            System.out.println("escapeTime: " + (t - time)); //$NON-NLS-1$
        }
        else
        {
            System.out.println("escapeDistance: " + (s - watcher.getDistance())); //$NON-NLS-1$
        }
    }
}

你可能感兴趣的:(Java)