#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define WARRIOR_NUM 5
/*
dragon 、ninja、iceman、lion、wolf
*/
class CHeadquarter;
class CWarrior {
private:
int ID; // 英雄编号
int kind; // 英雄种类
CHeadquarter * pheadquarter;
public:
static char * warrior_name[WARRIOR_NUM];
static int initial_health[WARRIOR_NUM];
CWarrior(int id, int kind_, CHeadquarter * p);
void print_info(int time);
};
class CHeadquarter {
private:
int total_energy;
int total_warriors;
int color; // 0-red 1-blue
int next_warrior; // 接下来要生产的英雄序号
bool is_stopped; // 是否停止
int warriors_count[WARRIOR_NUM];
CWarrior * pWarriors[1000];
public:
friend class CWarrior;
static int making_seq[2][WARRIOR_NUM]; //双方生产英雄的顺序
~CHeadquarter();
void init(int color, int energy);
bool produce(int time);
void get_color(char * color);
};
// CWarrior
char * CWarrior::warrior_name[WARRIOR_NUM] = {"dragon", "ninja", "iceman", "lion", "wolf"};
int CWarrior::initial_health[WARRIOR_NUM];
CWarrior::CWarrior(int id, int kind_, CHeadquarter *p):ID(id), kind(kind_), pheadquarter(p) {}
//004 blue lion 5 born with strength 5,2 lion in red headquarter
void CWarrior::print_info(int time) {
char color[10];
pheadquarter->get_color(color);
printf("%03d %s %s %d born with strength %d,%d %s in %s headquarter\n",
time, color, warrior_name[kind], ID + 1, initial_health[kind],
pheadquarter->warriors_count[kind], warrior_name[kind], color);
}
//CHeadquater
int CHeadquarter::making_seq[2][WARRIOR_NUM] = {{2, 3, 4, 1, 0}, {3, 0, 1, 2, 4}};
void CHeadquarter::init(int c, int energy) {
total_energy = energy;
color = c;
total_warriors = 0;
is_stopped = false;
next_warrior = 0;
for (int i = 0; i < WARRIOR_NUM; i++)
warriors_count[i] = 0;
}
void CHeadquarter::get_color(char * buf) {
if (color == 0)
strcpy(buf, "red");
else if (color == 1)
strcpy(buf, "blue");
}
CHeadquarter::~CHeadquarter() {
for (int i = 0; i < total_warriors; i++)
delete pWarriors[i];
}
bool CHeadquarter::produce(int time) {
if (is_stopped) return false;
int count = 0, ne;
while(count < WARRIOR_NUM) {
ne = making_seq[color][next_warrior];
if (CWarrior::initial_health[ne] <= total_energy) break;
next_warrior = (next_warrior + 1) % WARRIOR_NUM;
count++;
}
if (count == WARRIOR_NUM) {
is_stopped = true;
//003 red headquarter stops making warriors
if (color == 0)
printf("%03d red headquarter stops making warriors\n", time);
else
printf("%03d blue headquarter stops making warriors\n", time);
return false;
}
total_energy -= CWarrior::initial_health[ne];
pWarriors[total_warriors] = new CWarrior(total_warriors, ne, this);
warriors_count[ne]++;
pWarriors[total_warriors]->print_info(time);
total_warriors++;
next_warrior = (next_warrior + 1) % WARRIOR_NUM;
return true;
}
int main() {
int n, m;
scanf("%d", &n);
CHeadquarter red, blue;
for (int i = 1; i <= n; i++) {
printf("Case:%d\n", i);
scanf("%d", &m);
red.init(0, m);
blue.init(1, m);
for (int j = 0; j < WARRIOR_NUM; j++)
scanf("%d", &CWarrior::initial_health[j]);
bool flg_r = true, flg_b = true;
int nTime = 0;
while (flg_r || flg_b) {
if (!red.produce(nTime)) flg_r = false;
if (!blue.produce(nTime)) flg_b = false;
nTime++;
}
}
return 0;
}
04-06
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-21
07-13
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交