uva 11157 Dynamic Frog (贪心)

来源:互联网 发布:linux操作系统好用吗 编辑:程序博客网 时间:2024/04/28 03:42

uva 11157 Dynamic Frog   (贪心)


题目大义:一直青蛙跳过去再跳回来,路程中有大石头,小石头,小石头跳一下就掉下去了,大石头不会掉下去。问你来回的路程中,最大的一次跳跃距离多大?

解题思路:转换下题意,想象成两只青蛙同时往对岸跳,过程中有大石头能供两只青蛙同时站立,小石头只能被一只青蛙站立就掉下去,问两只青蛙最大的一次跳跃距离多大?

这个题意与题目是等价的,所以答案也相同。具体解法就是:

1)遇到大石头,两只青蛙同时跳到这个位置

2)遇到大石头之间的小石头,两只青蛙轮流使用站在小石头,若这个石头A青蛙跳,下个石头就得B青蛙跳,这样才能使最大跳跃最小(这个需要思考一下)。

3)更新过程中每次的跳跃距离,更新出最大的距离即答案

4)两岸(起点,终点)都可以看成大石头,可供两只青蛙站立。

代码如下:

 

/*********************uva 11157 Dynamic frogAccepted by tao_wangon 2013/3/8using 0.012s**********************/#include <iostream>#include <cstdio>#include <vector>using namespace std;const int maxn=110;struct stone{    int cnt;    int w;    stone(char ch=' ',int w0=0){        if(ch=='S') cnt=1;        else cnt=maxn;        w=w0;    }};int n,l,ans;vector <stone> v;void initial(){    v.clear();    ans=0;}void input(){     scanf("%d%d",&n,&l);     v.push_back(stone('B',0));     char ch1,ch2;     int tmp;     for(int i=0;i<n;i++){        cin>>ch1>>ch2>>tmp;        v.push_back(stone(ch1,tmp));     }     v.push_back(stone('B',l));}void computing(){     int pre1=0,now1=0;     int pre2=0,now2=0;     for(int i=0;i<v.size();i++){           if(v[i].cnt>1){               now1=v[i].w;               now2=v[i].w;               if(now1-pre1>ans) ans=now1-pre1;               if(now2-pre2>ans) ans=now2-pre2;               pre1=now1;               pre2=now2;           }           else{                if(pre2>pre1){                     now1=v[i].w;                     if(now1-pre1>ans) ans=now1-pre1;                     pre1=now1;                }                else{                     now2=v[i].w;                     if(now2-pre2>ans) ans=now2-pre2;                     pre2=now2;                }           }     }}void output(){     printf("%d\n",ans);}int main(){    int casen;    scanf("%d",&casen);    for(int i=1;i<=casen;i++){       initial();       input();       computing();       printf("Case %d: ",i);       output();    }    return 0;}


 


 

 

原创粉丝点击