魔兽世界之一:备战

===============================================================================================

题目:

总时间限制: 1000ms    内存限制: 65536kB

描述

魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。 
红司令部,City 1,City 2,……,City n,蓝司令部

两军的司令部都会制造武士。武士一共有 dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值、攻击力这三种属性。 
双方的武士编号都是从1开始计算。红方制造出来的第n个武士,编号就是n。同样,蓝方制造出来的第n个武士,编号也是n。 
武士在刚降生的时候有一个生命值。 
在每个整点,双方的司令部中各有一个武士降生。 

红方司令部按照iceman、lion、wolf、ninja、dragon的顺序循环制造武士。 
蓝方司令部按照lion、dragon、ninja、iceman、wolf的顺序循环制造武士。 

制造武士需要生命元。 
制造一个初始生命值为m的武士,司令部中的生命元就要减少m个。 
如果司令部中的生命元不足以制造某个按顺序应该制造的武士,那么司令部就试图制造下一个。如果所有武士都不能制造了,则司令部停止制造武士。

给定一个时间,和双方司令部的初始生命元数目,要求你将从0点0分开始到双方司令部停止制造武士为止的所有事件按顺序输出。
一共有两种事件,其对应的输出样例如下: 

1) 武士降生 
输出样例: 004 blue lion 5 born with strength 5,2 lion in red headquarter
表示在4点整,编号为5的蓝魔lion武士降生,它降生时生命值为5,降生后蓝魔司令部里共有2个lion武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。

2) 司令部停止制造武士
输出样例: 010 red headquarter stops making warriors
表示在10点整,红方司令部停止制造武士

输出事件时: 
    首先按时间顺序输出; 
    同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。

输入:

    第一行是一个整数,代表测试数据组数。

    每组测试数据共两行。 
    第一行:一个整数M。其含义为, 每个司令部一开始都有M个生命元( 1 <= M <= 1000)。
    第二行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值。它们都大于0小于等于100。

输出:

    对每组测试数据,要求输出从0时0分开始,到双方司令部都停止制造武士为止的所有事件。
    对每组测试数据,首先输出"Case:n" n是测试数据的编号,从1开始 。
    接下来按恰当的顺序和格式输出所有事件。每个事件都以事件发生的时间开头,时间以小时为单位,有三位。

样例输入

1
20
3 4 5 6 7

样例输出

Case:1
000 red iceman 1 born with strength 5,1 iceman in red headquarter
000 blue lion 1 born with strength 6,1 lion in blue headquarter
001 red lion 2 born with strength 6,1 lion in red headquarter
001 blue dragon 2 born with strength 3,1 dragon in blue headquarter
002 red wolf 3 born with strength 7,1 wolf in red headquarter
002 blue ninja 3 born with strength 4,1 ninja in blue headquarter
003 red headquarter stops making warriors
003 blue iceman 4 born with strength 5,1 iceman in blue headquarter
004 blue headquarter stops making warriors

===============================================================================================

贴代码之前先扯两句。这个题算是“输出密集型”题目了。如果你有幸第一次做错了,不要紧,善良的北大给出了测试用例和对应的输出。

可是这对比输出也是一件烦人的事,可以用 linux shell 命令:

diff out.txt outz.txt

shell 就是牛,oh yeah!

===============================================================================================

我的代码:

#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <map>
#include <string>
using namespace std;

/*------------------------------------ class Warrior --------------------------------------*/
class Warrior{
private:
    string camp_;
    string species_;
    int id_;
public:
    Warrior(string cp, string sp, int id, 
    map<string, int> &W_hp, map<string,int> &C_cnt);
};
Warrior::Warrior(string cp, string sp, int id, map<string, int> &W_hp, map<string,int> &C_cnt){
    camp_ = cp;
    species_ = sp;
    id_ = id;
    cout << " " << camp_
         << " " << species_
         << " " << id_
         << " born with strength "
         << W_hp[species_] << "," << C_cnt[species_]
         << " " << species_
         << " in " << camp_ << " headquarter"
         << endl;
}
/*---------------------------------- class Camp  -----------------------------------------*/
class Camp{
private:
    string camp_;
    int C_hp_;
    map<string, int> cnt_;
public:
    static int clock_, ri, bi;
    static map <string, int> W_hp_;
    Camp(string camp, int C_hp);
    static void resetStatic(int *hp_arr);
    bool createWarrior();
};
map<string, int> initW_hp(){
    map<string, int> hpm;
    hpm["dragon"] = 0;
    hpm["ninja"]  = 0;
    hpm["iceman"] = 0;
    hpm["lion"]   = 0;
    hpm["wolf"]   = 0;
    return hpm;
}
int Camp::clock_ = 0;                        //it's a complex trick, got rememer this
int Camp::ri = 0;                            //static variable members initialization
int Camp::bi = 0;
map<string, int> Camp::W_hp_ = initW_hp();

Camp::Camp(string camp, int C_hp){
    camp_ = camp;
    C_hp_ = C_hp;
    cnt_["dragon"] = cnt_["ninja"]  = cnt_["iceman"]  = cnt_["lion"]  = cnt_["wolf"]  = 0;
}
void Camp::resetStatic(int *hp_arr){
    W_hp_["dragon"] = hp_arr[0];
    W_hp_["ninja"]  = hp_arr[1];
    W_hp_["iceman"] = hp_arr[2];
    W_hp_["lion"]   = hp_arr[3];
    W_hp_["wolf"]   = hp_arr[4];
    clock_ = ri = bi = 0;
}
/*-------------------------------- important method --------------------------------------*/
bool Camp::createWarrior(){
    bool retval = false;
    
    // output clock message
    cout << setfill('0') << setw(3) << clock_;
    
    // control create order
    if(camp_ == "red"){
        string redso[5] = {"iceman","lion","wolf","ninja","dragon"};
        int ri_old = ri;
        for(; ri<ri_old+5; ++ri){
            if(C_hp_ >= W_hp_[redso[ri%5]]){
                ++cnt_[redso[ri%5]];
                C_hp_ -= W_hp_[redso[ri%5]];
                Warrior z("red", redso[ri%5], clock_+1, W_hp_, cnt_);
                retval = true;
                ++ri; break;
            }else{
                continue;
            }
        }
        if(!retval)
            cout << " red headquarter stops making warriors" << endl;
    }else if(camp_ == "blue"){
        string blueso[5] = {"lion","dragon","ninja","iceman","wolf"};
        int bi_old = bi;
        for(; bi<bi_old+5; ++bi){
            if(C_hp_ >= W_hp_[blueso[bi%5]]){
                ++cnt_[blueso[bi%5]];
                C_hp_ -= W_hp_[blueso[bi%5]];
                Warrior z("blue", blueso[bi%5], clock_+1, W_hp_, cnt_);
                retval = true;
                ++bi; break;
            }else{
                continue;
            }
        }
        if(!retval)
        cout << " blue headquarter stops making warriors" << endl;
    }
    return retval;
}

/*---------------------------------------------------------------------------------------*/
int main(){
    freopen("in.txt", "r", stdin);
    freopen("outz.txt", "w", stdout);
    int grpNum, C_hp, w_hp[5];
    cin >> grpNum;
    for(int i=1; i<=grpNum; ++i){
        cin >> C_hp;
        for(int j=0; j<5; ++j){
            cin >> w_hp[j];
        }
        Camp::resetStatic(w_hp);
        Camp red("red", C_hp);
        Camp blue("blue", C_hp);
        cout << "Case:" << i << endl;
        bool rf, bf;
        rf = bf = 1;
        while(rf || bf){
            if(rf){
                rf = red.createWarrior();
            }
            if(bf){
                bf = blue.createWarrior();
            }
            Camp::clock_++;
        }
    }
    return 0;
}


转载于:https://my.oschina.net/matrixz/blog/211712

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值