【前前言】下面的應該是2018年的程設大作業,應該每年都會有變化~僅供參考!
&當時好像是為了教某個同學寫魔獸才寫的下面的博客,里面有的措辭真是一眼難盡hhhh(所以有的地方過於做作請大家忽略!
看評論說我的魔獸三代碼沒有提供!現在也更新上了!魔獸四和終極版的也加上了!不過不能特別保證正確性hhhh僅供大家參考!
然后我看了一下當時還是沒有特別面向對象,很多操作都是直接調用里面的元素來做的,並沒有實現封裝的感覺!比如直接調用武器的耐用值來操作!經過軟工的學習之后發現這種寫法是不好的!請大家通過調用函數來修改!從而保證每個類的封裝性~盡可能把屬性都設置為私有的嗯嗯!
【前言】:在學習了一些看上去只是能讓程序更好看更清晰的類的知識之后...為了讓我們真的覺得類是個好東西,喪心病狂的作業——魔獸出現了...然而...emmmm...
好吧,這篇博客主要就是記錄一下自己在打這些題的一些思路和想法...由於這個題里面其實沒有什么算法和數據結構的運用,感覺這篇博客成了一個教大家學習怎么樣完成作業的blog hhh不過這樣也挺好。雖然這么說啦,我會把我覺得我的程序里自己覺得很漂亮的部分重點講一下,也可以分享一些面對這樣一個比較復雜而冗長的代碼怎樣更好的提高調試效率的一些小方法。
因為直接讓大家做魔獸·終極版會直接讓大家放棄,所以老師很機智地設置了幾個循序漸進的版本
【魔獸世界一:備戰】
筆者認為呢,這個題的目的主要在於:1.營造場景 2.讓我們體會一下面向對象和面向過程之間的關系 3.emmm增加一下大家的信心,方便大家入坑(霧)
有N個城市,兩個指揮部,紅方和藍方。兩個指揮部都會制造武士,雙方會各自有一個制造武士的順序,並且每小時制造一個武士,同時制造武士需要代價,基地會有一個初始的值來消耗,然后如果當前順序能制造就制造,不能制造了就用順序的下一個,直到所有的武士都不能制造,再宣稱停止。
然后魔獸一出現了:它需要讓你按時間輸出這些制造的武士。
emm你看我上面的措辭:“輸出這些制造的武士”hhh並不是“制造這些武士並輸出...”
為什么會想到不去真的制造武士呢,首先是...懶hhh能少一個類就少一個,其次因為在這個題中的武士是沒有任何行為的。輸出行為可以放在司令部中輸出
所以這題中我只設置了一個司令部的類。然后我們來思索一下這個類里面需要什么呢?一般可以先想一想它進行的行為:
【行為】1制造一個武士 2停止制造武士
那么在制造武士的時候需要知道1.當前司令部的生命元 2.當前要制造的武士是誰 3.當前武士的編號 4.現在的時間 5.這個武士需要多少的生命元 6這個武士有多少了
這里我們會發現:4和5 其實在意義上應該設置成全局變量的,因為比如時間應該是每一個函數都可以調用到的,而每個武士需要用的生命元也是一個客觀的給出的條件,也應該是允許所有人調用的。
那么我們來考慮1236,其中又會發現3也不用考慮啦,因為編號就是時間+1...然后1我們可以在基地里設置一個整型的HP。而2的話會發現和順序和上一次用的是誰有關系,所以需要一個記錄順序的數組Order[],還有一個Index來記錄上一個是誰,對於6的話呢,我們可以用一個桶Count[],每次制造了一個武士who之后就能使用:Count[who]++;
嗯嗯,這樣我們就解決了這個類的成員了!然后就只有一個問題啦,怎樣通過Order[]數組和Index來得知下一個是誰呢?想來有很多方法啦...hhh我的代碼就是盡量短一點,然后好懂啦hhh[這也是寫代碼很好的習慣啦,hhh比如who,Index這種變量]
1 Index++; 2 int who=Order[Index%5],tmp=0; 3 while(HP<Cost[who] && tmp<5) 4 who=Order[++Index%5],tmp++;
然后就沒有難的地方啦!做完之后可以沾沾自喜了(誒,這貌似是一個貶義詞Hhh...不管啦) (果然是誘惑入坑的好題...)

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 5 using namespace std; 6 7 int Time_Index; 8 int Cost[6]; 9 int ord1[5]={3,4,5,2,1},ord2[5]={4,1,2,3,5}; 10 char Warrior_Name[6][7]={"\0","dragon","ninja","iceman","lion","wolf"}; 11 12 class Headquarter{ 13 public: 14 int Index; 15 int HP; 16 int Count[6]; 17 int Order[5]; 18 char Name[5]; 19 bool STOP; 20 Headquarter(int HP_,char *s,int *ord){ 21 Time_Index=-1; 22 Index=-1; 23 HP=HP_; 24 for(int i=1;i<=5;i++) Count[i]=0; 25 for(int i=0;i<5;i++) Order[i]=ord[i]; 26 memset(Name,0,sizeof(Name)); 27 strcpy(Name,s); 28 STOP=false; 29 } 30 void Build(){ 31 Time_Index++; 32 Index++; 33 if(STOP) return; 34 int who=Order[Index%5],tmp=0; 35 while(HP<Cost[who] && tmp<5) 36 who=Order[++Index%5],tmp++; 37 if(HP>=Cost[who]){ 38 HP-=Cost[who]; 39 Count[who]++; 40 printf("%03d %s %s %d born with strength %d,%d %s in %s headquarter\n",Time_Index,Name,Warrior_Name[who],Time_Index+1,Cost[who],Count[who],Warrior_Name[who],Name); 41 } 42 else{ 43 printf("%03d %s headquarter stops making warriors\n",Time_Index,Name); 44 STOP=true; 45 } 46 } 47 }; 48 49 int main(){ 50 #ifndef ONLINE_JUDGE 51 freopen("x.in","r",stdin); 52 freopen("x.out","w",stdout); 53 #endif 54 int Kase,W; 55 char s1[4]="red",s2[5]="blue"; 56 scanf("%d",&Kase); 57 for(int T=1;T<=Kase;T++){ 58 printf("Case:%d\n",T); 59 scanf("%d",&W); 60 for(int i=1;i<=5;i++) 61 scanf("%d",&Cost[i]); 62 Headquarter r(W,s1,ord1),b(W,s2,ord2); 63 while(!r.STOP || !b.STOP){ 64 r.Build(); 65 b.Build(); 66 } 67 } 68 return 0; 69 }
【魔獸世界二:裝備】
魔獸二是魔獸一的一個加強版,這個里面呢,我們還是要制造和輸出武士的信息,但是這個題目中最重要的就是出現了裝備!
哇,簡直是一個新世界啦...因為這樣的話,我們就有新的對象需要出現了!
在之前的魔獸世界一,武士這個類其實是沒有什么用的,他們只要提供名字和編號就可以了。但是在魔獸世界二里如果我們接着這樣做的話,就會有些麻煩咯,因為需要給武士們發武器了,那么這樣就一下誕生了兩個對象:武器和武士...
[當然啦這個題里其實武器也是沒有任何作用的,更容易的打法可以刪掉武器這個類哦...不過筆者感覺要開始為魔獸三做點准備了就寫了這個類]
我們注意到在魔獸一里武士們是沒有任何區別的,只有HP值和Name的不同。
但是在這一題中:我們發現這些武士們開始偷偷分化了!然后就會有很多細節啦,有時候半句話就是一個小細節。
[這種題最容易錯的就是細節,筆者有一個好方法:就是你可以先大概建好整個程序,然后一行一行地去看題目的描述,每看一句就去噼里啪啦敲好QwQ,這樣就不會漏掉細節了哦...emmm當然!也可以畫圖或者畫表來讓題目中的信息更加的清晰]
嗯嗯這個題中因為各種武士的不一樣,所以就可以開始感受虛函數的好處啦。
虛函數和多態在筆者看來是一個很實用的東西,它其實是類似 If 的一個函數,同樣的一句話,if 這個函數的主體是x就執行 x 里寫的函數,if 是 y 來做就執行 y 里寫的函數。但是這個卻不是在編譯中寫好的 if 而是執行的時候才去執行,這樣就讓代碼看上去邏輯感很好,而且也很簡潔啦!
然后筆者覺得邏輯這種東西,在寫一個比較長比較復雜的程序的時候是非常重要的,在保證一定的可讀性(這個需要變量名取得好哦...多用'_'或者首字母大寫來給變量名命名會很好)的情況下,如果你的程序邏輯清楚的話,是很容易找到錯誤的。
所以我在用virtual的時候一般是先去思考一個一般武士的行動,然后思考是不是有特例的武士不會這么做,那么就設置成virtual,比如看着題目的輸出樣例,你就可以很清楚的知道有:出生->得到武器->輸出自己情況 這樣三個普遍的行動,但是每一個部分都是有特例的,比如出生的時候有人會獲得士氣和忠誠這樣的屬性。得到武器的時候有人會得到兩把,輸出自己時有的人沒有武器要輸出....[p.s.]不過出生的時候不一樣,寫在構造函數里不一樣就好啦,這個就不能virtual了
然后這個題也就沒有困難啦...
【魔獸二:代碼】

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 6 using namespace std; 7 8 int Cost[5],Time_Index; 9 int ord1[5]={2,3,4,1,0},ord2[5]={3,0,1,2,4}; 10 char Weapon_Name[3][6]={"sword","bomb","arrow"}; 11 char Warrior_Name[5][7]={"dragon","ninja","iceman","lion","wolf"}; 12 char Headquarter_Name[2][5]={"red","blue"}; 13 14 class Weapon{ 15 char Name[6]; 16 public: 17 Weapon(int which){ 18 strcpy(Name,Weapon_Name[which]); 19 } 20 void print(){printf("It has a %s",Name);} 21 void Name_print(){printf("%s",Name);} 22 }; 23 24 class Warrior{ 25 protected: 26 int HP; 27 char Name[7]; 28 Weapon *w1,*w2; 29 public: 30 Warrior(int HP_,char *s){ 31 memset(Name,0,sizeof(Name)); 32 HP=HP_; 33 strcpy(Name,s); 34 w1=w2=NULL; 35 } 36 virtual void get_weapon(){ 37 w1=new Weapon((Time_Index+1)%3); 38 } 39 void Name_print(){ 40 printf("%s",Name); 41 } 42 virtual void Weapon_print(){ 43 w1->print(); 44 printf("\n"); 45 } 46 ~Warrior(){if(w1) delete w1;if(w2) delete w2;} 47 }; 48 //0 49 class Dragon:public Warrior{ 50 double morale; 51 public: 52 Dragon(int HP,double m_):Warrior(HP,Warrior_Name[0]){morale=m_;} 53 virtual void Weapon_print(){ 54 w1->print(); 55 printf(",and it's morale is %.2lf\n",morale); 56 } 57 }; 58 //1 59 class Ninja:public Warrior{ 60 public: 61 Ninja(int HP):Warrior(HP,Warrior_Name[1]){} 62 virtual void get_weapon(){ 63 w1=new Weapon((Time_Index+1)%3); 64 w2=new Weapon((Time_Index+2)%3); 65 } 66 virtual void Weapon_print(){ 67 w1->print(); 68 printf(" and a "); 69 w2->Name_print(); 70 putchar('\n'); 71 } 72 }; 73 //2 74 class Iceman:public Warrior{ 75 public: 76 Iceman(int HP):Warrior(HP,Warrior_Name[2]){} 77 }; 78 //3 79 class Lion:public Warrior{ 80 int loyalty; 81 public: 82 Lion(int HP,int l_):Warrior(HP,Warrior_Name[3]){loyalty=l_;} 83 virtual void get_weapon(){} 84 virtual void Weapon_print(){ 85 printf("It's loyalty is %d\n",loyalty); 86 } 87 }; 88 //4 89 class Wolf:public Warrior{ 90 public: 91 Wolf(int HP):Warrior(HP,Warrior_Name[4]){} 92 virtual void get_weapon(){}; 93 virtual void Weapon_print(){}; 94 }; 95 96 class Headquarter{ 97 private: 98 char Name[5]; 99 int HP; 100 int Order[5]; 101 int Count[5]; 102 int Warrior_Index; 103 Warrior *cur; 104 bool STOP; 105 public: 106 Headquarter(char *s,int HP_,int* O_){ 107 memset(Name,0,sizeof(Name)); 108 memset(Count,0,sizeof(Count)); 109 strcpy(Name,s); 110 HP=HP_; 111 for(int i=0;i<5;i++) 112 Order[i]=O_[i]; 113 Warrior_Index=-1; 114 cur=NULL; 115 STOP=0; 116 } 117 void Change_HP(int HP_){ 118 HP=HP_; 119 memset(Count,0,sizeof(Count)); 120 Warrior_Index=-1; 121 cur=NULL; 122 STOP=0; 123 }; 124 void Build_Warrior(){ 125 if(STOP) return; 126 Warrior_Index=(Warrior_Index+1)%5; 127 int who=Order[Warrior_Index]; 128 int temp=0; 129 while(Cost[who]>HP && temp<5){ 130 Warrior_Index=(Warrior_Index+1)%5; 131 who=Order[Warrior_Index]; 132 temp++; 133 } 134 if(HP>=Cost[who]){ 135 Count[who]++; 136 HP-=Cost[who]; 137 switch(who){ 138 case 0: cur=new Dragon(Cost[0],(double)HP/Cost[0]);break; 139 case 1: cur=new Ninja(Cost[1]);break; 140 case 2: cur=new Iceman(Cost[2]);break; 141 case 3: cur=new Lion(Cost[3],HP);break; 142 case 4: cur=new Wolf(Cost[4]);break; 143 }; 144 cur->get_weapon(); 145 printf("%03d %s ",Time_Index,Name); 146 cur->Name_print(); 147 printf(" %d born with strength %d,%d ",Time_Index+1,Cost[who],Count[who]); 148 cur->Name_print(); 149 printf(" in %s headquarter\n",Name); 150 cur->Weapon_print(); 151 delete cur; 152 cur=NULL; 153 } 154 else{ 155 printf("%03d %s headquarter stops making warriors\n",Time_Index,Name); 156 STOP=true; 157 } 158 } 159 bool Stop(){return STOP;} 160 }; 161 162 Headquarter r(Headquarter_Name[0],0,ord1),b(Headquarter_Name[1],0,ord2); 163 164 int main(){ 165 #ifndef ONLINE_JUDGE 166 freopen("x.in","r",stdin); 167 freopen("x.out","w",stdout); 168 #endif 169 int Kase,W; 170 scanf("%d",&Kase); 171 for(int T=1;T<=Kase;T++){ 172 printf("Case:%d\n",T); 173 scanf("%d",&W); 174 Time_Index=0; 175 r.Change_HP(W); 176 b.Change_HP(W); 177 for(int i=0;i<5;i++) 178 scanf("%d",&Cost[i]); 179 while(!r.Stop() || !b.Stop()){ 180 r.Build_Warrior(); 181 b.Build_Warrior(); 182 Time_Index++; 183 } 184 } 185 return 0; 186 }
【魔獸世界三:開戰】
這個版本里面的魔獸世界呢...
很重要的兩個任務就是讓你的武士們可以移動起來,然后兩個武士之間會發生互動(打架、搶武器什么的)
下面就詳細講一下啦...
先講一講關於武士的移動!
【武士的移動】
這里的移動涉及到的主要是武士和城市這兩個單位。
而這里不同於之前兩道魔獸的地方是:我不僅要輸出每一個移動,並且在我程序構建出來的這個世界中,武士是真的在移動的。
那么我們還是先考慮這個部分的輸出:
因為城市是分普通城市和指揮部的,所以對應的輸出也是有兩種
先看看普通城市的輸出:(這些都是從提供數據中的out文件里提出來的哦...)
000:10 red iceman 1 marched to city 1 with 117 elements and force 50
再看到達指揮部的輸出:
012:10 red wolf 3 reached blue headquarter with 5 elements and force 150 012:10 blue headquarter was taken
注意到兩種的區別!首先藍色指揮部的名字"blue headquarter"取代了"city %d",其次,在魔獸世界三中,只要有武士到達對方指揮部,對方的指揮部就會被占領,游戲也就結束,此時就需要我們輸出一行"blue headquarter was taken"
【Tips1】這里有一個很容易錯的地方,那就是當前Case結束輸出的時機!如果最后游戲是平局,那么應該是在時間結束的時候也結束輸出的。但是如果中途發生了指揮部被占領的情況就應該結束啦,也就是這個時刻之后的都不用輸出啦...!而在這個地方還有一個要注意:雖然在司令部被占領之后游戲結束,但是這個時刻的輸出還是要輸出完的,比如說紅司令部被占領了,但是這個時刻我才剛剛枚舉了移動到第0個城市的武士,可是還有很多武士也在這個時刻移動,應該把他們都輸出完了才能結束這個Case的輸出!
【Tips2】這里提到一個打程序的小技巧:為了讓我們的代碼不會出錯,我的建議是在要輸出的地方,先寫一個print("");然后把題目中提供的要輸出的信息填進去。然后,再把題目中提到的特殊的信息給挖空拿掉,再在printf的后面想辦法把這些用一些方法給表示出來。這樣的話錯誤率會降到很低很低哦...
比如說對於一個普通城市的輸出,我的代碼呢就長這樣了...
printf("%03d:10 %s %s %d marched to city %d with %d elements and force %d\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,City_Index,HP,MP);
hhh不過我一直以為大家都是這么打的...tips強行湊字數hhh
然后我們再來考慮一下有多個武士,最后輸出這些移動信息的順序:
大的方向呢是從0到n+1的城市方向,並且在同一座城市會先輸出紅色武士的信息,再輸出藍色武士的信息。【Attention】注意啦!這里的城市是指雙方武士移動后所在的城市!而我們通常會把移動武士和輸出武士一塊寫,那么這個時候請注意啦...對於第 i 城市的輸出就要移動的是 i-1 城市的紅方武士和 i+1 城市的藍方武士,具體如下(筆者de出來的第一個bug就是這里寫錯了QwQ)...
下面是筆者的代碼:嗯里面的City是一個Warrior類的指針的二維數組,第一維的下標是這個城市的編號,而第二維的[0]或[1]分別存的是red和blue方的武士指針。[emmm你們可能覺得筆者你....你居然不把City寫成類!好吧...我就是懶hhh因為這題中的City好像沒有什么用]
for(int i=0;i<=City_Amount+1;i++){
if(i>0)
if(City[i-1][0]!=NULL) City[i-1][0]->Process(); if(i<=City_Amount) if(City[i+1][1]!=NULL) City[i+1][1]->Process();
}
看完上面這個代碼,很多人就放松了警惕(包括當初的筆者我自己...)感覺很滿意呀...代碼思路這么清楚了,可以安心寫process去了。這時一個帥氣的男生停在了你的面前:STOP!(咳咳,就是筆者我...hhhh)這樣打會有一個問題,那就是當我移動我的紅色方的武士的時候,他肯定會到下一個城市 (因為這里還沒有開始打架...) 那么,當我枚舉下一個城市里的紅色戰士的時候,發現:誒,你不是從上個城市過來的么?我原來那個武士呢?算了算了就移動你把...然后不知不覺一輪枚舉直接把我紅方的武士送進了對面的指揮部...
唔,那怎么解決呢?筆者的辦法是建立一個新城市,讓移動后的武士先住在新城市里,等舊城市的所有武士都走完了,再讓新城市里的人回來...具體過程呢可以見最后的代碼啦...。
好了,現在終於安心了,可以開始寫我們的Process函數了。
首先我們注意到題目中對移動這個操作有三種人:Iceman(邊走邊掉血),Lion(越走越膽小),Others。
那么這個Process肯定就是一個虛函數了,那我們分別看一下這三種人。先是Others。
【Others】 我走路走的好好的!
不過好好的走需要什么呢?
需要的信息1.我當前的位置 2.我往哪邊走,那么這兩個信息可以變成兩個值在初始化的時候搞定!
1.int City_Index 表示自己的位置 red初始值是0 ,blue 是 N+1
2.int Direction 表示自己的方向 red用1 blue用-1
這樣設計的話 City_Index+=Direction; 就讓這個武士走完了誒!
不行不行,你還得照顧一下那些在City里找武士的人呀...所以我們還得修改外層指向這個武士的指針:
City[City_Index][Direction<0]=NULL; City_Index+=Direction; New_City[City_Index][Direction<0]=this;
這里很多小伙伴就不知道Direction<0是什么了...hhh忽然感覺自己是科普小天使....
比如< > ==這種符號都是雙目的運算符,它們也是有返回值的,是一個bool類型!如果正確呢那就是1,不正確呢那就是0
所以如果我把red定為0,blue定為1的話Direction<0就可以知道這個武士是哪一家的了!
然后上面代碼的New_City就是我所設想的新城市啦...
好了,現在我們移動了武士自己,還改了城市里的指針,是不是結束啦!嗯嗯,別忘了加上之前的輸出哦
1 virtual void Process(){ 2 City[City_Index][Direction<0]=NULL; 3 City_Index+=Direction; 4 New_City[City_Index][Direction<0]=this; 5 if(City_Index==0){ 6 Red_Lost=true; 7 printf("%03d:10 blue %s %d reached red headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 8 printf("%03d:10 red headquarter was taken\n",Time_Index); 9 } 10 else if(City_Index==City_Amount+1){ 11 Blue_Lost=true; 12 printf("%03d:10 red %s %d reached blue headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 13 printf("%03d:10 blue headquarter was taken\n",Time_Index); 14 } 15 else 16 printf("%03d:10 %s %s %d marched to city %d with %d elements and force %d\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,City_Index,HP,MP); 17 }
不要看代碼這么長,其實主要是輸出有點長啦....QwQ,最重要的只有三行而已啦...
好了,下面討論一下Iceman
【Iceman】我前進就要掉血!但我還是要前進!
就記得移動的時候HP-=HP/10哦...注意啦這個掉血是當前血量的10%,所以不用擔心Iceman行軍路上失血過多而死hhh
然后是我們的Lion
【Lion】我不想走啦!再逼我我就逃跑!
嗯嗯記得就是把Loyalty-=K就好啦,然后要補一個判斷,看看Loyalty是不是<0啦...
嗯嗯這個里面的話呢我的處理就是逃兵一律殺掉emmm直接讓這個逃跑的被上層delete掉就好啦
【武士的戰斗】
武士戰斗最重要的兩個信息:武士、武器
而這兩個就分別是程序中最重要的兩個類。
這一題中的武器數目從最多2很快提升到了10,同時武器的屬於關系也不再固定,而是可以在不同的武士之間傳遞。而對於這樣的關系,我認為指針是最好的選擇了。因為武器的對象在傳遞的時候其實是不會消失的,所以當一個武器從A到B手上時,只用把B中一個空的指針變成A的指針,然后A中指向這個武器的指針定為空就完成了一次武器的交接。(不過筆者還打聽到一種比較巧妙的方法哦:這個題里可以不要武器類的,因為武器這個對象其實沒有具體的什么值,它的攻擊是主人根據主人來的,它唯一具有自己的屬性就是耐久度了,而這個我們用一個數組來當成武器庫存耐久度也不是很麻煩,而且這樣的話其實傳遞武器排序武器都是很簡單的了...不過筆者還是講自己的類的做法吧)
1 class Warrior{ 2 int Weapon_Count;//Weapon_Count表示武器的數量 3 Weapon* w[10]; 4 };
上面解決了武士和武器之間的關系,我們在這個基礎上再來看看打斗中的各種操作應該如何實現
還是根據故事的邏輯來看,把戰斗按照時間順序分為戰前、戰中、和戰后三個階段:(p.s.當然戰斗發生的條件是這個城市里有兩個武士哈)
【戰前】
戰前有一件對戰斗很重要的事情:那就是wolf會搶奪武器!
邏輯上:這是一件很獨特的事情,可以用virtual Before_Fight()來處理,也可以if (Is_Wolf()) Rob_Weapon(Enermy)來處理
操作上:在搶奪之前:要知道搶奪發生的條件:
1,對方不是wolf,這樣需要一個判斷函數:virtual bool Is_Wolf();然后只有wolf類返回true其他都返回false就可以了
2.自己的武器數量<10 3.對方的武器數量>0,知道自己可以搶奪之后,還不能馬上搶,這中間還有一個很重要的操作:你要搶編號最小的那一類武器,而且如果搶不完要優先搶使用次數少的。這樣的話我們就要給敵人的武器按照剛才的方法排序,筆者的習慣是用系統函數sort()搭配自己寫的cmp來排序,可以把自己的cmp函數發上來供大家參考:
bool cmp(const Weapon *A,const Weapon *B){//這里我們是想給Warrior中的Weapon *w[]數組排序,所以應該比較兩個Weapon*的大小 if(A==NULL) return false;//我們要讓NULL的元素都往后放,而sort中return true;就會把A放在前面,所以這里應該返回false if(B==NULL) return true; if(A->Index!=B->Index) return A->Index<B->Index;//Index是武器的種類的編號(Sword-0,Bomb-1,Arrow-2),編號小的在前面,可以看到這里還是很好的運用了<的返回值來使得代碼精簡(如果A的編號<B那么就會return true讓A在前面) return A->Can_use>B->Can_use;//Can_use是武器的耐久度,初始值是2,bomb每使用一次會-2,Arrow使用一次會-1,耐久度大的放前面,所以是>號 }
[ 在上面的cmp函數中,我順帶介紹了自己關於Can_use的這個設計...嗯嗯感覺我這樣挺漂亮的算,一個好設計hhh,然后同時也科普了一下cmp函數的寫法:return true會讓A放在B的前面,然后運用<和>號可以讓自己的代碼很精簡。]
然后就是搶奪的過程了,這個就是武器從屬權的一個傳遞,在上面的指針部分也有介紹。如果還有不懂可以看下面的搶奪代碼

1 virtual void Before_fight(Warrior *Enermy){ 2 if(Enermy->Is_Wolf()) return; 3 if(Weapon_Count<10 && Enermy->Weapon_Count>0){ 4 sort(w,w+10,cmp); 5 sort(Enermy->w,Enermy->w+10,cmp); 6 int Min_Index=Enermy->w[0]->Index,Amount=0; 7 for(int i=0;Enermy->w[i]!=NULL && Enermy->w[i]->Index==Min_Index && Weapon_Count<10 && i<Enermy->Weapon_Count+Amount;i++){ 8 w[Weapon_Count++]=Enermy->w[i],Amount++; 9 Enermy->Weapon_Count--; 10 Enermy->w[i]=NULL; 11 } 12 printf("%03d:35 %s wolf %d took %d %s from %s %s %d in city %d\n",Time_Index,Headquarter_Name[Direction<0],Born_Number,Amount,Weapon_Name[Min_Index],Headquarter_Name[Direction>0],Enermy->Name,Enermy->Born_Number,City_Index); 13 } 14 }
【戰中】
戰斗的過程是兩個人互相使用武器的過程。
首先,還是需要先給兩個人的武器排好序來確定使用的順序,然后就類似魔獸世界一二中生產武士的方法來循環這個武器的順序:
1 int temp=0; 2 while(w[Weapon_Index=(Weapon_Index+1)%10]==NULL && temp<10) temp++;
上面的代碼就是幫助我找到下一個可以用的武器(夠精簡吧hhh),然后就是使用武器的過程了,這里武器的使用,只和使用者、被使用者、武器類型有關,而與具體的武器對象唯一的關聯就是耐久度這個屬性(所以才會誕生出不設置武器類的做法),那么我們在使用武器的時候就需要把這些因素都考慮進去,同時不同類型的武器是不一樣的,所以我們需要用到虛函數來處理。在這個過程中是可能導致武士死亡的,但是我們現在不能急着把武士給delete掉,因為整個武士的最頂級指針是最上層的City數組,而且等一會還有敵人來收繳武器,所以我們可以用一個bool Die來表示一個武士是否死亡,然后延遲delete的時間,在打斗的時候不delete,等戰斗發生完了以后回到上層再去刪除。
然后用完了武器就會面臨武器可能失效的問題,這個時候要刪除這個武器,因為武器的最上層控制者就是武士,所以這個delete的操作也應該是放在武士的函數里執行的。同時記得w[i]=NULL和Weapon_Count--的操作。
戰中還有一個很重要的過程就是宣布戰中結束,進入戰后。有兩種情況是很容易判斷的:1. 有一方死亡時 2. 雙方都沒有兵器時
還有一種情況是比較復雜的,那就是雙方的攻擊力<5(這時使用Sword將會造成0傷害),然后互相打到地老天荒,這個時候怎么辦呢?
室友想到一種很偷懶的方法,就是執行完1000輪還不結束,我就宣告平局...emmm顯然可以造數據卡掉你嘛,,不過最后數據確實沒有卡(因為現在還沒有看到任何一個TLE的同學...)hhh懷疑那些運行比我(2ms)慢的(24ms)可能就是用了這種方法?
我的辦法會比較符合邏輯一點,就是我會有一個倒計時Time_Tick,它的初始值是兩人的武器數量中大的一個的兩倍,那么當倒計時執行完之后,即使是武器數量大的那個人也只可能有sword這件武器了,就進入了白熱化階段,然后你再判斷一下雙方的攻擊力,或者判斷一下場面是不是還有變化,這樣就可以結束了。
【戰后】
戰后首先會有一個叫Dragon的兄弟,如果沒死他就會yell一下,記得輸出。
然后戰后主要就是死亡之后的收繳兵器。因為兵器的最高控制者就是武士,所以寫在武士的函數里(這里提到了很多次最高控制者,因為筆者程序中的指針往往表示的是控制關系,所以每當我想刪除一個東西的時候,就需要去考慮誰的指針還是指着它的,那么我會讓這個刪除操作發生在哪個指揮者的函數中)
而收繳兵器的過程和wolf搶兵器的過程是幾乎一樣的,只是不只可以搶一類武器了,去掉那個終止條件就可以了。
那么戰斗也就分析完了...
最后來看看怎么讓你的程序可以AC吧:
【關於如何從CE->WA】
啊,這個過程可以說是很痛苦了...不過相信你們可以的hhh
【最后:關於怎樣調試代碼(WA/RE/TLE->AC)】
首先呢,筆者自己對這道題大概debug了兩天左右...其實感覺自己debug已經很有方法了,只是前面寫代碼的時候好像太粗糙了
[其中的情緒變化大約是:[冷靜] 這里有錯誒?-> [急躁] 怎么還有錯?!-> [抓狂] 還有哪有錯呀?-> [目光呆滯] 沒錯了....吧...]
所以,大家自己敲第一遍代碼的時候一定要謹慎!小心!要跟着自己的光標一起思考這個位置應該是什么,這樣打是不是一定對?
這個過程一定要謹慎哦...為了大家調試代碼的愉悅度!
調試的過程:
【關於調試工具】
emmm 筆者用的是gdb這種原始又實用的東西 hhh然后大家就各自用自己的IDE好了!
首先大家需要知道基本的【文件操作】來讓自己的程序可以讀一個比較大的輸入文件,也能把輸出的東西存到一個文件里面去
1 #ifndef ONLINE_JUDGE 2 freopen("data.in","r",stdin); 3 freopen("my_code.out","w",stdout); 4 #endif
只要把這個東西加在int main的后面就可以啦(前面需要包含<cstdio>庫),其中2、3行是正式的代碼啦,然后1、4行可以讓你的程序在自己本機運行的時候用2、3里面的語句,但是交到openjudge上的時候又不會調用里面的語句,是不是很厲害呢hhhh
然后大家既然都輸出到文件里面了,那就當然也要知道一種可以比較文件是否相同的辦法!因為要肉眼比較輸出有一點麻煩...【不過我就是這么做的】,如果你要這么做的話,請選一個好看的文本編輯器hhh:肉眼比較一次可以比較一版,就是將兩個out調整到相同的位置(這個調整到相同位置也是很有技巧的,你要靈活的使用鼠標和光標來調整位置hhh),然后反復切換,在來回切換的時候不同點會很明顯,這樣你就可以找到了!
然后言歸正傳還是講一講【怎么比較文件】
首先是Linux最熟悉啦:在當前位置打開終端,然后輸入:diff -c my_code.out std.out 就可以比較了(其中-c是可以幫你輸出錯誤行號的,感覺一看就明白啦...然后my_code.out是自己的輸出結果,std是正確的結果 [這個正是我用的hhh])
然后是Windows下:先敲一下cmd調出命令行,然后cd 后面加你文件存的位置,這個位置怎么找呢?就是你可以打開你文件所在的文件夾,然后單擊一下,然后cd + 這個藍色的東西...+回車 (我真是科普小天使呀hhh)
然后你就進入到了當前文件夾,然后輸入命令:fc my_code.out std.out+回車就可以啦...然后這個不同可能很多,導致控制台裝不下..[hhh我就經常發生這種情況]
我們可以fc my_code.out std.out > Difference.txt 這樣就可以把比較信息輸出到這個txt里面去了,上面Linux也是一樣的哦
然后是根據讀者范圍友情添加的Mac大神應該怎樣做呢?
Hhh我也不知道啦...不過下面這個博客提供了兩種文件比較的工具,可以看一下啦 :https://blog.csdn.net/wowfly98/article/details/52774275
【關於數據】
想要調試好,你需要獲得一些比較優秀的數據,course上提供的數據就挺棒的(因為過了那個就能A啦!hhh)
不過呢course上的數據是多組的,為了方便調試,我們可以一組一組的拆開來試【by the way 不知道blog怎么上傳文件QwQ那就大家自己來吧...hhh我的經驗是第一二組數據是很好的數據,后面的九十也都是比較強的,然后中間有一些是很短的數據,價值不高】
然后對於一組數據我們也是可以拆的!至於為什么要拆呢?請聽下面的筆者哭訴節目:
筆者當時經常會遇到RE。RE是一種比WA更難受的東西,它有時是很良心的:會告訴你卡在哪一行了...有時不會告訴你,它就是卡住了!(太傲嬌了吧)
RE在這種涉及到一堆指針的題目里面是十分常見的,以為你只要delete了一個空的地址就會報錯,然后訪問了一個空指針的內容也會RE,所以delete之后一定記得把指針賦值為NULL!記得要訪問之前判斷是不是NULL!
然后這里涉及到的指針主要是City和New_City的Warrior指針,還有就是Warrior里面放Weapon的w[]指針數組。City要記得每個Case之后要清空一下,順帶把指向的戰士消除掉,然后w[]數組是很容易出錯的一個地方!因為中途我會有武器損耗、武器被搶等等都可能將w[i]中間某一個位置給挖空!這樣的話w[]中有指向的范圍就不是連續的了!Weapon_Count也只能給你計數了,所以每次我想要一塊連續的武器存儲一定要記得排序,而且排序的范圍一定是[0,10)哦(筆者有n次RE都是這個原因,因為需要用Weapon的地方太多了,改的時候記得一起改完)
不過也不一定都是這種情況的RE,對於RE我們有傳統方案就是設置斷點,在這種時間很多的題中的斷點一般長這樣:
1 if(Time_Index==7) 2 printf("Stop\n");
通過不停地修改Index后面的數值就可以知道在哪個時間段里RE啦,然后再單獨進入里面調試。
當然啦,這個題有一個得天獨厚的優勢:就是輸入數據中是有時間的,所以你只要修改后面的時間也可以打到通過設置斷點找到RE的時間點的過程,同時你也可以通過修改時間找到一個里RE比較近的可以運行的時間去觀察輸出數據中關於時間的輸出來方便自己設置斷點來檢查RE。
然后關於RE的情況在上面已經講得比較豐富了...
下面來講講WA的調試方法。
魔獸三這樣的題其實是很容易找到WA的問題的,因為我們是面向對象的程序,而且我們的輸出一定是有一個對象的,那么你在哪一個位置WA了,一定是針對某一個對象的儲存值出現了問題,而我們是可以在這個WA的地方之前找到關於這個對象所有的報告的,方法就是control+F然后輸入你想找的對象名字 [這個時候又需要你有一個優秀的文本編輯器啦hhh]比如我的界面是這樣子的:
比如說打架的結果不同了,我就去看一下前面匯報的兩個人的血量生命值還是有武器數量什么的...然后就可以手上模擬看看程序在哪兒出問題啦..
總而言之WA雖然可能的誘因無窮無盡,但是相對於RE也是更容易找到而且找的過程更加的有趣的hhh,當然關於WA的誘因,最重要的一部分就是題里面的細節
那我就列舉一些我印象深刻的害人的細節吧:
1.題面描述里:這里在40分鍾和50分鍾之間混入了一個10分鍾輸出...但事實上最后輸出是需要在10分的時刻輸出的(詳細見上面描述行軍的部分)
2.Dragon沒有戰死就會歡呼
3.Ninja使用炸彈自己不會掉血
4.使用炸彈可能把自己炸死而敵人不死,這時候敵人也會收繳你的武器
5.當指揮部被占領后游戲結束,但是還是要輸出這個時刻所有武士的移動報告
6.Wolf不會搶Wolf的武器;Wolf只會搶一種武器;如果武器是用cnt[3]存的話,Wolf要小心一起搶光使得自己的武器多於10把的情況。
如果是和筆者一樣用的數組存儲武器的話,要注意下面這個程序段中的for循環中的終止條件應該寫成:i<Enermy->Weapon_Count+Amount
而不能寫成i<Enermy->Weapon_Count,因為每當我搶一件武器,我的Weapon_Count都會-1,但是我搶的武器確實從頭搶起的,所以如果我寫成了<Weapon_Count的話就會使得最后幾件武器搶不到了,所以這個終止條件應該是搶武器之前的武器數量,也就是Weapon_Count+Amount了。
1 sort(w,w+10,cmp); 2 sort(Enermy->w,Enermy->w+10,cmp); 3 int Min_Index=Enermy->w[0]->Index,Amount=0; 4 for(int i=0;Enermy->w[i]!=NULL && Enermy->w[i]->Index==Min_Index && Weapon_Count<10 && i<Enermy->Weapon_Count+Amount;i++){ 5 w[Weapon_Count++]=Enermy->w[i],Amount++; 6 Enermy->Weapon_Count--; 7 Enermy->w[i]=NULL; 8 }
7.輸入的時間的單位是分鍾;如果雙方司令部都停止生產了而時間還沒有到,也得接着輸出(不同於魔獸一二)
8.這個題中的指揮部生產士兵,如果下一個不能生產了就不會再生產,而不需要去找下一個能產的(不同於一二)
9.這個題中也會分發武器,但是不需要輸出分發武器的信息;這一題中Lion出生也會發兵器(不同於魔獸二)。
10.使用swith case的時候一定記得break!
[上面都是筆者的痛苦教訓,希望你們可以省些力氣哦...]
【魔獸世界三:代碼】

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 6 using namespace std; 7 8 class Warrior; 9 10 int Loyalty_Decrease,City_Amount; 11 int Cost[5],Power[5],Time_Index; 12 int ord1[5]={2,3,4,1,0},ord2[5]={3,0,1,2,4}; 13 Warrior* City[22][2];//0->red & 1->blue; 14 Warrior* New_City[22][2]; 15 char Weapon_Name[3][6]={"sword","bomb","arrow"}; 16 char Warrior_Name[5][7]={"dragon","ninja","iceman","lion","wolf"}; 17 char Headquarter_Name[2][5]={"red","blue"}; 18 bool Red_Lost,Blue_Lost; 19 20 inline int MAX(int a,int b){return a>b?a:b;} 21 22 class Weapon{ 23 public: 24 int Can_use; 25 int Index; 26 char Name[6]; 27 Weapon(char* s,int I_){ 28 strcpy(Name,s); 29 Index=I_; 30 Can_use=2; 31 } 32 int Use(){return Can_use;} 33 virtual void AD(Warrior *Owner,Warrior *Enermy); 34 }; 35 36 bool cmp(const Weapon *A,const Weapon *B){ 37 if(A==NULL) return false; 38 if(B==NULL) return true; 39 if(A->Index!=B->Index) return A->Index<B->Index; 40 return A->Can_use>B->Can_use; 41 } 42 43 class Sword: public Weapon{ 44 public: 45 Sword():Weapon(Weapon_Name[0],0){}; 46 virtual void AD(Warrior *Owner,Warrior *Enermy); 47 }; 48 49 class Bomb: public Weapon{ 50 public: 51 Bomb():Weapon(Weapon_Name[1],1){}; 52 virtual void AD(Warrior *Owner,Warrior *Enermy); 53 }; 54 55 class Arrow: public Weapon{ 56 public: 57 Arrow():Weapon(Weapon_Name[2],2){}; 58 virtual void AD(Warrior *Owner,Warrior *Enermy); 59 }; 60 61 class Warrior{ 62 public: 63 int HP,MP; 64 int City_Index,Direction; 65 char Name[7]; 66 int Born_Number; 67 int Weapon_Count; 68 int Weapon_Index; 69 bool Die; 70 Weapon* w[10]; 71 Warrior(int HP_,int MP_,char *s,int B_,int C_,int D_){ 72 memset(Name,0,sizeof(Name)); 73 memset(w,0,sizeof(w)); 74 Weapon_Count=0; 75 HP=HP_; 76 MP=MP_; 77 strcpy(Name,s); 78 City_Index=C_; 79 Direction=D_; 80 Die=false; 81 Born_Number=B_; 82 } 83 virtual void get_weapon(){ 84 switch((Time_Index+1)%3){ 85 case 0: w[Weapon_Count++]=new Sword;break; 86 case 1: w[Weapon_Count++]=new Bomb;break; 87 case 2: w[Weapon_Count++]=new Arrow;break; 88 }; 89 } 90 virtual void After_Born(){} 91 void Hurt(int Damage){ 92 HP-=Damage; 93 if(HP<=0) Die=true; 94 } 95 virtual bool Escape(){return false;} 96 virtual bool Is_Wolf(){return false;} 97 virtual bool Is_Ninja(){return false;} 98 virtual void Before_fight(Warrior *Enermy){} 99 void Use_Weapon(Warrior *Enermy){ 100 if(!Weapon_Count) return ; 101 int temp=0; 102 while(w[Weapon_Index=(Weapon_Index+1)%10]==NULL && temp<10) temp++; 103 w[Weapon_Index]->AD(this,Enermy); 104 if(!w[Weapon_Index]->Use()){ 105 delete w[Weapon_Index],w[Weapon_Index]=NULL; 106 Weapon_Count--; 107 } 108 if(Enermy->Die && !Die){ 109 //in order to make the Weapon is in the w[Weapon_Count]'s place You have to reorder it before get others 110 sort(w,w+10,cmp); 111 if(Weapon_Count<10 && Enermy->Weapon_Count>0){ 112 sort(Enermy->w,Enermy->w+Enermy->Weapon_Count,cmp); 113 for(int i=0;i<Enermy->Weapon_Count && Weapon_Count<10;i++) 114 w[Weapon_Count++]=Enermy->w[i]; 115 } 116 } 117 if(!Enermy->Die && Die){ 118 sort(Enermy->w,Enermy->w+10,cmp); 119 if(Enermy->Weapon_Count<10 && Weapon_Count>0){ 120 sort(w,w+Weapon_Count,cmp); 121 for(int i=0;i<Weapon_Count && Enermy->Weapon_Count<10;i++) 122 Enermy->w[Weapon_Count++]=w[i]; 123 } 124 } 125 } 126 /*Time_Tick is a clock to test the max time that both are still*/ 127 void Fight_First(Warrior *A){ 128 if(Weapon_Count) sort(w,w+10,cmp); 129 if(A->Weapon_Count) sort(A->w,A->w+10,cmp); 130 Weapon_Index=-1; 131 A->Weapon_Index=-1; 132 int rec_HP=HP,rec_HP_A=A->HP,rec_W=Weapon_Count,rec_W_A=A->Weapon_Count; 133 int Time_Tick=2*MAX(rec_W,rec_W_A); 134 while(!Die && !A->Die && (Weapon_Count || A->Weapon_Count) ){ 135 Use_Weapon(A); 136 if(!A->Die) 137 A->Use_Weapon(this); 138 if(--Time_Tick==0){ 139 if(rec_HP==HP && rec_HP_A==A->HP && rec_W==Weapon_Count && rec_W_A==A->Weapon_Count) 140 break; 141 else{ 142 rec_HP=HP,rec_HP_A=A->HP,rec_W=Weapon_Count,rec_W_A=A->Weapon_Count; 143 Time_Tick=MAX(rec_W,rec_W_A); 144 } 145 } 146 } 147 Warrior *r; 148 Warrior *b; 149 if(Direction>0) r=this,b=A; 150 else r=A,b=this; 151 if(r->Die){ 152 if(b->Die) printf("%03d:40 both red %s %d and blue %s %d died in city %d\n",Time_Index,r->Name,r->Born_Number,b->Name,b->Born_Number,r->City_Index); 153 else printf("%03d:40 blue %s %d killed red %s %d in city %d remaining %d elements\n",Time_Index,b->Name,b->Born_Number,r->Name,r->Born_Number,r->City_Index,b->HP); 154 } 155 else{ 156 if(b->Die) printf("%03d:40 red %s %d killed blue %s %d in city %d remaining %d elements\n",Time_Index,r->Name,r->Born_Number,b->Name,b->Born_Number,r->City_Index,r->HP); 157 else printf("%03d:40 both red %s %d and blue %s %d were alive in city %d\n",Time_Index,r->Name,r->Born_Number,b->Name,b->Born_Number,r->City_Index); 158 } 159 } 160 virtual void Process(){ 161 City[City_Index][Direction<0]=NULL; 162 City_Index+=Direction; 163 New_City[City_Index][Direction<0]=this; 164 if(City_Index==0){ 165 Red_Lost=true; 166 printf("%03d:10 blue %s %d reached red headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 167 printf("%03d:10 red headquarter was taken\n",Time_Index); 168 } 169 else if(City_Index==City_Amount+1){ 170 Blue_Lost=true; 171 printf("%03d:10 red %s %d reached blue headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 172 printf("%03d:10 blue headquarter was taken\n",Time_Index); 173 } 174 else 175 printf("%03d:10 %s %s %d marched to city %d with %d elements and force %d\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,City_Index,HP,MP); 176 } 177 void Print_Weapon(){ 178 int cnt[3]; 179 for(int i=0;i<3;i++) cnt[i]=0; 180 for(int i=0;i<10;i++) 181 if(w[i]!=NULL) cnt[w[i]->Index]++; 182 printf("%03d:55 %s %s %d has %d sword %d bomb %d arrow and %d elements\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,cnt[0],cnt[1],cnt[2],HP); 183 } 184 virtual void After_Fight(Warrior* Enermy){} 185 }; 186 //0 187 class Dragon:public Warrior{ 188 public: 189 Dragon(int HP,int MP_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[0],B_,C_,D_){} 190 virtual void After_Fight(Warrior* Enermy){ 191 printf("%03d:40 %s dragon %d yelled in city %d\n",Time_Index,Headquarter_Name[Direction<0],Born_Number,City_Index); 192 } 193 }; 194 //1 195 class Ninja:public Warrior{ 196 public: 197 Ninja(int HP,int MP_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[1],B_,C_,D_){} 198 virtual void get_weapon(){ 199 switch((Time_Index+1)%3){ 200 case 0: w[Weapon_Count++]=new Sword;break; 201 case 1: w[Weapon_Count++]=new Bomb;break; 202 case 2: w[Weapon_Count++]=new Arrow;break; 203 }; 204 switch((Time_Index+2)%3){ 205 case 0: w[Weapon_Count++]=new Sword;break; 206 case 1: w[Weapon_Count++]=new Bomb;break; 207 case 2: w[Weapon_Count++]=new Arrow;break; 208 }; 209 } 210 virtual bool Is_Ninja(){return true;} 211 }; 212 //2 213 class Iceman:public Warrior{ 214 public: 215 Iceman(int HP,int MP_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[2],B_,C_,D_){} 216 virtual void Process(){ 217 City[City_Index][Direction<0]=NULL; 218 City_Index+=Direction; 219 New_City[City_Index][Direction<0]=this; 220 HP-=HP/10; 221 if(City_Index==0){ 222 Red_Lost=true; 223 printf("%03d:10 blue %s %d reached red headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 224 printf("%03d:10 red headquarter was taken\n",Time_Index); 225 } 226 else if(City_Index==City_Amount+1){ 227 Blue_Lost=true; 228 printf("%03d:10 red %s %d reached blue headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 229 printf("%03d:10 blue headquarter was taken\n",Time_Index); 230 } 231 else 232 printf("%03d:10 %s %s %d marched to city %d with %d elements and force %d\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,City_Index,HP,MP); 233 } 234 }; 235 //3 236 class Lion:public Warrior{ 237 int loyalty; 238 public: 239 Lion(int HP,int MP_,int l_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[3],B_,C_,D_){loyalty=l_;} 240 virtual void After_Born(){ 241 printf("Its loyalty is %d\n",loyalty); 242 } 243 virtual bool Escape(){return loyalty<=0;} 244 virtual void Process(){ 245 City[City_Index][Direction<0]=NULL; 246 City_Index+=Direction; 247 New_City[City_Index][Direction<0]=this; 248 loyalty-=Loyalty_Decrease; 249 if(City_Index==0){ 250 Red_Lost=true; 251 printf("%03d:10 blue %s %d reached red headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 252 printf("%03d:10 red headquarter was taken\n",Time_Index); 253 } 254 else if(City_Index==City_Amount+1){ 255 Blue_Lost=true; 256 printf("%03d:10 red %s %d reached blue headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 257 printf("%03d:10 blue headquarter was taken\n",Time_Index); 258 } 259 else 260 printf("%03d:10 %s %s %d marched to city %d with %d elements and force %d\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,City_Index,HP,MP); 261 } 262 }; 263 //4 264 class Wolf:public Warrior{ 265 public: 266 Wolf(int HP,int MP_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[4],B_,C_,D_){} 267 virtual void get_weapon(){} 268 virtual void Weapon_print(){} 269 virtual bool Is_Wolf(){return true;} 270 virtual void Before_fight(Warrior *Enermy){ 271 if(Enermy->Is_Wolf()) return; 272 if(Weapon_Count<10 && Enermy->Weapon_Count>0){ 273 sort(w,w+10,cmp); 274 sort(Enermy->w,Enermy->w+10,cmp); 275 int Min_Index=Enermy->w[0]->Index,Amount=0; 276 for(int i=0;Enermy->w[i]!=NULL && Enermy->w[i]->Index==Min_Index && Weapon_Count<10 && i<Enermy->Weapon_Count+Amount;i++){ 277 w[Weapon_Count++]=Enermy->w[i],Amount++; 278 Enermy->Weapon_Count--; 279 Enermy->w[i]=NULL; 280 } 281 printf("%03d:35 %s wolf %d took %d %s from %s %s %d in city %d\n",Time_Index,Headquarter_Name[Direction<0],Born_Number,Amount,Weapon_Name[Min_Index],Headquarter_Name[Direction>0],Enermy->Name,Enermy->Born_Number,City_Index); 282 } 283 } 284 }; 285 286 void Weapon::AD(Warrior *Owner,Warrior *Enermy){} 287 void Sword::AD(Warrior*Owner,Warrior *Enermy){ Enermy->Hurt(Owner->MP/5);} 288 void Bomb::AD(Warrior*Owner,Warrior *Enermy){ 289 Owner->w[Owner->Weapon_Index]->Can_use-=2; 290 Enermy->Hurt(Owner->MP*2/5); 291 if(!Owner->Is_Ninja()) 292 Owner->Hurt(Owner->MP*2/5/2); 293 } 294 void Arrow::AD(Warrior*Owner,Warrior *Enermy){ 295 Owner->w[Owner->Weapon_Index]->Can_use--; 296 Enermy->Hurt(Owner->MP*3/10); 297 } 298 299 class Headquarter{ 300 private: 301 char Name[5]; 302 int HP; 303 int Order[5]; 304 int Count[5]; 305 int Warrior_Index; 306 Warrior *cur; 307 bool STOP; 308 public: 309 Headquarter(char *s,int HP_,int* O_){ 310 memset(Name,0,sizeof(Name)); 311 memset(Count,0,sizeof(Count)); 312 strcpy(Name,s); 313 HP=HP_; 314 for(int i=0;i<5;i++) 315 Order[i]=O_[i]; 316 Warrior_Index=-1; 317 cur=NULL; 318 STOP=0; 319 } 320 void Change_HP(int HP_){ 321 HP=HP_; 322 memset(Count,0,sizeof(Count)); 323 Warrior_Index=-1; 324 cur=NULL; 325 STOP=0; 326 }; 327 void Build_Warrior(){ 328 if(STOP) return; 329 int Born_Place,Direct; 330 if(Name[0]=='r') Born_Place=0,Direct=1; 331 else Born_Place=City_Amount+1,Direct=-1; 332 Warrior_Index=(Warrior_Index+1)%5; 333 int who=Order[Warrior_Index]; 334 if(HP>=Cost[who]){ 335 Count[who]++; 336 HP-=Cost[who]; 337 switch(who){ 338 case 0: cur=new Dragon(Cost[0],Power[0],Time_Index+1,Born_Place,Direct);break; 339 case 1: cur=new Ninja(Cost[1],Power[1],Time_Index+1,Born_Place,Direct);break; 340 case 2: cur=new Iceman(Cost[2],Power[2],Time_Index+1,Born_Place,Direct);break; 341 case 3: cur=new Lion(Cost[3],Power[3],HP,Time_Index+1,Born_Place,Direct);break; 342 case 4: cur=new Wolf(Cost[4],Power[4],Time_Index+1,Born_Place,Direct);break; 343 }; 344 cur->get_weapon(); 345 printf("%03d:00 %s %s %d born\n",Time_Index,Name,Warrior_Name[who],Time_Index+1); 346 cur->After_Born(); 347 if(Name[0]=='r') City[0][0]=cur; 348 else City[City_Amount+1][1]=cur; 349 cur=NULL; 350 } 351 else{ 352 //printf("%03d:00 %s headquarter stops making warriors\n",Time_Index,Name); 353 STOP=true; 354 } 355 } 356 void Print_HP(){ 357 printf("%03d:50 %d elements in %s headquarter\n",Time_Index,HP,Name); 358 } 359 bool Stop(){return STOP;} 360 }; 361 362 Headquarter r(Headquarter_Name[0],0,ord1),b(Headquarter_Name[1],0,ord2); 363 364 int main(){ 365 #ifndef ONLINE_JUDGE 366 freopen("x.in","r",stdin); 367 freopen("x.out","w",stdout); 368 #endif 369 int Kase,W,Time,Time_Sum; 370 scanf("%d",&Kase); 371 for(int T=1;T<=Kase;T++){ 372 printf("Case %d:\n",T); 373 scanf("%d%d%d%d",&W,&City_Amount,&Loyalty_Decrease,&Time); 374 for(int j=0;j<=City_Amount+1;j++)City[j][0]=City[j][1]=NULL; 375 Time_Index=Time_Sum=0; 376 r.Change_HP(W); b.Change_HP(W); 377 Red_Lost=Blue_Lost=false; 378 for(int i=0;i<5;i++) 379 scanf("%d",&Cost[i]); 380 for(int i=0;i<5;i++) 381 scanf("%d",&Power[i]); 382 while(Time_Sum<=Time){ 383 //Time:0 384 r.Build_Warrior(); 385 b.Build_Warrior(); 386 //Time:5 387 if(Time<Time_Sum+5) break; 388 for(int i=0;i<=City_Amount+1;i++){ 389 if(City[i][0]!=NULL && City[i][0]->Escape()){ 390 printf("%03d:05 red lion %d ran away\n",Time_Index,City[i][0]->Born_Number); 391 delete City[i][0],City[i][0]=NULL; 392 } 393 if(City[i][1]!=NULL && City[i][1]->Escape()){ 394 printf("%03d:05 blue lion %d ran away\n",Time_Index,City[i][1]->Born_Number); 395 delete City[i][1],City[i][1]=NULL; 396 } 397 } 398 //Time:10 399 if(Time<Time_Sum+10) break; 400 for(int i=0;i<=City_Amount+1;i++){ 401 if(i>0) 402 if(City[i-1][0]!=NULL) City[i-1][0]->Process(); 403 if(i<=City_Amount) 404 if(City[i+1][1]!=NULL) City[i+1][1]->Process(); 405 } 406 if(Blue_Lost || Red_Lost) break; 407 for(int i=0;i<=City_Amount+1;i++){ 408 City[i][0]=New_City[i][0]; 409 New_City[i][0]=NULL; 410 City[i][1]=New_City[i][1]; 411 New_City[i][1]=NULL; 412 } 413 //Time:35 414 if(Time<Time_Sum+35) break; 415 for(int i=1;i<=City_Amount;i++){ 416 if(City[i][0]!=NULL && City[i][1]!=NULL){ 417 City[i][0]->Before_fight(City[i][1]); 418 City[i][1]->Before_fight(City[i][0]); 419 } 420 } 421 //Time:40 422 if(Time<Time_Sum+40) break; 423 for(int i=1;i<=City_Amount;i++){ 424 if(City[i][0]!=NULL && City[i][1]!=NULL){ 425 City[i][!(i&1)]->Fight_First(City[i][i&1]); 426 if(!City[i][0]->Die) City[i][0]->After_Fight(City[i][1]); 427 if(!City[i][1]->Die) City[i][1]->After_Fight(City[i][0]); 428 if(City[i][0]->Die) delete City[i][0],City[i][0]=NULL; 429 if(City[i][1]->Die) delete City[i][1],City[i][1]=NULL; 430 } 431 } 432 //Time:50 433 if(Time<Time_Sum+50) break; 434 r.Print_HP(); 435 b.Print_HP(); 436 //Time:55 437 if(Time<Time_Sum+55) break; 438 for(int i=0;i<=City_Amount+1;i++){ 439 if(City[i][0]!=NULL) City[i][0]->Print_Weapon(); 440 if(City[i][1]!=NULL) City[i][1]->Print_Weapon(); 441 } 442 Time_Index++; 443 Time_Sum+=60; 444 } 445 //Delete all the warriors alive 446 for(int j=0;j<=City_Amount+1;j++){ 447 if(City[j][0]!=NULL) delete City[j][0],City[j][0]=NULL; 448 if(City[j][1]!=NULL) delete City[j][1],City[j][1]=NULL; 449 if(New_City[j][0]!=NULL) delete New_City[j][0],New_City[j][0]=NULL; 450 if(New_City[j][1]!=NULL) delete New_City[j][1],New_City[j][1]=NULL; 451 } 452 } 453 return 0; 454 }
【魔獸世界四:代碼】

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 6 using namespace std; 7 8 class Warrior; 9 10 int Loyalty_Decrease,City_Amount; 11 int Cost[5],Power[5],Time_Index; 12 int ord1[5]={2,3,4,1,0},ord2[5]={3,0,1,2,4}; 13 Warrior* City[22][2];//0->red & 1->blue; 14 Warrior* New_City[22][2]; 15 char Weapon_Name[3][6]={"sword","bomb","arrow"}; 16 char Warrior_Name[5][7]={"dragon","ninja","iceman","lion","wolf"}; 17 char Headquarter_Name[2][5]={"red","blue"}; 18 int Red_Lost=-1,Blue_Lost=-1; 19 20 inline int MAX(int a,int b){return a>b?a:b;} 21 22 //the Sword's Can_Use can be predicted 23 int Count_Can_Use(int t){ 24 t/=5; 25 int tmp; 26 for(tmp=0;t;tmp++) t=t*4/5; 27 return tmp; 28 } 29 30 class Weapon{ 31 public: 32 int Can_use; 33 int Index; 34 char Name[6]; 35 Warrior* Owner; 36 Weapon(char* s,Warrior* O_,int I_,int C_){ 37 strcpy(Name,s); 38 Owner=O_; 39 Index=I_; 40 Can_use=C_; 41 } 42 int Use(){return Can_use;} 43 virtual bool Is_Sword(){return false;} 44 virtual bool Is_Bomb(){return false;} 45 virtual bool Is_Arrow(){return false;} 46 }; 47 48 bool cmp(const Weapon *A,const Weapon *B){ 49 if(A==NULL) return false; 50 if(B==NULL) return true; 51 if(A->Index!=B->Index) return A->Index<B->Index; 52 return A->Can_use>B->Can_use; 53 } 54 55 class Sword: public Weapon{ 56 public: 57 Sword(Warrior* O_):Weapon(Weapon_Name[0],O_,0){}; 58 virtual bool Is_Sword(){return true;} 59 }; 60 61 class Bomb: public Weapon{ 62 public: 63 Bomb(Warrior* O_):Weapon(Weapon_Name[1],O_,1){}; 64 virtual bool Is_Bomb(){return true;} 65 }; 66 67 class Arrow: public Weapon{ 68 public: 69 Arrow(Warrior* O_):Weapon(Weapon_Name[2],O_,2){}; 70 virtual bool Is_Arrow(){return true;} 71 }; 72 73 class Warrior{ 74 public: 75 int HP,MP; 76 int City_Index,Direction; 77 char Name[7]; 78 int Born_Number; 79 int Weapon_Count; 80 int Weapon_Index; 81 bool Die; 82 Weapon* w[10]; 83 Warrior(int HP_,int MP_,char *s,int B_,int C_,int D_){ 84 memset(Name,0,sizeof(Name)); 85 memset(w,0,sizeof(w)); 86 Weapon_Count=0; 87 HP=HP_; 88 MP=MP_; 89 strcpy(Name,s); 90 City_Index=C_; 91 Direction=D_; 92 Die=false; 93 Born_Number=B_; 94 } 95 virtual void get_weapon(){ 96 switch((Time_Index+1)%3){ 97 case 0: w[Weapon_Count++]=new Sword(this);break; 98 case 1: w[Weapon_Count++]=new Bomb(this);break; 99 case 2: w[Weapon_Count++]=new Arrow(this);break; 100 }; 101 } 102 virtual void After_Born(){} 103 void Hurt(int Damage){ 104 HP-=Damage; 105 if(HP<=0) Die=true; 106 } 107 virtual bool Escape(){return false;} 108 virtual bool Is_Wolf(){return false;} 109 virtual bool Is_Ninja(){return false;} 110 virtual void Before_fight(Warrior *Enermy){} 111 void Use_Weapon(Warrior *Enermy){ 112 if(!Weapon_Count) return ; 113 int temp=0; 114 while(w[Weapon_Index=(Weapon_Index+1)%10]==NULL && temp<10) temp++; 115 if(w[Weapon_Index]->Is_Sword()) 116 Enermy->Hurt(MP/5); 117 else if(w[Weapon_Index]->Is_Bomb()){ 118 w[Weapon_Index]->Can_use-=2; 119 Enermy->Hurt(MP*2/5); 120 if(!Is_Ninja()) 121 Hurt(MP*2/5/2); 122 } 123 else if(w[Weapon_Index]->Is_Arrow()){ 124 w[Weapon_Index]->Can_use--; 125 Enermy->Hurt(MP*3/10); 126 } 127 if(!w[Weapon_Index]->Use()){ 128 delete w[Weapon_Index],w[Weapon_Index]=NULL; 129 Weapon_Count--; 130 } 131 if(Enermy->Die && !Die){ 132 //in order to make the Weapon is in the w[Weapon_Count]'s place You have to reorder it before get others 133 sort(w,w+10,cmp); 134 if(Weapon_Count<10 && Enermy->Weapon_Count>0){ 135 sort(Enermy->w,Enermy->w+Enermy->Weapon_Count,cmp); 136 for(int i=0;i<Enermy->Weapon_Count && Weapon_Count<10;i++) 137 w[Weapon_Count++]=Enermy->w[i]; 138 } 139 } 140 if(!Enermy->Die && Die){ 141 sort(Enermy->w,Enermy->w+10,cmp); 142 if(Enermy->Weapon_Count<10 && Weapon_Count>0){ 143 sort(w,w+Weapon_Count,cmp); 144 for(int i=0;i<Weapon_Count && Enermy->Weapon_Count<10;i++) 145 Enermy->w[Weapon_Count++]=w[i]; 146 } 147 } 148 } 149 /*Time_Tick is a clock to test the max time that both are still*/ 150 void Fight_First(Warrior *A){ 151 if(Weapon_Count) sort(w,w+10,cmp); 152 if(A->Weapon_Count) sort(A->w,A->w+10,cmp); 153 Weapon_Index=-1; 154 A->Weapon_Index=-1; 155 int rec_HP=HP,rec_HP_A=A->HP,rec_W=Weapon_Count,rec_W_A=A->Weapon_Count; 156 int Time_Tick=2*MAX(rec_W,rec_W_A); 157 while(!Die && !A->Die && (Weapon_Count || A->Weapon_Count) ){ 158 Use_Weapon(A); 159 if(!A->Die) 160 A->Use_Weapon(this); 161 if(--Time_Tick==0){ 162 if(rec_HP==HP && rec_HP_A==A->HP && rec_W==Weapon_Count && rec_W_A==A->Weapon_Count) 163 break; 164 else{ 165 rec_HP=HP,rec_HP_A=A->HP,rec_W=Weapon_Count,rec_W_A=A->Weapon_Count; 166 Time_Tick=MAX(rec_W,rec_W_A); 167 } 168 } 169 } 170 Warrior *r; 171 Warrior *b; 172 if(Direction>0) r=this,b=A; 173 else r=A,b=this; 174 if(r->Die){ 175 if(b->Die) printf("%03d:40 both red %s %d and blue %s %d died in city %d\n",Time_Index,r->Name,r->Born_Number,b->Name,b->Born_Number,r->City_Index); 176 else printf("%03d:40 blue %s %d killed red %s %d in city %d remaining %d elements\n",Time_Index,b->Name,b->Born_Number,r->Name,r->Born_Number,r->City_Index,b->HP); 177 } 178 else{ 179 if(b->Die) printf("%03d:40 red %s %d killed blue %s %d in city %d remaining %d elements\n",Time_Index,r->Name,r->Born_Number,b->Name,b->Born_Number,r->City_Index,r->HP); 180 else printf("%03d:40 both red %s %d and blue %s %d were alive in city %d\n",Time_Index,r->Name,r->Born_Number,b->Name,b->Born_Number,r->City_Index); 181 } 182 } 183 virtual void Process(){ 184 City[City_Index][Direction<0]=NULL; 185 City_Index+=Direction; 186 New_City[City_Index][Direction<0]=this; 187 if(City_Index==0){ 188 printf("%03d:10 blue %s %d reached red headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 189 if(++Red_Lost>0) 190 printf("%03d:10 red headquarter was taken\n",Time_Index); 191 } 192 else if(City_Index==City_Amount+1){ 193 printf("%03d:10 red %s %d reached blue headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 194 if(++Blue_Lost>0) 195 printf("%03d:10 blue headquarter was taken\n",Time_Index); 196 } 197 else 198 printf("%03d:10 %s %s %d marched to city %d with %d elements and force %d\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,City_Index,HP,MP); 199 } 200 void Print_Weapon(){ 201 int cnt[3]; 202 for(int i=0;i<3;i++) cnt[i]=0; 203 for(int i=0;i<10;i++) 204 if(w[i]!=NULL) cnt[w[i]->Index]++; 205 printf("%03d:55 %s %s %d has %d sword %d bomb %d arrow and %d elements\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,cnt[0],cnt[1],cnt[2],HP); 206 } 207 virtual void After_Fight(Warrior* Enermy){} 208 }; 209 //0 210 class Dragon:public Warrior{ 211 public: 212 Dragon(int HP,int MP_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[0],B_,C_,D_){} 213 virtual void After_Fight(Warrior* Enermy){ 214 printf("%03d:40 %s dragon %d yelled in city %d\n",Time_Index,Headquarter_Name[Direction<0],Born_Number,City_Index); 215 } 216 }; 217 //1 218 class Ninja:public Warrior{ 219 public: 220 Ninja(int HP,int MP_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[1],B_,C_,D_){} 221 virtual void get_weapon(){ 222 switch((Time_Index+1)%3){ 223 case 0: w[Weapon_Count++]=new Sword(this);break; 224 case 1: w[Weapon_Count++]=new Bomb(this);break; 225 case 2: w[Weapon_Count++]=new Arrow(this);break; 226 }; 227 switch((Time_Index+2)%3){ 228 case 0: w[Weapon_Count++]=new Sword(this);break; 229 case 1: w[Weapon_Count++]=new Bomb(this);break; 230 case 2: w[Weapon_Count++]=new Arrow(this);break; 231 }; 232 } 233 virtual bool Is_Ninja(){return true;} 234 }; 235 //2 236 class Iceman:public Warrior{ 237 public: 238 Iceman(int HP,int MP_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[2],B_,C_,D_){} 239 virtual void Process(){ 240 City[City_Index][Direction<0]=NULL; 241 City_Index+=Direction; 242 New_City[City_Index][Direction<0]=this; 243 HP-=HP/10; 244 if(City_Index==0){ 245 Red_Lost=true; 246 printf("%03d:10 blue %s %d reached red headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 247 printf("%03d:10 red headquarter was taken\n",Time_Index); 248 } 249 else if(City_Index==City_Amount+1){ 250 Blue_Lost=true; 251 printf("%03d:10 red %s %d reached blue headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 252 printf("%03d:10 blue headquarter was taken\n",Time_Index); 253 } 254 else 255 printf("%03d:10 %s %s %d marched to city %d with %d elements and force %d\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,City_Index,HP,MP); 256 } 257 }; 258 //3 259 class Lion:public Warrior{ 260 int loyalty; 261 public: 262 Lion(int HP,int MP_,int l_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[3],B_,C_,D_){loyalty=l_;} 263 virtual void After_Born(){ 264 printf("Its loyalty is %d\n",loyalty); 265 } 266 virtual bool Escape(){return loyalty<=0;} 267 virtual void Process(){ 268 City[City_Index][Direction<0]=NULL; 269 City_Index+=Direction; 270 New_City[City_Index][Direction<0]=this; 271 loyalty-=Loyalty_Decrease; 272 if(City_Index==0){ 273 Red_Lost=true; 274 printf("%03d:10 blue %s %d reached red headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 275 printf("%03d:10 red headquarter was taken\n",Time_Index); 276 } 277 else if(City_Index==City_Amount+1){ 278 Blue_Lost=true; 279 printf("%03d:10 red %s %d reached blue headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 280 printf("%03d:10 blue headquarter was taken\n",Time_Index); 281 } 282 else 283 printf("%03d:10 %s %s %d marched to city %d with %d elements and force %d\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,City_Index,HP,MP); 284 } 285 }; 286 //4 287 class Wolf:public Warrior{ 288 public: 289 Wolf(int HP,int MP_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[4],B_,C_,D_){} 290 virtual void get_weapon(){} 291 virtual void Weapon_print(){} 292 virtual bool Is_Wolf(){return true;} 293 virtual void Before_fight(Warrior *Enermy){ 294 if(Enermy->Is_Wolf()) return; 295 if(Weapon_Count<10 && Enermy->Weapon_Count>0){ 296 sort(w,w+10,cmp); 297 sort(Enermy->w,Enermy->w+10,cmp); 298 int Min_Index=Enermy->w[0]->Index,Amount=0; 299 for(int i=0;Enermy->w[i]!=NULL && Enermy->w[i]->Index==Min_Index && Weapon_Count<10 && i<Enermy->Weapon_Count+Amount;i++){ 300 w[Weapon_Count++]=Enermy->w[i],Amount++; 301 Enermy->Weapon_Count--; 302 Enermy->w[i]=NULL; 303 } 304 printf("%03d:35 %s wolf %d took %d %s from %s %s %d in city %d\n",Time_Index,Headquarter_Name[Direction<0],Born_Number,Amount,Weapon_Name[Min_Index],Headquarter_Name[Direction>0],Enermy->Name,Enermy->Born_Number,City_Index); 305 } 306 } 307 }; 308 309 class City{ 310 int HP; 311 int flag; 312 int Last_Res;//the result of last war 313 Warrior *r,*b; 314 public: 315 Rebuild(){flag=Last_War=-1;HP=0;} 316 Change_flag(int f_):f(f_){} 317 HP_Taken(){HP=0;} 318 HP_Accumulate(){HP+=10;} 319 Rec_Res(int res){ 320 if(Last_Res==res) flag=res; 321 Last_Res=res; 322 } 323 }city[20]; 324 325 class Headquarter{ 326 private: 327 char Name[5]; 328 int HP; 329 int Order[5]; 330 int Count[5]; 331 int Warrior_Index; 332 Warrior *cur; 333 bool STOP; 334 public: 335 Headquarter(char *s,int HP_,int* O_){ 336 memset(Name,0,sizeof(Name)); 337 memset(Count,0,sizeof(Count)); 338 strcpy(Name,s); 339 HP=HP_; 340 for(int i=0;i<5;i++) 341 Order[i]=O_[i]; 342 Warrior_Index=-1; 343 cur=NULL; 344 STOP=0; 345 } 346 void Change_HP(int HP_){ 347 HP=HP_; 348 memset(Count,0,sizeof(Count)); 349 Warrior_Index=-1; 350 cur=NULL; 351 STOP=0; 352 }; 353 void Build_Warrior(){ 354 if(STOP) return; 355 int Born_Place,Direct; 356 if(Name[0]=='r') Born_Place=0,Direct=1; 357 else Born_Place=City_Amount+1,Direct=-1; 358 Warrior_Index=(Warrior_Index+1)%5; 359 int who=Order[Warrior_Index]; 360 if(HP>=Cost[who]){ 361 Count[who]++; 362 HP-=Cost[who]; 363 switch(who){ 364 case 0: cur=new Dragon(Cost[0],Power[0],Time_Index+1,Born_Place,Direct);break; 365 case 1: cur=new Ninja(Cost[1],Power[1],Time_Index+1,Born_Place,Direct);break; 366 case 2: cur=new Iceman(Cost[2],Power[2],Time_Index+1,Born_Place,Direct);break; 367 case 3: cur=new Lion(Cost[3],Power[3],HP,Time_Index+1,Born_Place,Direct);break; 368 case 4: cur=new Wolf(Cost[4],Power[4],Time_Index+1,Born_Place,Direct);break; 369 }; 370 cur->get_weapon(); 371 printf("%03d:00 %s %s %d born\n",Time_Index,Name,Warrior_Name[who],Time_Index+1); 372 cur->After_Born(); 373 if(Name[0]=='r') City[0][0]=cur; 374 else City[City_Amount+1][1]=cur; 375 cur=NULL; 376 } 377 else{ 378 //printf("%03d:00 %s headquarter stops making warriors\n",Time_Index,Name); 379 STOP=true; 380 } 381 } 382 void Print_HP(){ 383 printf("%03d:50 %d elements in %s headquarter\n",Time_Index,HP,Name); 384 } 385 bool Stop(){return STOP;} 386 }; 387 388 Headquarter r(Headquarter_Name[0],0,ord1),b(Headquarter_Name[1],0,ord2); 389 390 int main(){ 391 #ifndef ONLINE_JUDGE 392 freopen("x.in","r",stdin); 393 freopen("x.out","w",stdout); 394 #endif 395 int Kase,W,Time,Time_Sum; 396 scanf("%d",&Kase); 397 for(int T=1;T<=Kase;T++){ 398 printf("Case %d:\n",T); 399 scanf("%d%d%d%d",&W,&City_Amount,&Loyalty_Decrease,&Time); 400 for(int i=0;i<=City_Amount+1;i++) 401 city[i].Rebuild(); 402 Time_Index=Time_Sum=0; 403 r.Change_HP(W); b.Change_HP(W); 404 Red_Lost=Blue_Lost=-1; 405 for(int i=0;i<5;i++) 406 scanf("%d",&Cost[i]); 407 for(int i=0;i<5;i++) 408 scanf("%d",&Power[i]); 409 while(Time_Sum<=Time){ 410 //Time:0 411 r.Build_Warrior(); 412 b.Build_Warrior(); 413 //Time:5 414 if(Time<Time_Sum+5) break; 415 for(int i=0;i<=City_Amount+1;i++){ 416 if(city[i].r!=NULL && city[i].r->Escape()){ 417 printf("%03d:05 red lion %d ran away\n",Time_Index,city[i].r->Born_Number); 418 delete city[i].r,city[i].r=NULL; 419 } 420 if(city[i].b!=NULL && city[i].b->Escape()){ 421 printf("%03d:05 blue lion %d ran away\n",Time_Index,city[i].b->Born_Number); 422 delete city[i].b,city[i].b=NULL; 423 } 424 } 425 //Time:10 426 if(Time<Time_Sum+10) break; 427 for(int i=0;i<=City_Amount+1;i++){ 428 if(i>0) 429 if(city[i-1].r!=NULL) city[i-1].r->Process(); 430 if(i<=City_Amount) 431 if(city[i+1].b!=NULL) city[i+1].b->Process(); 432 } 433 if(Blue_Lost>0 || Red_Lost>0) break; 434 for(int i=0;i<=City_Amount+1;i++){ 435 city[i].r=New_City[i][0]; 436 New_City[i][0]=NULL; 437 city[i].b=New_City[i][1]; 438 New_City[i][1]=NULL; 439 } 440 //Time:35 441 if(Time<Time_Sum+35) break; 442 for(int i=1;i<=City_Amount;i++){ 443 if(city[i].r!=NULL && city[i].b!=NULL){ 444 city[i].r->Before_fight(city[i].b); 445 city[i].b->Before_fight(city[i].r); 446 } 447 } 448 //Time:40 449 if(Time<Time_Sum+40) break; 450 for(int i=1;i<=City_Amount;i++){ 451 if(city[i].r!=NULL && city[i].b!=NULL){ 452 if(city[i].flag==0) city[i].r->Fight_First(city[i].b); 453 else city[i].b->Fight_First(city[i].r); 454 if(!city[i].r->Die) city[i].r->After_Fight(city[i].b); 455 if(!city[i].b->Die) city[i].b->After_Fight(city[i].r); 456 if(city[i].r->Die) delete city[i].r,city[i].r=NULL; 457 if(city[i].b->Die) delete city[i].b,city[i].b=NULL; 458 } 459 } 460 //Time:50 461 if(Time<Time_Sum+50) break; 462 r.Print_HP(); 463 b.Print_HP(); 464 //Time:55 465 if(Time<Time_Sum+55) break; 466 for(int i=0;i<=City_Amount+1;i++){ 467 if(city[i].r!=NULL) city[i].r->Print_Weapon(); 468 if(city[i].b!=NULL) city[i].b->Print_Weapon(); 469 } 470 Time_Index++; 471 Time_Sum+=60; 472 } 473 //Delete all the warriors alive 474 for(int i=0;i<=City_Amount+1;i++){ 475 if(city[i].r!=NULL) delete city[i].r,city[i].r=NULL; 476 if(city[i].b!=NULL) delete city[i].b,city[i].b=NULL; 477 if(New_City[i][0]!=NULL) delete New_City[i][0],New_City[i][0]=NULL; 478 if(New_City[i][1]!=NULL) delete New_City[i][1],New_City[i][1]=NULL; 479 } 480 } 481 return 0; 482 }
【魔獸世界終極版:代碼】

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 6 using namespace std; 7 8 class Warrior; 9 10 int Loyalty_Decrease,City_Amount,Arrow_MP; 11 int Cost[5],Power[5],Time_Index; 12 int ord1[5]={2,3,4,1,0},ord2[5]={3,0,1,2,4}; 13 Warrior* New_City[22][2]; 14 char Weapon_Name[3][6]={"sword","bomb","arrow"}; 15 char Warrior_Name[5][7]={"dragon","ninja","iceman","lion","wolf"}; 16 char Headquarter_Name[2][5]={"red","blue"}; 17 bool Red_Lost,Blue_Lost; 18 const int INF=0x3fffffff; 19 20 inline int MAX(int a,int b){return a>b?a:b;} 21 22 class Weapon{ 23 public: 24 int Can_Use; 25 int MP; 26 char Name[6]; 27 Weapon(char* s,int MP_):MP(MP_){ 28 strcpy(Name,s); 29 Can_Use=3; 30 } 31 int Use(){return Can_Use;} 32 virtual void AD(Warrior *Enermy); 33 }; 34 35 class Sword: public Weapon{ 36 public: 37 Sword(int MP_):Weapon(Weapon_Name[0],MP_){}; 38 virtual void AD(Warrior *Enermy); 39 }; 40 41 class Bomb: public Weapon{ 42 public: 43 Bomb(int MP_):Weapon(Weapon_Name[1],MP_){}; 44 virtual void AD(Warrior *Enermy); 45 }; 46 47 class Arrow: public Weapon{ 48 public: 49 Arrow(int MP_):Weapon(Weapon_Name[2],MP_){}; 50 virtual void AD(Warrior *Enermy); 51 }; 52 53 class City{ 54 public: 55 int HP; 56 int flag; 57 int Last_Res;//the result of last war 58 bool Change_Flag; 59 Warrior *r,*b; 60 void Rebuild(){flag=Last_Res=-1;HP=Change_Flag=0;} 61 void Rec_Res(int res){ 62 if(Last_Res==res && flag!=res) Change_Flag=true,flag=res; 63 else Change_Flag=false; 64 Last_Res=res; 65 } 66 }city[20]; 67 68 class Warrior{ 69 public: 70 int HP,MP; 71 int City_Index,Direction; 72 char Name[7]; 73 int Born_Number; 74 bool Die; 75 Weapon* w[3]; 76 Warrior(int HP_,int MP_,char *s,int B_,int C_,int D_){ 77 memset(Name,0,sizeof(Name)); 78 memset(w,0,sizeof(w)); 79 HP=HP_; 80 MP=MP_; 81 strcpy(Name,s); 82 City_Index=C_; 83 Direction=D_; 84 Die=false; 85 Born_Number=B_; 86 } 87 Warrior(const Warrior &A){ 88 HP=A.HP,MP=A.MP; 89 Die=A.Die; 90 if(A.w[0]){ 91 w[0]=new Sword(0); 92 w[0]->Can_Use=A.w[0]->Can_Use; 93 w[0]->MP=A.w[0]->MP; 94 } 95 } 96 virtual void get_weapon(){ 97 switch((Time_Index+1)%3){ 98 case 0: w[0]=new Sword(MP/5);break; 99 case 1: w[1]=new Bomb(0);break; 100 case 2: w[2]=new Arrow(Arrow_MP);break; 101 }; 102 } 103 virtual void After_Born(){} 104 void Hurt(int Damage){ 105 HP-=Damage; 106 if(HP<=0) Die=true,HP=0; 107 } 108 virtual bool Escape(){return false;} 109 virtual bool Is_Wolf(){return false;} 110 virtual bool Is_Ninja(){return false;} 111 /*Time_Tick is a clock to test the max time that both are still*/ 112 void Attack(Warrior *Enermy){ 113 if(w[0]) w[0]->AD(Enermy); 114 Enermy->Hurt(MP); 115 } 116 void Attack_Back(Warrior *Enermy){ 117 if(w[0]) w[0]->AD(Enermy); 118 Enermy->Hurt(MP/2); 119 } 120 int Will_I_Die(Warrior *Enermy){ 121 Warrior Arti_Man1(*this),Arti_Man2(*Enermy); 122 Arti_Man1.Attack(&Arti_Man2); 123 if(Arti_Man2.Die) return -1; 124 else 125 Arti_Man2.Attack_Back(&Arti_Man1); 126 return Arti_Man1.Die; 127 } 128 virtual void Before_Fight(Warrior *Enermy){ 129 if(w[1] || Enermy->w[1]){ 130 int res=Will_I_Die(Enermy); 131 if(res>0 && w[1]){ 132 w[1]->AD(Enermy),Hurt(INF); 133 printf("%03d:38 red %s %d used a bomb and killed blue %s %d\n",Time_Index,Name,Born_Number,Enermy->Name,Enermy->Born_Number); 134 } 135 else if(res<0 && Enermy->w[1]){ 136 Enermy->w[1]->AD(this),Enermy->Hurt(INF); 137 printf("%03d:38 blue %s %d used a bomb and killed red %s %d\n",Time_Index,Enermy->Name,Enermy->Born_Number,Name,Born_Number); 138 } 139 } 140 } 141 void Fight_First(Warrior *Enermy){ 142 Attack(Enermy); 143 if(w[0] && !w[0]->Use()) delete w[0],w[0]=NULL; 144 printf("%03d:40 %s %s %d attacked %s %s %d in city %d with %d elements and force %d\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,Headquarter_Name[Direction>0],Enermy->Name,Enermy->Born_Number,City_Index,HP,MP); 145 if(!Enermy->Die && !Enermy->Is_Ninja()){ 146 Enermy->Attack_Back(this); 147 if(Enermy->w[0] && !Enermy->w[0]->Use()) delete w[0],w[0]=NULL; 148 printf("%03d:40 %s %s %d fought back against %s %s %d in city %d with %d elements and force %d\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,Headquarter_Name[Direction>0],Enermy->Name,Enermy->Born_Number,City_Index,HP,MP); 149 } 150 } 151 void Print_Res(Warrior *Enermy){ 152 Warrior *r; 153 Warrior *b; 154 if(Direction>0) r=this,b=Enermy; 155 else r=Enermy,b=this; 156 if(r->Die){ 157 if(b->Die){ 158 printf("%03d:40 both red %s %d and blue %s %d died in city %d\n",Time_Index,r->Name,r->Born_Number,b->Name,b->Born_Number,r->City_Index); 159 city[r->City_Index].Rec_Res(-1); 160 } 161 else{ 162 printf("%03d:40 blue %s %d killed red %s %d in city %d remaining %d elements\n",Time_Index,b->Name,b->Born_Number,r->Name,r->Born_Number,r->City_Index,b->HP); 163 city[r->City_Index].Rec_Res(1); 164 } 165 } 166 else{ 167 if(b->Die){ 168 printf("%03d:40 red %s %d killed blue %s %d in city %d remaining %d elements\n",Time_Index,r->Name,r->Born_Number,b->Name,b->Born_Number,r->City_Index,r->HP); 169 city[r->City_Index].Rec_Res(0); 170 } 171 else{ 172 printf("%03d:40 both red %s %d and blue %s %d were alive in city %d\n",Time_Index,r->Name,r->Born_Number,b->Name,b->Born_Number,r->City_Index); 173 city[r->City_Index].Rec_Res(-1); 174 } 175 } 176 } 177 virtual void Process(){ 178 if(Direction<0) city[City_Index].r=NULL; 179 else city[City_Index].b=NULL; 180 City_Index+=Direction; 181 New_City[City_Index][Direction<0]=this; 182 if(City_Index==0){ 183 Red_Lost=true; 184 printf("%03d:10 blue %s %d reached red headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 185 printf("%03d:10 red headquarter was taken\n",Time_Index); 186 } 187 else if(City_Index==City_Amount+1){ 188 Blue_Lost=true; 189 printf("%03d:10 red %s %d reached blue headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 190 printf("%03d:10 blue headquarter was taken\n",Time_Index); 191 } 192 else 193 printf("%03d:10 %s %s %d marched to city %d with %d elements and force %d\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,City_Index,HP,MP); 194 } 195 void Print_Weapon(){ 196 printf("%03d:55 %s wolf 2 has ",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number); 197 if(w[2]!=NULL) printf("arrow(%d)",w[2]->Use()); 198 if(w[1]!=NULL){ 199 if(w[2]!=NULL) printf(","); 200 printf("bomb"); 201 } 202 if(w[0]!=NULL){ 203 if(w[1]!=NULL || w[2]!=NULL) printf(","); 204 printf("sword(%d)",w[0]->MP); 205 } 206 if(w[0]==NULL && w[1]==NULL && w[2]==NULL) 207 printf("no weapon"); 208 putchar('\n'); 209 } 210 virtual void After_Fight(Warrior* Enermy){} 211 }; 212 //0 213 class Dragon:public Warrior{ 214 double morale; 215 public: 216 Dragon(int HP,int MP_,int B_,int C_,int D_,int M_):Warrior(HP,MP_,Warrior_Name[0],B_,C_,D_),morale(M_){} 217 virtual void After_Born(){ 218 printf("Its morale is %.2lf\n",morale); 219 } 220 virtual void After_Fight(Warrior* Enermy){ 221 if(!Enermy->Die) morale-=0.2; 222 else morale+=0.2; 223 if(morale>0.8) 224 printf("%03d:40 %s dragon %d yelled in city %d\n",Time_Index,Headquarter_Name[Direction<0],Born_Number,City_Index); 225 } 226 }; 227 //1 228 class Ninja:public Warrior{ 229 public: 230 Ninja(int HP,int MP_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[1],B_,C_,D_){} 231 virtual void get_weapon(){ 232 switch((Time_Index+1)%3){ 233 case 0: w[0]=new Sword(MP/5);break; 234 case 1: w[1]=new Bomb(INF);break; 235 case 2: w[2]=new Arrow(Arrow_MP);break; 236 }; 237 switch((Time_Index+2)%3){ 238 case 0: w[0]=new Sword(MP/5);break; 239 case 1: w[1]=new Bomb(INF);break; 240 case 2: w[2]=new Arrow(Arrow_MP);break; 241 }; 242 } 243 virtual bool Is_Ninja(){return true;} 244 }; 245 //2 246 class Iceman:public Warrior{ 247 int Moved; 248 public: 249 Iceman(int HP,int MP_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[2],B_,C_,D_){Moved=0;} 250 virtual void Process(){ 251 Moved++; 252 if(Moved==2) HP=HP>9?HP-9:1,MP+=20,Moved=0; 253 if(Direction<0) city[City_Index].b=NULL; 254 else city[City_Index].r=NULL; 255 City_Index+=Direction; 256 New_City[City_Index][Direction<0]=this; 257 if(City_Index==0){ 258 Red_Lost++; 259 printf("%03d:10 blue %s %d reached red headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 260 if(Red_Lost>0) printf("%03d:10 red headquarter was taken\n",Time_Index); 261 } 262 else if(City_Index==City_Amount+1){ 263 Blue_Lost++; 264 printf("%03d:10 red %s %d reached blue headquarter with %d elements and force %d\n",Time_Index,Name,Born_Number,HP,MP); 265 if(Blue_Lost>0) printf("%03d:10 blue headquarter was taken\n",Time_Index); 266 } 267 else 268 printf("%03d:10 %s %s %d marched to city %d with %d elements and force %d\n",Time_Index,Headquarter_Name[Direction<0],Name,Born_Number,City_Index,HP,MP); 269 } 270 }; 271 //3 272 class Lion:public Warrior{ 273 int loyalty; 274 public: 275 Lion(int HP,int MP_,int l_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[3],B_,C_,D_){loyalty=l_;} 276 virtual void After_Born(){ 277 printf("Its loyalty is %d\n",loyalty); 278 } 279 virtual void get_weapon(){} 280 virtual bool Escape(){return loyalty<=0;} 281 virtual void After_Fight(Warrior* Enermy){ 282 if(!Enermy->Die) loyalty-=Loyalty_Decrease; 283 } 284 }; 285 //4 286 class Wolf:public Warrior{ 287 public: 288 Wolf(int HP,int MP_,int B_,int C_,int D_):Warrior(HP,MP_,Warrior_Name[4],B_,C_,D_){} 289 virtual void get_weapon(){} 290 virtual void Weapon_print(){} 291 virtual bool Is_Wolf(){return true;} 292 virtual void After_Fight(Warrior *Enermy){ 293 if(Enermy->Die){ 294 for(int i=0;i<3;i++) 295 if(w[i]==NULL && Enermy->w[i]!=NULL) 296 w[i]=Enermy->w[i],Enermy->w[i]=NULL; 297 } 298 } 299 }; 300 301 void Weapon::AD(Warrior *Enermy){} 302 void Sword::AD(Warrior *Enermy){ 303 Enermy->Hurt(MP); 304 MP=MP*4/5; 305 if(!MP) Can_Use=0; 306 } 307 void Bomb::AD(Warrior *Enermy){ 308 Enermy->Hurt(INF); 309 Can_Use-=3; 310 } 311 void Arrow::AD(Warrior *Enermy){ 312 Can_Use--; 313 Enermy->Hurt(MP); 314 } 315 316 class Headquarter{ 317 private: 318 char Name[5]; 319 int Order[5]; 320 int Count[5]; 321 int Warrior_Index; 322 Warrior *cur; 323 bool STOP; 324 public: 325 int HP; 326 Headquarter(char *s,int HP_,int* O_){ 327 memset(Name,0,sizeof(Name)); 328 memset(Count,0,sizeof(Count)); 329 strcpy(Name,s); 330 HP=HP_; 331 for(int i=0;i<5;i++) 332 Order[i]=O_[i]; 333 Warrior_Index=-1; 334 cur=NULL; 335 STOP=0; 336 } 337 void Change_HP(int HP_){ 338 HP=HP_; 339 memset(Count,0,sizeof(Count)); 340 Warrior_Index=-1; 341 cur=NULL; 342 STOP=0; 343 }; 344 void Build_Warrior(){ 345 if(STOP) return; 346 int Born_Place,Direct; 347 if(Name[0]=='r') Born_Place=0,Direct=1; 348 else Born_Place=City_Amount+1,Direct=-1; 349 Warrior_Index=(Warrior_Index+1)%5; 350 int who=Order[Warrior_Index]; 351 if(HP>=Cost[who]){ 352 Count[who]++; 353 HP-=Cost[who]; 354 switch(who){ 355 case 0: cur=new Dragon(Cost[0],Power[0],Time_Index+1,Born_Place,Direct,(double)HP/Cost[0]);break; 356 case 1: cur=new Ninja(Cost[1],Power[1],Time_Index+1,Born_Place,Direct);break; 357 case 2: cur=new Iceman(Cost[2],Power[2],Time_Index+1,Born_Place,Direct);break; 358 case 3: cur=new Lion(Cost[3],Power[3],HP,Time_Index+1,Born_Place,Direct);break; 359 case 4: cur=new Wolf(Cost[4],Power[4],Time_Index+1,Born_Place,Direct);break; 360 }; 361 cur->get_weapon(); 362 printf("%03d:00 %s %s %d born\n",Time_Index,Name,Warrior_Name[who],Time_Index+1); 363 cur->After_Born(); 364 if(Name[0]=='r') city[0].r=cur; 365 else city[City_Amount+1].b=cur; 366 cur=NULL; 367 } 368 else{ 369 //printf("%03d:00 %s headquarter stops making warriors\n",Time_Index,Name); 370 STOP=true; 371 } 372 } 373 void Print_HP(){ 374 printf("%03d:50 %d elements in %s headquarter\n",Time_Index,HP,Name); 375 } 376 bool Stop(){return STOP;} 377 }; 378 379 Headquarter r(Headquarter_Name[0],0,ord1),b(Headquarter_Name[1],0,ord2); 380 381 int main(){ 382 #ifndef ONLINE_JUDGE 383 freopen("x.in","r",stdin); 384 freopen("x.out","w",stdout); 385 #endif 386 int Kase,W,Time,Time_Sum; 387 scanf("%d",&Kase); 388 for(int T=1;T<=Kase;T++){ 389 printf("Case %d:\n",T); 390 scanf("%d%d%d%d%d",&W,&City_Amount,&Arrow_MP,&Loyalty_Decrease,&Time); 391 for(int j=0;j<=City_Amount+1;j++) 392 city[j].r=city[j].b=NULL,city[j].flag=!(j&1); 393 Time_Index=Time_Sum=0; 394 r.Change_HP(W); b.Change_HP(W); 395 Red_Lost=Blue_Lost=false; 396 for(int i=0;i<5;i++) 397 scanf("%d",&Cost[i]); 398 for(int i=0;i<5;i++) 399 scanf("%d",&Power[i]); 400 while(Time_Sum<=Time){ 401 //Time:0 402 r.Build_Warrior(); 403 b.Build_Warrior(); 404 //Time:5 405 if(Time<Time_Sum+5) break; 406 for(int i=0;i<=City_Amount+1;i++){ 407 if(i!=City_Amount+1 && city[i].r!=NULL && city[i].r->Escape()){ 408 printf("%03d:05 red lion %d ran away\n",Time_Index,city[i].r->Born_Number); 409 delete city[i].r,city[i].r=NULL; 410 } 411 if(i!=0 && city[i].b!=NULL && city[i].b->Escape()){ 412 printf("%03d:05 blue lion %d ran away\n",Time_Index,city[i].b->Born_Number); 413 delete city[i].b,city[i].b=NULL; 414 } 415 } 416 //Time:10 417 if(Time<Time_Sum+10) break; 418 for(int i=0;i<=City_Amount+1;i++){ 419 if(i>0) 420 if(city[i-1].r!=NULL) city[i-1].r->Process(); 421 if(i<=City_Amount) 422 if(city[i+1].b!=NULL) city[i+1].b->Process(); 423 } 424 if(Blue_Lost || Red_Lost) break; 425 for(int i=0;i<=City_Amount+1;i++){ 426 city[i].r=New_City[i][0]; 427 New_City[i][0]=NULL; 428 city[i].b=New_City[i][1]; 429 New_City[i][1]=NULL; 430 } 431 //Time:20 432 if(Time<Time_Sum+20) break; 433 for(int i=1;i<=City_Amount;i++) city[i].HP+=10; 434 //Time:30 435 if(Time<Time_Sum+30) break; 436 for(int i=1;i<=City_Amount;i++){ 437 if(city[i].r!=NULL && city[i].b==NULL) r.HP+=city[i].HP,city[i].HP=0; 438 if(city[i].b!=NULL && city[i].r==NULL) b.HP+=city[i].HP,city[i].HP=0; 439 } 440 //Time:35 441 if(Time<Time_Sum+35) break; 442 for(int i=0;i<=City_Amount+1;i++){ 443 if(i!=City_Amount+1 && city[i+1].b!=NULL && city[i].r!=NULL && city[i].r->w[2]!=NULL){ 444 city[i].r->w[2]->AD(city[i+1].b); 445 if(city[i+1].b->Die) 446 printf("%03d:35 red %s %d shot and killed blue %s %d\n",Time_Index,city[i].r->Name,city[i].r->Born_Number,city[i+1].b->Name,city[i+1].b->Born_Number); 447 else 448 printf("%03d:35 red %s %d shot\n",Time_Index,city[i].r->Name,city[i].r->Born_Number); 449 if(!city[i].r->w[2]->Use()) delete city[i].r->w[2],city[i].r->w[2]=NULL; 450 } 451 if(i!=0 && city[i-1].r!=NULL && city[i].b!=NULL && city[i].b->w[2]!=NULL){ 452 city[i].b->w[2]->AD(city[i-1].r); 453 if(city[i-1].r->Die) 454 printf("%03d:35 blue %s %d shot and killed red %s %d\n",Time_Index,city[i].b->Name,city[i].b->Born_Number,city[i-1].r->Name,city[i-1].r->Born_Number); 455 else 456 printf("%03d:35 blue %s %d shot\n",Time_Index,city[i].b->Name,city[i].b->Born_Number); 457 if(!city[i].b->w[2]->Use()) delete city[i].b->w[2],city[i].b->w[2]=NULL; 458 } 459 } 460 //Time 38 461 if(Time<Time_Sum+38) break; 462 for(int i=1;i<=City_Amount;i++) 463 if(city[i].r!=NULL && city[i].b!=NULL && !city[i].r->Die && !city[i].b->Die){ 464 if(city[i].flag) city[i].b->Before_Fight(city[i].r); 465 else city[i].r->Before_Fight(city[i].b); 466 } 467 //Time:40 468 if(Time<Time_Sum+40) break; 469 for(int i=1;i<=City_Amount;i++){ 470 if(city[i].r!=NULL && city[i].b!=NULL){ 471 bool Bef_r=city[i].r->Die,Bef_b=city[i].b; 472 if(!Bef_r && !Bef_b){ 473 if(city[i].flag) city[i].b->Fight_First(city[i].r); 474 else city[i].r->Fight_First(city[i].b); 475 } 476 if(!Bef_r && city[i].r->Die){ 477 printf("%03d:40 red %s %d was killed in city %d\n",Time_Index,city[i].r->Name,city[i].r->Born_Number,i); 478 } 479 if(!Bef_b && city[i].b->Die){ 480 printf("%03d:40 blue %s %d was killed in city %d\n",Time_Index,city[i].b->Name,city[i].b->Born_Number,i); 481 } 482 483 if(!city[i].r->Die) city[i].r->After_Fight(city[i].b); 484 if(!city[i].b->Die) city[i].b->After_Fight(city[i].r); 485 if(!city[i].r->Die && city[i].b->Die) 486 printf("#03d:40 red %s %d earned %d elements for his headquarter\n",Time_Index,city[i].r->Name,city[i].r->Born_Number,city[i].HP); 487 if(!city[i].b->Die && city[i].r->Die) 488 printf("#03d:40 blue %s %d earned %d elements for his headquarter\n",Time_Index,city[i].b->Name,city[i].b->Born_Number,city[i].HP); 489 if(city[i].Change_Flag) 490 printf("%03d:40 %s flag raised in city %d\n",Headquarter_Name[city[i].flag],i); 491 } 492 } 493 //Aword 494 for(int i=City_Amount;i>=1 && r.HP>=8;i--) 495 if(city[i].r!=NULL && city[i].b!=NULL && !city[i].r->Die && city[i].b->Die) r.HP-=8,city[i].r->HP+=8; 496 for(int i=1;i<=City_Amount && b.HP>=8;i++) 497 if(city[i].r!=NULL && city[i].b!=NULL && !city[i].b->Die && city[i].r->Die) b.HP-=8,city[i].b->HP+=8; 498 for(int i=1;i<=City_Amount;i++) 499 if(city[i].r!=NULL && city[i].b!=NULL){ 500 if(!city[i].r->Die && city[i].b->Die) r.HP+=city[i].HP,city[i].HP=0; 501 else if(!city[i].b && city[i].r->Die) b.HP+=city[i].HP,city[i].HP=0; 502 if(city[i].r->Die) delete city[i].r,city[i].r=NULL; 503 if(city[i].b->Die) delete city[i].b,city[i].b=NULL; 504 } 505 //Time:50 506 if(Time<Time_Sum+50) break; 507 r.Print_HP(); 508 b.Print_HP(); 509 //Time:55 510 if(Time<Time_Sum+55) break; 511 for(int i=0;i<=City_Amount+1;i++){ 512 if(city[i].r!=NULL) city[i].r->Print_Weapon(); 513 if(city[i].b!=NULL) city[i].b->Print_Weapon(); 514 } 515 Time_Index++; 516 Time_Sum+=60; 517 } 518 //Delete all the warriors alive 519 for(int j=0;j<=City_Amount+1;j++){ 520 if(city[j].r!=NULL) delete city[j].r,city[j].r=NULL; 521 if(city[j].b!=NULL) delete city[j].b,city[j].b=NULL; 522 if(New_City[j][0]!=NULL) delete New_City[j][0],New_City[j][0]=NULL; 523 if(New_City[j][1]!=NULL) delete New_City[j][1],New_City[j][1]=NULL; 524 } 525 } 526 return 0; 527 }