Skip to content

Instantly share code, notes, and snippets.

@qiuwei
Created March 22, 2014 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qiuwei/9712447 to your computer and use it in GitHub Desktop.
Save qiuwei/9712447 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cstdio>
using namespace std;
enum WarriorType{
DRAGON, NINJA, ICEMAN, LION, WOLF
};
static const char *warriorNames[] = {"dragon", "ninja", "iceman", "lion", "wolf"};
const char *getWarriorName(WarriorType wt){
return warriorNames[wt];
}
class HeadQuarter{
private:
string color;
int totalLife;
map<WarriorType, int> &lifespec;
vector<WarriorType> &orderspec;
map<WarriorType, int> currentNumWarriors;
int current;
int currentNum;
bool finished;
int getNext(int cur){
int totalwt = orderspec.size();
return (cur+1) % totalwt;
}
void createWarriorHelp(int trying){
if(lifespec[orderspec[trying]] <= totalLife){
WarriorType tryingwt= orderspec[trying];
currentNumWarriors[tryingwt]++;
totalLife -= lifespec[tryingwt];
current = getNext(trying);
cout << color << " " << getWarriorName(tryingwt) << " "<< ++currentNum <<" born with strength " << lifespec[tryingwt] << ",";
cout << getNumWarriors(tryingwt) << " " << getWarriorName(tryingwt) << " in " << color << " headquarter" << endl;
}else{
//fails
int nexttry = getNext(trying);
if(nexttry == current){
finish(); // already tried all types, fail
}else{
createWarriorHelp(nexttry);
}
}
}
public:
HeadQuarter(string _color, int _totalLife, map<WarriorType,int> &_lifespec, vector<WarriorType> &_orderspec):
color(_color), totalLife(_totalLife), lifespec(_lifespec), orderspec(_orderspec), currentNum(0), finished(false){
current = 0;
}
bool isFinished(){
return finished;
}
void finish(){
finished = true;
cout << color << " headquarter stops making warriors" << endl;
}
int getNumWarriors(WarriorType wt){
return currentNumWarriors[wt];
}
void createWarrior(int time){
printf("%03d ", time);
createWarriorHelp(current);
}
};
template <typename T, size_t N>
T* begin(T(&arr)[N]) { return &arr[0]; }
template <typename T, size_t N>
T* end(T(&arr)[N]) { return &arr[0]+N; }
void emulate(HeadQuarter &hq1, HeadQuarter &hq2){
int time = 0;
while(!hq1.isFinished() || !hq2.isFinished()){
if(!hq1.isFinished()) hq1.createWarrior(time);
if(!hq2.isFinished()) hq2.createWarrior(time);
time++;
}
}
int main()
{
int count = 0;
cin >> count;
WarriorType redo[] = {ICEMAN, LION, WOLF, NINJA, DRAGON};
vector<WarriorType> redOrder(begin(redo), end(redo));
WarriorType blueo[] = {LION, DRAGON, NINJA, ICEMAN, WOLF};
vector<WarriorType> blueOrder(begin(blueo), end(blueo));
WarriorType input[] = {DRAGON, NINJA, ICEMAN, LION, WOLF};
int totallife = 0;
int buf = 0;
map<WarriorType, int> lifespec;
for(int i = 1; i <= count; i++){
cout << "Case:" << i << endl;
cin >> totallife;
for(size_t j = 0; j < redOrder.size(); j++){
cin >> buf;
lifespec[input[j]] = buf;
}
HeadQuarter redHead(string("red"), totallife, lifespec, redOrder);
HeadQuarter blueHead(string("blue"), totallife, lifespec, blueOrder);
emulate(redHead, blueHead);
lifespec.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment