欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Coursera-PKU程序设计与算法-C++程序设计-魔兽终极版

程序员文章站 2022-03-09 15:24:20
...

编程题#4: 魔兽世界终极版
注意: 总时间限制: 2000ms 内存限制: 65536kB

AC
(题干略)

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

class Headquarter {
public:
	Headquarter() { cityawardr = 0; cityawardb = 0; redtob = 0; bluetor = 0; }
	int Mr, Mb, cityawardr, cityawardb, redtob, bluetor;
};

class warrior {
public:
	warrior() {
		info.step = 0; info.loyalty = 0; info.sword = 0; info.arrow = 0; info.bomb = 0; info.sq = 0.00;
	}
	struct Info{
		int force, life, step, loyalty, sword, arrow, bomb;
		double sq;
		string name;
	};
	int n;
	Info info;
};

class Binfo {
public:
	int life, force;
};

class city {
public:
	city() { info.life = 0; info.red = 0, info.blue = 0, info.flagr = 0, info.flagb = 0; info.flag = '0'; }
	struct Info {
		int life, red, blue, flagr, flagb;
		char flag;
	};
	int n;
	Info info;
};

typedef map<int, warrior::Info> MAP;
typedef map<int, city::Info> CITY;

int main() {
	int caseN; cin >> caseN; int ii = 0; if (caseN == 0) { return 0; }
	while (ii < caseN) {
		++ii;
		cout << "Case " << ii << ":" << endl;
		int M, N, R, K, T; cin >> M >> N >> R >> K >> T; //life/city/arrow force/loyalty/time
	
		if (T >= 0) {
			Headquarter a; a.Mr = a.Mb = M;
			int life[5] = { 0 }; int force[5] = { 0 }; string name[5] = { "dragon", "ninja", "iceman", "lion", "wolf" }; 
			for (int j = 0; j < 5; ++j) { cin >> life[j]; } for (int j = 0; j < 5; ++j) { cin >> force[j]; }

			map<string, Binfo> mname; for (int j = 0; j < 5; ++j) {
				Binfo info; info.force = force[j]; info.life = life[j]; mname.insert(make_pair(name[j], info)); 
			}

			MAP mr; MAP mb; MAP::iterator i; MAP::iterator temp;//insert warriors in map
			int min = 0; int hour = 0; warrior redw; warrior bluew;
			CITY acity; CITY::iterator icity; city aacity;
			for (int j = 0; j < N; ++j) { acity.insert(make_pair(j + 1, aacity.info)); } //initiate city
			string redname[5] = { "iceman", "lion", "wolf", "ninja", "dragon" }; string bluename[5] = { "lion", "dragon", "ninja", "iceman", "wolf" }; //order to make warrior
			int jr = 0, jb = 0, nr = 0, nb = 0;  //index of array/sum of warriors

			while (T >= 0) {
				if (min == 0) {   // make warriors
					if (jr == 5) { jr = 0; }  //jr is the index of array / nr is used as count
					map<string, Binfo>::iterator ir = mname.find(redname[jr]); //redheadquarter
					if (a.Mr >= ir->second.life) { //if it is enough to make warrior
						++nr;
						redw.info.life = ir->second.life; redw.info.force = ir->second.force; redw.n = nr; a.Mr -= ir->second.life;
						redw.info.step = 0; redw.info.loyalty = 0; redw.info.sword = 0; redw.info.arrow = 0; redw.info.bomb = 0; redw.info.sq = 0.00;
						if (redname[jr] == "iceman" || redname[jr] == "dragon" || redname[jr] == "ninja") { //they born with weapon
							if (nr % 3 == 0) { redw.info.sword = (int)(0.2*redw.info.force); }
							else if (nr % 3 == 1) { redw.info.bomb = 1; }
							else if (nr % 3 == 2) { redw.info.arrow = 3; }
							if (redname[jr] == "ninja") {
								redw.info.name = "ninja"; //ninja born with 2 weapons
								if ((nr + 1) % 3 == 0) { redw.info.sword = (int)(0.2*redw.info.force); }
								else if ((nr + 1) % 3 == 1) { redw.info.bomb = 1; }
								else if ((nr + 1) % 3 == 2) { redw.info.arrow = 3; }
								cout << setfill('0') << setw(3) << hour << ":00 red ninja " << nr << " born" << endl;
							}
							else if (redname[jr] == "iceman") {
								redw.info.name = "iceman";
								cout << setfill('0') << setw(3) << hour << ":00 red iceman " << nr << " born" << endl;
							}
							else if (redname[jr] == "dragon") { //dragon born with morale
								redw.info.name = "dragon"; redw.info.sq = (double)a.Mr / (double)redw.info.life;
								cout << setfill('0') << setw(3) << hour << ":00 red dragon " << nr << " born" << endl;
								cout << "Its morale is " << fixed<<setprecision(2)<<redw.info.sq << endl;
							}
						}
						else if (redname[jr] == "lion") { //lion born with morale
							redw.info.name = "lion"; redw.info.loyalty = a.Mr;
							cout << setfill('0') << setw(3) << hour << ":00 red lion " << nr << " born" << endl;
							cout << "Its loyalty is " << redw.info.loyalty << endl;
						}
						else if (redname[jr] == "wolf") {
							redw.info.name = "wolf";
							cout << setfill('0') << setw(3) << hour << ":00 red wolf " << nr << " born" << endl;
						}
						mr.insert(make_pair(redw.n, redw.info)); ++jr;  //insert warriors in map
					}

					if (jb == 5) { jb = 0; } 
					map<string, Binfo>::iterator ib = mname.find(bluename[jb]); 
					if (a.Mb >= ib->second.life) { //blueheadquarter
						++nb;
						bluew.info.life = ib->second.life; bluew.info.force = ib->second.force; bluew.n = nb; a.Mb -= ib->second.life;
						bluew.info.step = 0; bluew.info.loyalty = 0; bluew.info.sword = 0; bluew.info.arrow = 0; bluew.info.bomb = 0; bluew.info.sq = 0.00;
						if (bluename[jb] == "iceman" || bluename[jb] == "dragon" || bluename[jb] == "ninja") {
							if (nb % 3 == 0) { bluew.info.sword = (int) (0.2*bluew.info.force); }
							else if (nb % 3 == 1) { bluew.info.bomb = 1; }
							else if (nb % 3 == 2) { bluew.info.arrow = 3; }
							if (bluename[jb] == "ninja") {
								bluew.info.name = "ninja";
								if ((nb + 1) % 3 == 0) { bluew.info.sword = (int)(0.2*bluew.info.force); }
								else if ((nb + 1) % 3 == 1) { bluew.info.bomb = 1; }
								else if ((nb + 1) % 3 == 2) { bluew.info.arrow = 3; }
								cout << setfill('0') << setw(3) << hour << ":00 blue ninja " << nb << " born" << endl;
							}
							else if (bluename[jb] == "iceman") {
								bluew.info.name = "iceman";
								cout << setfill('0') << setw(3) << hour << ":00 blue iceman " << nb << " born" << endl;
							}
							else if (bluename[jb] == "dragon") {
								bluew.info.name = "dragon";
								bluew.info.sq = (double)a.Mb / (double)bluew.info.life;
								cout << setfill('0') << setw(3) << hour << ":00 blue dragon " << nb << " born" << endl;
								cout << "Its morale is " << fixed<< setprecision(2)<<bluew.info.sq << endl;
							}
						}
						else if (bluename[jb] == "lion") {
							bluew.info.name = "lion"; bluew.info.loyalty = a.Mb;
							cout << setfill('0') << setw(3) << hour << ":00 blue lion " << nb << " born" << endl;
							cout << "Its loyalty is " << bluew.info.loyalty << endl;
						}
						else if (bluename[jb] == "wolf") {
							bluew.info.name = "wolf";
							cout << setfill('0') << setw(3) << hour << ":00 blue wolf " << nb << " born" << endl;
						}
						mb.insert(make_pair(bluew.n, bluew.info)); ++jb;
					}
				}
				T -= 5; if (T < 0) break; else min += 5;

				if (min == 5) { //lion ran away
					if (mr.size() != 0) { 
						i = mr.end(); --i;  //lion born with 0 loyalty would run away too
						if (i->second.name == "lion" && i->second.loyalty <= 0 && i->second.step==0){ 
							cout << setfill('0') << setw(3) << hour << ":05 red lion " << i->first << " ran away" << endl; 
							++i;
							if (i != mr.end()) { temp = i; --i; mr.erase(i); i = temp; }
							else {
								--i;
								if (mr.size() == 1) { mr.erase(i); }
								else { --i; temp = i; ++i; mr.erase(i); i = temp; }
							}
						}
					} 
					MAP::iterator j; if (mb.size() != 0) { j = mb.begin(); }
					for (icity = acity.begin(); icity != acity.end(); ++icity) {
						if (icity->second.blue != 0 || icity->second.red != 0) {
							if (icity->second.red != 0) { // the city has red warrior
								while (i->second.step < icity->first && i!=mr.begin()) { --i; }
								if (i->second.step == icity->first && i->second.name == "lion" && i->second.loyalty <= 0 ) {
									cout << setfill('0') << setw(3) << hour << ":05 red lion " << i->first << " ran away" << endl;
									++i;
									if (i != mr.end()) { temp = i; --i; mr.erase(i); i = temp; }
									else {
										--i;
										if (mr.size() == 1) { mr.erase(i); }
										else { --i; temp = i; ++i; mr.erase(i); i = temp; }
									}
									icity->second.red -= 1;
								}
							}
							if (icity->second.blue != 0) {
								while (N + 1 - j->second.step < icity->first && j != mb.end()) { ++j; }
								if (j == mb.end()) { --j; }
								if (N + 1 - j->second.step == icity->first && j->second.name == "lion" && j->second.loyalty <= 0) {
									cout << setfill('0') << setw(3) << hour << ":05 blue lion " << j->first << " ran away" << endl;
									++j;
									if (j != mb.end()) { temp = j; --j; mb.erase(j); j = temp; }
									else {
										--j;
										if (mb.size() == 1) { mb.erase(j); }
										else { --j; temp = j; ++j; mb.erase(j); j = temp; }
									}
									icity->second.blue -= 1;
								}
							}
						}
					}
					if (mb.size() != 0) { //blue lion born with 0 loyalty will run away too
						j = mb.end(); --j; if (j->second.name == "lion" && j->second.loyalty <= 0 && j->second.step == 0) { //blue lion in the headquarter
							cout << setfill('0') << setw(3) << hour << ":05 blue lion " << j->first << " ran away" << endl;
							++j;
							if (j != mb.end()) { temp = j; --j; mb.erase(j); j = temp; }
							else {
								--j;
								if (mb.size() == 1) { mb.erase(j); }
								else { --j; temp = j; ++j; mb.erase(j); j = temp; }
							}
						}
					}
				}
				T -= 5; if (T < 0) break; else min += 5;

				if (min == 10) { //everybody makes a move to the next city/headquarter
					MAP::iterator j; 
					if (mb.size() != 0) { //everybody makes a step first
						for (j = mb.begin(); j != mb.end(); ++j) { j->second.step += 1; }
					}
					if (mr.size() != 0) {
						for (i = mr.begin(); i != mr.end(); ++i) { i->second.step += 1; }
					}

					if (mb.size() != 0) { // if blue warrior marched into red head quarter
						j = mb.begin(); 
						while (j->second.step > N + 1 && j != mb.end()) { ++j; if (j == mb.end()) { --j; break; } }
						
						if (j->second.step == N + 1) {
							if (j->second.name == "iceman") { //iceman force increase/life decrease
								if (j->second.step % 2 == 0) {
									if (i->second.life <= 9) { i->second.life = 1; i->second.force += 20; }
									else { i->second.life -= 9; i->second.force += 20; }
								}
							}
							cout << setfill('0') << setw(3) << hour << ":10 blue " << j->second.name << " " << j->first << " reached red headquarter with " << j->second.life << " elements and force " << j->second.force << endl;
							a.bluetor += 1;
							if (a.bluetor == 2) {
								cout << setfill('0') << setw(3) << hour << ":10 red headquarter was taken" << endl;
							}
							icity = acity.begin(); icity->second.blue -= 1;
						}
					}

					if (mr.size() != 0) { i = mr.end(); --i; }
					if (mb.size() != 0) { j = mb.begin(); }
					for (icity = acity.begin(); icity != acity.end(); ++icity) {
						if (mr.size() != 0) {
							while (i->second.step < icity->first && i != mr.begin()) { --i; }
							if (i->second.step == icity->first && icity->first <= N) {
								if (icity->first > 1) { icity->second.red += 1; --icity; icity->second.red -= 1; ++icity; }
								else { icity->second.red += 1; }
								if (i->second.name == "iceman") { //iceman force increase/life decrease
									if (i->second.step % 2 == 0) {
										if (i->second.life <= 9) { i->second.life = 1; i->second.force += 20; }
										else { i->second.life -= 9; i->second.force += 20; }
									}
								}
								if (i->second.step <= N) {
									cout << setfill('0') << setw(3) << hour << ":10 red " << i->second.name << " " << i->first << " marched to city " << i->second.step << " with " << i->second.life << " elements and force " << i->second.force << endl;
								}
							}
						}
							
						if (mb.size() != 0) {
							if (icity != acity.end()) {
								while (N + 1 - j->second.step < icity->first ) { ++j; if (j == mb.end()) { --j; break; }}
								if (N + 1 - j->second.step == icity->first) {
									if (j->second.step > 1) { icity->second.blue += 1; ++icity; icity->second.blue -= 1; --icity; }
									else { icity->second.blue += 1; }
									if (j->second.name == "iceman") {
										if (j->second.step % 2 == 0) {
											if (j->second.life <= 9) { j->second.life = 1; j->second.force += 20; }
											else { j->second.life -= 9; j->second.force += 20; }
										}
									}
									cout << setfill('0') << setw(3) << hour << ":10 blue " << j->second.name << " " << j->first << " marched to city " << N + 1 - j->second.step << " with " << j->second.life << " elements and force " << j->second.force << endl;
								}
							}
						}
					}

					if (mr.size() != 0) { //red warrior entering the headquarter
						i = mr.begin();
						while (i->second.step > N + 1 && i!=mr.end()) { ++i; }
						if (i == mr.end()) { --i; }
						if (i->second.step == N + 1) {
							icity = acity.end(); --icity; icity->second.red -= 1; 
							if (i->second.name == "iceman") { //iceman force increase/life decrease
								if (i->second.step % 2 == 0) {
									if (i->second.life <= 9) { i->second.life = 1; i->second.force += 20; }
									else { i->second.life -= 9; i->second.force += 20; }
								}
							}
							cout << setfill('0') << setw(3) << hour << ":10 red " << i->second.name << " " << i->first << " reached blue headquarter with " << i->second.life << " elements and force " << i->second.force << endl;
							a.redtob += 1;
							if (a.redtob == 2) { //two warriors in the headquarter
								cout << setfill('0') << setw(3) << hour << ":10 blue headquarter was taken" << endl; break;
							}
						}
					}

					if (a.bluetor == 2 || a.redtob == 2) { break; }
				}
				T -= 10; if (T < 0) break; else min += 10;

				if (min == 20) { //city life add 10
					for (icity = acity.begin(); icity != acity.end(); ++icity) { icity->second.life += 10; }
				}
				T -= 10; if (T < 0) break; else min += 10;

				if (min == 30) { //if only one warrior in the city, he will have the city life
					for (icity = acity.begin(); icity != acity.end(); ++icity) {
						if (icity->second.blue == 1 && icity->second.red == 0 ) { 
							a.Mb += icity->second.life;  
							for (i = mb.begin(); i != mb.end(); ++i) {
								if (i->second.step == N + 1 - icity->first) {
									cout << setfill('0') << setw(3) << hour << ":30 blue " << i->second.name << " " << i->first << " earned "<<icity->second.life<<" elements for his headquarter" << endl; 
									icity->second.life = 0; break;
								}
							}
						}
						else if (icity->second.red == 1 && icity->second.blue == 0 ) { 
							a.Mr += icity->second.life; 
							i = mr.end(); --i;
							for (; i != mr.begin(); --i) {
								if (i->second.step == icity->first) {
									cout << setfill('0') << setw(3) << hour << ":30 red " << i->second.name << " " << i->first << " earned " << icity->second.life << " elements for his headquarter" << endl; 
									icity->second.life = 0; break;
								}
							}
							if (i == mr.begin() && i->second.step == icity->first) {
								cout << setfill('0') << setw(3) << hour << ":30 red " << i->second.name << " " << i->first << " earned " << icity->second.life << " elements for his headquarter" << endl;
								icity->second.life = 0; 
							}
						}
					}
				}
				T -= 5; if (T < 0) break; else min += 5;

				if (min == 35) { //arrow attack
					if (mr.size() != 0 && mb.size() != 0) {
						MAP::iterator j; i = mr.end(); --i; icity = acity.begin();

						if (acity.size() > 1) {
							i = mr.end(); --i; icity = acity.begin(); ++icity;
							if (i->second.step == 1 && i->second.arrow > 0 && icity->second.blue > 0) { //red shot in city 1
								j = mb.begin();
								while (N + 1 - j->second.step < 2 && j != mb.end()) { ++j; }
								if (j->second.step == N - 1) {
									cout << setfill('0') << setw(3) << hour << ":35 red " << i->second.name << " " << i->first << " shot";
									j->second.life -= R; i->second.arrow -= 1;
									if (j->second.life <= 0) { cout << " and killed blue " << j->second.name << " " << j->first; }
									cout << endl;
								}
							}
						}

						icity = acity.begin(); ++icity; j = mb.begin();
						for (; icity != acity.end(); ++icity) {
							while (i->second.step < icity->first && i != mr.begin()) { --i; }
							if (i->second.step == icity->first && i->second.arrow > 0 && i->second.step != N) {
								++icity;
								if (icity->second.blue > 0) {
									while (N + 1 - j->second.step < icity->first && j != mb.end()) { ++j; }
									if (j == mb.end()) { --j; }
									if (N + 1 - j->second.step == icity->first) {
										cout << setfill('0') << setw(3) << hour << ":35 red " << i->second.name << " " << i->first << " shot";
										j->second.life -= R; i->second.arrow -= 1;
										if (j->second.life <= 0) { 
											cout << " and killed blue " << j->second.name << " " << j->first; 
										}
										cout << endl;
									}
									--icity;
								}
								else --icity;
							}

							j = mb.begin();
							while (N + 1 - j->second.step < icity->first && j != mb.end()) { ++j; if (j == mb.end()) { --j; break; }}
							if (N + 1 - j->second.step == icity->first && j->second.arrow > 0){
								--icity;
								if (icity->second.red > 0) {
									temp = mr.end(); --temp;
									while (temp->second.step < icity->first && temp != mr.begin()) { --temp; }
									if (temp->second.step == icity->first) {
										cout << setfill('0') << setw(3) << hour << ":35 blue " << j->second.name << " " << j->first << " shot";
										temp->second.life -= R; j->second.arrow -= 1;
										if (temp->second.life <= 0) {
											cout << " and killed red " << temp->second.name << " " << temp->first;
										}
										cout << endl;
									}
									++icity;
								}
								else ++icity;
							}
						}
					}
				}
				T -= 3; if (T < 0) break; else min += 3;

				if (min == 38) { //bomb attack
					if (mr.size() != 0 && mb.size() != 0) {
						MAP::iterator j=mb.begin(); i = mr.end(); --i;
						for (icity = acity.begin(); icity != acity.end(); ++icity) {
							if (icity->second.blue > 0 && icity->second.red > 0) {
								while (i->second.step < icity->first && i != mr.begin()) { --i; }
								while (N + 1 - j->second.step < icity->first && j != mb.end()) { ++j; }
								if (j == mb.end()) { --j; }
								if (i->second.step == N + 1 - j->second.step){ 
									if (i->second.bomb > 0) {
										if (((icity->second.flag == 'r' || (icity->first % 2 == 1 && icity->second.flag != 'b')) && i->second.life > 0 && j->second.life > 0 && j->second.life > i->second.force + i->second.sword &&
											i->second.life <= j->second.force / 2 + j->second.sword && j->second.name!="ninja" ) ||
											((icity->second.flag == 'b' || (icity->first % 2 == 0 && icity->second.flag != 'r')) && i->second.life <= j->second.force + j->second.sword && i->second.life > 0 && j->second.life > 0)) {
											cout << setfill('0') << setw(3) << hour << ":38 red " << i->second.name << " " << i->first << " used a bomb and killed blue " << j->second.name << " " << j->first << endl;
											++i;
											if (i != mr.end()) { temp = i; --i; mr.erase(i); i = temp; }
											else {
												--i;
												if (mr.size() == 1) { mr.erase(i); }
												else { --i; temp = i; ++i; mr.erase(i); i = temp; }
											}
											icity->second.red -= 1;
											++j;
											if (j != mb.end()) { temp = j; --j; mb.erase(j); j = temp; }
											else {
												--j;
												if (mb.size() == 1) { mb.erase(j); }
												else { --j; temp = j; ++j; mb.erase(j); j = temp; }
											}
											icity->second.blue -= 1;
										}
									}
									if (j->second.bomb > 0) {
										if (((icity->second.flag == 'r' || (icity->first % 2 == 1 && icity->second.flag != 'b')) && j->second.life <= i->second.force + i->second.sword && j->second.life > 0 && i->second.life > 0 && icity->second.blue > 0) ||
											((icity->second.flag == 'b' || (icity->first % 2 == 0 && icity->second.flag != 'r')) && j->second.life > 0 && i->second.life > 0 && i->second.life > j->second.force + j->second.sword &&
												j->second.life <= i->second.force / 2 + i->second.sword && i->second.name!="ninja" ) && icity->second.blue>0) {
											cout << setfill('0') << setw(3) << hour << ":38 blue " << j->second.name << " " << j->first << " used a bomb and killed red " << i->second.name << " " << i->first << endl;
											++i;
											if (i != mr.end()) { temp = i; --i; mr.erase(i); i = temp; }
											else {
												--i;
												if (mr.size() == 1) { mr.erase(i); }
												else { --i; temp = i; ++i; mr.erase(i); i = temp; }
											}
											icity->second.red -= 1;
											++j;
											if (j != mb.end()) { temp = j; --j; mb.erase(j); j = temp; }
											else {
												--j;
												if (mb.size() == 1) { mb.erase(j); }
												else { --j; temp = j; ++j; mb.erase(j); j = temp; }
											}
											icity->second.blue -= 1;
										}
									}
								}
							}
						}
					}
				}
				T -= 2; if (T < 0) break; else min += 2;
				
				if (min == 40) {
					if (mr.size() != 0 && mb.size() != 0) {
						set<int> v;
						for (icity = acity.begin(); icity != acity.end(); ++icity) {
							if (icity->second.blue == 0 || icity->second.red == 0) { //shot by enemy but his the only one in the city
								if (icity->second.blue == 0 && icity->second.red > 0){
									i = mr.end(); --i; 
									for (; i != mr.begin(); --i) { if (i->second.step == icity->first) { break; } }
									if (i->second.step == icity->first && i->second.life <= 0) {
										++i;
										if (i != mr.end()) { temp = i; --i; mr.erase(i); i = temp; }
										else {
											--i;
											if (mr.size() == 1) { mr.erase(i); }
											else { --i; temp = i; ++i; mr.erase(i); i = temp; }
										}
										icity->second.red -= 1;
									}
								}
								else if (icity->second.red == 0 && icity->second.blue > 0) {
									MAP::iterator j = mb.begin(); //find the blue warrior
									for (; j != mb.end(); ++j) { if (j->second.step == N + 1 - icity->first) { break; } }
									if (j->second.step == N + 1 - icity->first && j->second.life <= 0) {
										++j;
										if (j != mb.end()) { temp = j; --j; mb.erase(j); j = temp; }
										else {
											--j;
											if (mb.size() == 1) { mb.erase(j); }
											else { --j; temp = j; ++j; mb.erase(j); j = temp; }
										}
										icity->second.blue -= 1;
									}
								}
							}

							else if (icity->second.blue > 0 && icity->second.red > 0) { //enemies meet
								i = mr.end(); --i; //find the red warrior
								for (; i != mr.begin(); --i) { if (i->second.step == icity->first) { break; } } //suppose we could find it no matter what

								MAP::iterator j = mb.begin(); //find the blue warrior
								for (; j != mb.end(); ++j) { if (j->second.step == N + 1 - icity->first) { break; } } //found both if them, the war shall start

								if (i != mr.end() && j != mb.end()) {
									if (i->second.life <= 0 || j->second.life <= 0) { //any of them was killed by arrow
										if (i->second.life > 0) {
											v.insert(i->first);
											if (i->second.name == "dragon") {
												i->second.sq += 0.2;
												if ((icity->second.flag == 'r' || (icity->first % 2 == 1 && icity->second.flag != 'b')) && i->second.sq > 0.8) {
													cout << setfill('0') << setw(3) << hour << ":40 red dragon " << i->first << " yelled in city " << icity->first << endl;
												}
											}
											else if (i->second.name == "wolf") {
												if (i->second.sword <= 0) { i->second.sword = j->second.sword; }
												if (i->second.arrow <= 0) { i->second.arrow = j->second.arrow; }
												if (i->second.bomb <= 0) { i->second.bomb = j->second.bomb; }
											}
											++j;
											if (j != mb.end()) { temp = j; --j; mb.erase(j); j = temp; }
											else {
												--j;
												if (mb.size() == 1) { mb.erase(j); }
												else { --j; temp = j; ++j; mb.erase(j); j = temp; }
											}
											icity->second.blue -= 1;
											cout << setfill('0') << setw(3) << hour << ":40 red " << i->second.name << " " << i->first << " earned " << icity->second.life << " elements for his headquarter" << endl;
											a.cityawardr += icity->second.life; icity->second.life = 0; //citylife give to headquarter
											icity->second.flagr += 1; icity->second.flagb = 0;
											if (icity->second.flagr == 2 && icity->second.flag!='r') { // raise the flag
												cout << setfill('0') << setw(3) << hour << ":40 red flag raised in city " << icity->first << endl;
												icity->second.flag = 'r'; icity->second.flagr = 0;
											}
											
										}
										else if (j->second.life > 0) {
											if (j->second.name == "dragon") {
												j->second.sq += 0.2;
												if ((icity->second.flag == 'b' || (icity->first % 2 == 0 && icity->second.flag != 'r')) && j->second.sq > 0.8) {
													cout << setfill('0') << setw(3) << hour << ":40 blue dragon " << j->first << " yelled in city " << icity->first << endl;
												}
											}
											else if (j->second.name == "wolf") {
												if (j->second.sword <= 0) { j->second.sword = i->second.sword; }
												if (j->second.arrow <= 0) { j->second.arrow = i->second.arrow; }
												if (j->second.bomb <= 0) { j->second.bomb = i->second.bomb; }
											}
											++i;
											if (i != mr.end()) { temp = i; --i; mr.erase(i); i = temp; }
											else {
												--i;
												if (mr.size() == 1) { mr.erase(i); }
												else { --i; temp = i; ++i; mr.erase(i); i = temp; }
											}
											icity->second.red -= 1;
											if (a.Mb >= 8) { a.Mb -= 8; j->second.life += 8; }
											cout << setfill('0') << setw(3) << hour << ":40 blue " << j->second.name << " " << j->first << " earned " << icity->second.life << " elements for his headquarter" << endl;
											a.cityawardb += icity->second.life; icity->second.life = 0;
											icity->second.flagb += 1; icity->second.flagr = 0;
											if (icity->second.flagb == 2 && icity->second.flag != 'b') {
												cout << setfill('0') << setw(3) << hour << ":40 blue flag raised in city " << icity->first << endl;
												icity->second.flag = 'b'; icity->second.flagb = 0;
											}
										}
										else {
											++i;
											if (i != mr.end()) { temp = i; --i; mr.erase(i); i = temp; }
											else { 
												--i; 
												if (mr.size() == 1) { mr.erase(i); }
												else { --i; temp = i; ++i; mr.erase(i); i = temp; }
											}
											icity->second.red -= 1;
											++j;
											if (j != mb.end()) { temp = j; --j; mb.erase(j); j = temp; }
											else {
												--j;
												if (mb.size() == 1) { mb.erase(j); }
												else { --j; temp = j; ++j; mb.erase(j); j = temp; }
											}
											icity->second.blue -= 1;
										}
									}

									else { //both alive,then fight
										int templifer = i->second.life; int templifeb = j->second.life;
										if (icity->second.flag == 'r' || (icity->first % 2 == 1 && icity->second.flag != 'b')) { //attack first by red
											cout << setfill('0') << setw(3) << hour << ":40 red " << i->second.name << " " << i->first << " attacked blue " << j->second.name << " " << j->first << " in city " << icity->first << " with " << i->second.life << " elements and force " << i->second.force << endl;
											j->second.life -= (i->second.force + i->second.sword); i->second.sword = (int)(0.8 * i->second.sword);
											if (j->second.life <= 0) { //dead
												cout << setfill('0') << setw(3) << hour << ":40 blue " << j->second.name << " " << j->first << " was killed in city " << icity->first << endl;
												v.insert(i->first);
												if (j->second.name == "lion") { i->second.life += templifeb; }
												if (i->second.name == "dragon") {
													i->second.sq += 0.2;
													if (i->second.sq > 0.8) {
														cout << setfill('0') << setw(3) << hour << ":40 red dragon " << i->first << " yelled in city " << icity->first << endl;
													}
												}
												else if (i->second.name == "wolf") {
													if (i->second.sword <= 0) { i->second.sword = j->second.sword; }
													if (i->second.arrow <= 0) { i->second.arrow = j->second.arrow; }
													if (j->second.bomb <= 0) { i->second.bomb = j->second.bomb; }
												}
												++j;
												if (j != mb.end()) { temp = j; --j; mb.erase(j); j = temp; }
												else { 
													--j; 
													if (mb.size()==1){ mb.erase(j); }
													else { --j; temp = j; ++j; mb.erase(j); j = temp; }
												}
												icity->second.blue -= 1;
												cout << setfill('0') << setw(3) << hour << ":40 red " << i->second.name << " " << i->first << " earned " << icity->second.life << " elements for his headquarter" << endl;
												a.cityawardr += icity->second.life; icity->second.life = 0;
												icity->second.flagr += 1; icity->second.flagb = 0;
												if (icity->second.flagr == 2 && icity->second.flag!='r') {
													cout << setfill('0') << setw(3) << hour << ":40 red flag raised in city " << icity->first << endl;
													icity->second.flag = 'r'; icity->second.flagr = 0;
												}
											}
											else {
												icity->second.flagr = 0;
												if (i->second.name == "dragon") { i->second.sq -= 0.2; }
												else if (i->second.name == "lion") { i->second.loyalty -= K; }
												if (j->second.name != "ninja") {
													cout << setfill('0') << setw(3) << hour << ":40 blue " << j->second.name << " " << j->first << " fought back against red " << i->second.name << " " << i->first << " in city " << icity->first << endl;
													i->second.life -= (j->second.force / 2 + j->second.sword); j->second.sword = (int)(0.8 * j->second.sword);
													if (i->second.life <= 0) {
														cout << setfill('0') << setw(3) << hour << ":40 red " << i->second.name << " " << i->first << " was killed in city " << icity->first << endl;
														if (i->second.name == "lion") { j->second.life += templifer; }
														if (j->second.name == "dragon") { j->second.sq += 0.2; }
														else if (j->second.name == "wolf") {
															if (j->second.sword <= 0) { j->second.sword = i->second.sword; }
															if (j->second.arrow <= 0) { j->second.arrow = i->second.arrow; }
															if (j->second.bomb <= 0) { j->second.bomb = i->second.bomb; }
														}
														++i;
														if (i != mr.end()) { temp = i; --i; mr.erase(i); i = temp; }
														else {
															--i;
															if (mr.size() == 1) { mr.erase(i); }
															else { --i; temp = i; ++i; mr.erase(i); i = temp; }
														}
														icity->second.red -= 1;
														cout << setfill('0') << setw(3) << hour << ":40 blue " << j->second.name << " " << j->first << " earned " << icity->second.life << " elements for his headquarter" << endl;
														a.cityawardb += icity->second.life; icity->second.life = 0;
														if (a.Mb >= 8) { a.Mb -= 8; j->second.life += 8; }
														icity->second.flagb += 1; icity->second.flagr = 0;
														if (icity->second.flagb == 2 && icity->second.flag!='b') {
															cout << setfill('0') << setw(3) << hour << ":40 blue flag raised in city " << icity->first << endl;
															icity->second.flag = 'b'; icity->second.flagb = 0;
														}
													}
													else {
														if (j->second.name == "lion") { j->second.loyalty -= K; }
														if (i->second.name == "dragon") {
															i->second.sq -= 0.2;
															if (i->second.sq > 0.8) {
																cout << setfill('0') << setw(3) << hour << ":40 red dragon " << i->first << " yelled in city " << icity->first << endl;
															}
														}
														icity->second.flagb = 0; icity->second.flagr = 0;
													}
												}
												else { 
													if (i->second.name == "dragon") {
														i->second.sq -= 0.2;
														if (i->second.sq > 0.8) {
															cout << setfill('0') << setw(3) << hour << ":40 red dragon " << i->first << " yelled in city " << icity->first << endl;
														}
													}
													if (j->second.name == "lion") { j->second.loyalty -= K; }
													icity->second.flagr = 0; icity->second.flagb = 0; 
												}
											}
										}

										else if (icity->second.flag == 'b' || (icity->first % 2 == 0 && icity->second.flag != 'r')) { //attack first by blue
											cout << setfill('0') << setw(3) << hour << ":40 blue " << j->second.name << " " << j->first << " attacked red " << i->second.name << " " << i->first << " in city " << icity->first << " with " << j->second.life << " elements and force " << j->second.force << endl;
											i->second.life -= (j->second.force + j->second.sword); j->second.sword = (int)(0.8 * j->second.sword);
											if (i->second.life <= 0) { //dead
												cout << setfill('0') << setw(3) << hour << ":40 red " << i->second.name << " " << i->first << " was killed in city " << icity->first << endl;
												if (i->second.name == "lion") { j->second.life += templifer; }
												if (j->second.name == "dragon") {
													j->second.sq += 0.2;
													if (j->second.sq > 0.8) { cout << setfill('0') << setw(3) << hour << ":40 blue dragon " << j->first << " yelled in city " << icity->first << endl; }
												}
												else if (j->second.name == "wolf") {
													if (j->second.sword <= 0) { j->second.sword = i->second.sword; }
													if (j->second.sword <= 0) { j->second.arrow = i->second.arrow; }
													if (j->second.sword <= 0) { j->second.bomb = i->second.bomb; }
												}
												++i;
												if (i != mr.end()) { temp = i; --i; mr.erase(i); i = temp; }
												else {
													--i;
													if (mr.size() == 1) { mr.erase(i); }
													else { --i; temp = i; ++i; mr.erase(i); i = temp; }
												}
												icity->second.red -= 1;
												cout << setfill('0') << setw(3) << hour << ":40 blue " << j->second.name << " " << j->first << " earned " << icity->second.life << " elements for his headquarter" << endl;
												a.cityawardb += icity->second.life; icity->second.life = 0;
												if (a.Mb >= 8) { a.Mb -= 8; j->second.life += 8; }
												icity->second.flagb += 1; icity->second.flagr = 0;
												if (icity->second.flagb == 2 && icity->second.flag!='b') {
													cout << setfill('0') << setw(3) << hour << ":40 blue flag raised in city " << icity->first << endl;
													icity->second.flag = 'b'; icity->second.flagb = 0;
												}
												
											}
											else {
												if (j->second.name == "lion") { j->second.loyalty -= K; }
												else if (j->second.name == "dragon") { j->second.sq -= 0.2; }
												if (i->second.name != "ninja") {
													cout << setfill('0') << setw(3) << hour << ":40 red " << i->second.name << " " << i->first << " fought back against blue " << j->second.name << " " << j->first << " in city " << icity->first << endl;
													j->second.life -= (i->second.force / 2 + i->second.sword); i->second.sword = (int)(0.8 * i->second.sword);
													if (j->second.life <= 0) {
														v.insert(i->first); 
														cout << setfill('0') << setw(3) << hour << ":40 blue " << j->second.name << " " << j->first << " was killed in city " << icity->first << endl;
														if (j->second.name == "lion") { i->second.life += templifeb; }
														if (i->second.name == "dragon") { i->second.sq += 0.2; }
														else if (i->second.name == "wolf") {
															if (i->second.sword <= 0) { i->second.sword = j->second.sword; }
															if (i->second.arrow <= 0) { i->second.arrow = j->second.arrow; }
															if (i->second.bomb <= 0) { i->second.bomb = j->second.bomb; }
														}
														++j;
														if (j != mb.end()) { temp = j; --j; mb.erase(j); j = temp; }
														else {
															--j;
															if (mb.size() == 1) { mb.erase(j); }
															else { --j; temp = j; ++j; mb.erase(j); j = temp; }
														}
														icity->second.blue -= 1;
														cout << setfill('0') << setw(3) << hour << ":40 red " << i->second.name << " " << i->first << " earned " << icity->second.life << " elements for his headquarter" << endl;
														a.cityawardr += icity->second.life; icity->second.life = 0;
														icity->second.flagr += 1; icity->second.flagb = 0;
														if (icity->second.flagr == 2 && icity->second.flag!='r') {
															cout << setfill('0') << setw(3) << hour << ":40 red flag raised in city " << icity->first << endl;
															icity->second.flag = 'r'; icity->second.flagr = 0;
														}
													}
													else {
														if (i->second.name == "lion") { i->second.loyalty -= K; }
														if (j->second.name == "dragon") {
															j->second.sq -= 0.2;
															if (j->second.sq > 0.8) {
																cout << setfill('0') << setw(3) << hour << ":40 blue dragon " << j->first << " yelled in city " << icity->first << endl;
															}
														}
														icity->second.flagr = 0; icity->second.flagb = 0;
													}
												}
												else { 
													if (i->second.name == "lion") { i->second.loyalty -= K; }
													if (j->second.name == "dragon") {
														j->second.sq -= 0.2;
														if (j->second.sq > 0.8) {
															cout << setfill('0') << setw(3) << hour << ":40 blue dragon " << j->first << " yelled in city " << icity->first << endl;
														}
													}
													icity->second.flagb = 0; icity->second.flagr = 0;
												}
											}
										}
									}
								}
							}
						}
						if (v.size() != 0) {
							i = mr.end(); --i;
							for (; i != mr.begin(); --i) {
								if (a.Mr < 8) { break; }
								if (v.find(i->first) != v.end()) { i->second.life += 8; a.Mr -= 8; }
							}
							if (v.find(i->first) != v.end() && a.Mr >= 8) { i->second.life += 8; a.Mr -= 8; }
						}
						a.Mr += a.cityawardr; a.cityawardr = 0; a.Mb += a.cityawardb; a.cityawardb = 0;
					}
				}
				T -= 10; if (T < 0) break; else min += 10;

				if (min == 50) { //headquarter report life
					cout << setfill('0') << setw(3) << hour << ":50 " << a.Mr << " elements in red headquarter" << endl;
					cout << setfill('0') << setw(3) << hour << ":50 " << a.Mb << " elements in blue headquarter" << endl;
				}
				T -= 5; if (T < 0) break; else min += 5;

				if (min == 55) { //cout the weapon situation
					if (mr.size() != 0) {
						i = mr.end(); --i;
						for (; i != mr.begin(); --i) {
							cout << setfill('0') << setw(3) << hour << ":55 red " << i->second.name << " " << i->first << " has ";
							if (i->second.sword == 0 && i->second.bomb == 0 && i->second.arrow == 0) {
								cout << "no weapon";
							}
							else if (i->second.arrow != 0) {
								cout << "arrow(" << i->second.arrow << ")";
								if (i->second.bomb != 0) { cout << ",bomb"; }
								if (i->second.sword != 0) { cout << ",sword(" << i->second.sword << ")"; }
							}
							else if (i->second.bomb != 0) {
								cout << "bomb";
								if (i->second.sword != 0) { cout << ",sword(" << i->second.sword << ")"; }
							}
							else if (i->second.sword != 0) { cout << "sword(" << i->second.sword << ")"; }
							cout << endl;
						}
						if (i == mr.begin()) {
							cout << setfill('0') << setw(3) << hour << ":55 red " << i->second.name << " " << i->first << " has ";
							if (i->second.sword == 0 && i->second.bomb == 0 && i->second.arrow == 0) {
								cout << "no weapon";
							}
							else if (i->second.arrow != 0) {
								cout << "arrow(" << i->second.arrow << ")";
								if (i->second.bomb != 0) { cout << ",bomb"; }
								if (i->second.sword != 0) { cout << ",sword(" << i->second.sword << ")"; }
							}
							else if (i->second.bomb != 0) {
								cout << "bomb";
								if (i->second.sword != 0) { cout << ",sword(" << i->second.sword << ")"; }
							}
							else if (i->second.sword != 0) { cout << "sword(" << i->second.sword << ")"; }
							cout << endl;

						}
					}

					if (mb.size() != 0) {
						for (i = mb.begin(); i != mb.end(); ++i) {
							cout << setfill('0') << setw(3) << hour << ":55 blue " << i->second.name << " " << i->first << " has ";
							if (i->second.sword == 0 && i->second.bomb == 0 && i->second.arrow == 0) { cout << "no weapon"; }
							else if (i->second.arrow != 0) {
								cout << "arrow(" << i->second.arrow << ")";
								if (i->second.bomb != 0) { 
									cout << ",bomb"; }
								if (i->second.sword != 0) { 
									cout << ",sword(" << i->second.sword << ")"; }
							}
							else if (i->second.bomb != 0) {
								cout << "bomb";
								if (i->second.sword != 0) { cout << ",sword(" << i->second.sword << ")"; }
							}
							else if (i->second.sword != 0) { cout << "sword(" << i->second.sword << ")"; }
							cout << endl;
						}
					}
				}
				T -= 5; if (T < 0) break; else hour += 1; min = 0;
			}
		}
	}
	return 0;
}

Github: Westwood2723
Weibo: Cecilia_西木

相关标签: Coursera