守望者的逃离
题目:
恶魔猎手尤迫安野心勃勃.他背叛了暗夜精灵,率深藏在海底的那加企图叛变:守望者在与尤迪安的交锋中遭遇了围杀.被困在一个荒芜的大岛上。为了杀死守望者,尤迪安开始对这个荒岛施咒,这座岛很快就会沉下去,到那时,刀上的所有人都会遇难:守望者的跑步速度,为17m/s, 以这样的速度是无法逃离荒岛的。庆幸的是守望者拥有闪烁法术,可在1s内移动60m,不过每次使用闪烁法术都会消耗魔法值10点。守望者的魔法值恢复的速度为4点/s,只有处在原地休息状态时才能恢复。
现在已知守望者的魔法初值M,他所在的初始位置与岛的出口之间的距离S,岛沉没的时间T。你的任务是写一个程序帮助守望者计算如何在最短的时间内逃离荒岛,若不能逃出,则输出守望者在剩下的时间内能走的最远距离。注意:守望者跑步、闪烁或休息活动均以秒(s)为单位。且每次活动的持续时间为整数秒。距离的单位为米(m)。
输入描述
Input
输入仅一行,包括空格隔开的三个非负整数M,S,T。
输出描述
Output
输出包含两行:
第1行为字符串"Yes"或"No" (区分大小写),即守望者是否能逃离荒岛。
第2行包含一个整数,第一行为"Yes" (区分大小写)时表示守望着逃离荒岛的最短时间
第一行为"No" (区分大小写) 时表示守望者能走的最远距离。
样本输入
Input example
39 200 4
样本输出
Output example
No
197
解答:
#include<vector>
#include<stdio.h>
using namespace std;
int time, run, m;
int maxrun=0;
int mintime=-1;
class node{
public:
int x; //里程
int m; //法力
int t; //时间
vector<node> child; //孩子结点
node(){}
node(int X, int M, int T){ //构造函数
x = X;
m = M;
t = T;
}
void addchild(){
if(mintime!=-1){
if (this->t < mintime && this->x < run){
if (m < 10){
addchild0();
addchild17();
}
else addchild60();
}
}
else{
if (this->t < time && this->x < run){
if (m < 10){
addchild0();
addchild17();
}
else addchild60();
}
}
}
void addchild0(){
node child0(this->x, this->m + 4, this->t + 1);
//if (child0.x >= run&&child0.t<mintime) mintime = child0.t;
if(child0.x>=run){
if(mintime<0) mintime=child0.t;
else if(mintime>child0.t) mintime=child0.t;
}
if ((child0.t==time)&&child0.x>maxrun) maxrun = child0.x;
child0.addchild();
child.push_back(child0);
//printf("t=%d x=%d m=%d\n", child.back().t, child.back().x, child.back().m);
}
void addchild17(){
node child17(this->x+17, this->m, this->t + 1);
if(child17.x>=run){
if(mintime<0) mintime=child17.t;
else if(mintime>child17.t) mintime=child17.t;
}
if ((child17.t==time)&&child17.x>maxrun) maxrun = child17.x;
child17.addchild();
child.push_back(child17);
//printf("t=%d x=%d m=%d\n", child.back().t, child.back().x, child.back().m);
}
void addchild60(){
node child60(this->x+60, this->m - 10, this->t + 1);
if(child60.x>=run){
if(mintime<0) mintime=child60.t;
else if(mintime>child60.t) mintime=child60.t;
}
if ((child60.t==time)&&child60.x>maxrun) maxrun = child60.x;
child60.addchild();
child.push_back(child60);
//printf("t=%d x=%d m=%d\n", child.back().t, child.back().x, child.back().m);
}
};
int main()
{
int RUN,T;
RUN=T=0;
scanf("%d %d %d", &m, &run, &time);
while(m>=10&&time-T>0){
m-=10;
RUN+=60;
T++;
}
while(run-RUN>=120&&time-T>=7){
RUN+=120;
switch(m){
case 9:{
m=1;
T+=5;
break;
}
case 8:{
m=0;
T+=5;
break;
}
case 7:{
m=3;
T+=6;
break;
}
case 6:{
m=2;
T+=6;
break;
}
case 5:{
m=1;
T+=6;
break;
}
case 4:{
m=0;
T+=6;
break;
}
case 3:{
m=3;
T+=7;
break;
}
case 2:{
m=2;
T+=7;
break;
}
case 1:{
m=1;
T+=7;
break;
}
case 0:{
m=0;
T+=7;
break;
}
}
}
//node start(0, m, 0);
node start(RUN,m,T);
if(RUN>=run) mintime=T;
else if(T>=time) maxrun=RUN;
else start.addchild();
if (mintime!=-1)
printf("Yes\n%d\n",mintime); //printf("能逃出,最短时间为%d秒",mintime);
else printf("No\n%d\n",maxrun);//else printf("不能逃出,在剩余时间内最多跑%d米\n", runmax[time]);
return 0;
}