Created
March 22, 2014 18:58
-
-
Save qiuwei/9712447 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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