程设大作业之魔兽世界

【前前言】下面的应该是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 }
View Code

 

【魔兽世界二:装备】

  魔兽二是魔兽一的一个加强版,这个里面呢,我们还是要制造和输出武士的信息,但是这个题目中最重要的就是出现了装备!

  哇,简直是一个新世界啦...因为这样的话,我们就有新的对象需要出现了!

  在之前的魔兽世界一,武士这个类其实是没有什么用的,他们只要提供名字和编号就可以了。但是在魔兽世界二里如果我们接着这样做的话,就会有些麻烦咯,因为需要给武士们发武器了,那么这样就一下诞生了两个对象:武器和武士...

  [当然啦这个题里其实武器也是没有任何作用的,更容易的打法可以删掉武器这个类哦...不过笔者感觉要开始为魔兽三做点准备了就写了这个类]

  我们注意到在魔兽一里武士们是没有任何区别的,只有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 }
View Code

 

【魔兽世界三:开战】

  这个版本里面的魔兽世界呢...

  很重要的两个任务就是让你的武士们可以移动起来,然后两个武士之间会发生互动(打架、抢武器什么的)

  下面就详细讲一下啦...

  先讲一讲关于武士的移动!

【武士的移动】

  这里的移动涉及到的主要是武士和城市这两个单位。

  而这里不同于之前两道魔兽的地方是:我不仅要输出每一个移动,并且在我程序构建出来的这个世界中,武士是真的在移动的。

  那么我们还是先考虑这个部分的输出:

  因为城市是分普通城市和指挥部的,所以对应的输出也是有两种

  先看看普通城市的输出:(这些都是从提供数据中的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 }
View Code

 

【战中】

  战斗的过程是两个人互相使用武器的过程。

  首先,还是需要先给两个人的武器排好序来确定使用的顺序,然后就类似魔兽世界一二中生产武士的方法来循环这个武器的顺序:

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]=NULLWeapon_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 }
View Code

 

【魔兽世界四:代码】

  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 }
View Code

【魔兽世界终极版:代码】

  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 }
View Code

 

posted @ 2018-04-02 17:52  诚叙  阅读(3124)  评论(13编辑  收藏  举报