Luogu P2482 [SDOI2010]猪国杀

模拟狂魔

爆写12KB,写代码3h左右,调试2h左右。。。

其实还是有些不足的,有些需要使用方法的地方直接修改了,而且变量还是放在private里好一些。

代码开头写了点心路历程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// ver 1.0: 第一次提交,WA 0.
// ver 2.0: 第二次提交,修改了牌堆堆顶没有从 1 开始的 bug,WA 10.
// ver 3.0: 第三次提交,修改了决斗扣血写反了的bug,WA 30.
// ver 4.0: 第四次提交,修改了无懈可击会在首次循环就退出,导致无法使用无懈可击的 Bug,WA 55.
// ver 5.0: 第五次提交,修改了在主公获胜后仍然获取 bonus 牌的 bug, WA 85.
// ver 6.0: 第六次提交,修改了修改第 3 个 bug 时产生的 bug. 调用扣血方法时用错了对象,AC.
#include <bits/stdc++.h>
#define IT std::vector<Card>::iterator

struct Card {
char kind;
bool free;
};

class Input {
public:
void main_input();
void n_m_input();
void status_input();
void heap_input();
};

class Pig {
public:
Pig();
// 方法
void start_round(); // 开始新回合
void draw_card(); // 抽牌
void play_card(); // 出牌

void use_card(IT it); // 使用卡牌
bool find_card(char ch); // 找牌
void increase_HP(); // 续命
void decrease_HP(); // 被续
void hurt(Pig &that); // 造成伤害
void pith(Pig &that); // 杀死
void pithed(); // 被杀死
void jump(); // 跳
bool to_be_dead(); // 濒死
void become_antilike(); // 进入类反
void exit_antilike(); // 退出类反

bool accessible(char ch); // 卡牌可用性

int find_kill(); // 寻找杀的人
void kill(Pig &that); // 杀
bool kill_respond(); // 回应杀

int find_duel(); // 寻找决斗对象
void duel(Pig &that); // 决斗

void south_attack(); // 南蛮入侵
bool south_attack_respond(); // 回应南蛮入侵

void arrows_shot(); // 万箭齐发
bool arrows_shot_respond(); // 回应万箭齐发

bool invulnerability(Pig &that); // 无懈可击
bool invulnerability_respond(); // 回应无懈可击

void equip_crossbow(); // 装备诸葛连弩
void unequip_crossbow(); // 卸下诸葛连弩

// 变量
std::vector<Card> hand; // 手牌
int array_index; // 数组下标
char status; // 身份
int HP; // 生命
bool dead; // 挂了
bool crossbow; // 装备了诸葛连弩
bool kill_used; // 出过杀
bool antilike; // 类反
bool jumped; // 跳了
};

int n, m; // 工 具 人
int king; // 主公的下标
int anti; // 反猪数
int heap_top = 1; // 牌堆顶
char winner; // 胜利者
char heap[5005]; // 牌堆
bool gameover; // 游戏结束
Input in; // 读入对象
Pig pig[15]; // 猪群

const bool debug = false; // 调试入口

void print(); // 输出函数

signed main() {
in.main_input();
if (!anti) {
winner = 'M';
print();
return 0;
}
pig[king].jump();
int pig_now = 0;
while (1) { // 主进程
pig_now = pig_now % n + 1;
if (pig[pig_now].dead) continue;
pig[pig_now].start_round();
if (gameover) {
print();
return 0;
}
}
}

void print() {
printf("%s\n", winner == 'M' ? "MP" : "FP");
for (int i = 1; i <= n; ++i) {
if (pig[i].dead)
puts("DEAD");
else {
for (IT it = pig[i].hand.begin(); it != pig[i].hand.end(); ++it) {
if (it->free) printf("%c ", it->kind);
}
puts("");
}
}
}

void Input::n_m_input() { scanf("%d%d", &n, &m); }

void Input::status_input() {
char sta[7], tmp;
for (int i = 1; i <= n; ++i) {
scanf("%s", sta);
if (sta[0] == 'M')
king = i;
else if (sta[0] == 'F')
++anti;
pig[i].array_index = i;
pig[i].status = sta[0];
do {
tmp = getchar();
if (isalpha(tmp)) pig[i].hand.push_back((Card){tmp, true});
} while (tmp != '\n');
}
}

void Input::heap_input() {
char tmp = getchar();
for (int i = 1; i <= m; ++i) {
while (!isalpha(tmp)) tmp = getchar();
heap[i] = tmp, tmp = getchar();
}
}

void Input::main_input() {
n_m_input();
status_input();
heap_input();
for (int i = m + 1; i <= 5000; ++i) heap[i] = heap[m];
}

Pig::Pig() {
HP = 4;
dead = false;
crossbow = false;
kill_used = false;
antilike = false;
jumped = false;
}

void Pig::draw_card() { hand.push_back((Card){heap[heap_top++], true}); }

void Pig::increase_HP() { ++HP; }
void Pig::decrease_HP() { --HP; }

void Pig::equip_crossbow() { crossbow = true; }
void Pig::unequip_crossbow() { crossbow = false; }

void Pig::pithed() { dead = true; }

void Pig::jump() { jumped = true; }

void Pig::become_antilike() { antilike = true; }
void Pig::exit_antilike() { antilike = false; }

bool Pig::kill_respond() { return find_card('D'); }

bool Pig::arrows_shot_respond() { return find_card('D'); }

bool Pig::south_attack_respond() { return find_card('K'); }

void Pig::start_round() {
draw_card(), draw_card(); // 抽两张牌
if (debug) {
printf("now its %d's round.\n", this->array_index);
for (int i = 1; i <= n; ++i) {
if (pig[i].dead) continue;
printf("%d %c %d:", pig[i].array_index, pig[i].status, pig[i].HP);
for (auto x : pig[i].hand)
if (x.free) printf("%c ", x.kind);
puts("");
}
}
play_card(); // 出牌
if (kill_used) kill_used = false;
}

void Pig::play_card() {
bool flag = true;
while (flag) {
flag = false;
for (IT it = hand.begin(); it != hand.end(); ++it) {
if (it->free && accessible(it->kind)) { // 可以使用
use_card(it);
if (dead || gameover) return;
flag = true;
it = hand.begin() - 1;
}
}
}
}

void Pig::use_card(IT it) {
char kind = it->kind;
int tmp;
switch (kind) {
case 'P': // 桃
if (debug) {
printf("%d eats a peach!\n", array_index);
}
it->free = false;
increase_HP();
break;
case 'K': // 杀
tmp = find_kill();
if (!tmp) return;
if (debug) {
printf("%d uses Kill. -> %d\n", array_index, pig[tmp].array_index);
}
it->free = false;
kill(pig[tmp]);
break;
case 'F': // 决斗
tmp = find_duel();
if (!tmp) return;
if (debug) {
printf("%d uses Duel. -> %d\n", array_index, pig[tmp].array_index);
}
it->free = false;
duel(pig[tmp]);
break;
case 'N': // 南蛮入侵
it->free = false;
if (debug) {
printf("%d uses South Attack.\n", array_index);
}
south_attack();
break;
case 'W': // 万箭齐发
it->free = false;
if (debug) {
printf("%d uses Arrows.\n", array_index);
}
arrows_shot();
break;
case 'Z': // 诸葛连弩
it->free = false;
if (debug) {
printf("%d uses Crossbow.\n", array_index);
}
equip_crossbow();
break;
}
}

bool Pig::find_card(char ch) {
for (IT it = hand.begin(); it != hand.end(); ++it) {
if (it->kind == ch && it->free) {
if (debug) {
printf("%d uses %c\n", array_index, it->kind);
}
it->free = false;
return true;
}
}
return false;
}

void Pig::hurt(Pig &that) {
that.decrease_HP();
if (debug) {
printf("%d hurts %d. left %d %d HP.\n", array_index, that.array_index,
that.array_index, that.HP);
}
if (!that.HP && that.to_be_dead()) {
if (debug) {
printf("%d killed %d.\n", array_index, that.array_index);
}
this->pith(that);
}
}

void Pig::pith(Pig &that) {
that.pithed();
switch (that.status) {
case 'M':
winner = 'F';
gameover = true;
break;

case 'F':
--anti;
if (!anti) {
winner = 'M';
gameover = true;
return;
}
draw_card(), draw_card(), draw_card();
break;

case 'Z':
if (this->status == 'M') {
for (IT it = this->hand.begin(); it != this->hand.end(); ++it)
it->free = false;
this->unequip_crossbow();
}
break;
}
}

bool Pig::to_be_dead() {
for (IT it = hand.begin(); it != hand.end(); ++it) {
if (it->kind == 'P' && it->free) {
it->free = false;
increase_HP();
return false;
}
}
return true;
}

bool Pig::accessible(char ch) {
switch (ch) {
case 'P':
if (HP < 4) return true;
break;

case 'K':
if (find_kill()) return true;
break;

case 'F':
if (find_duel()) return true;
break;

case 'N':
return true;
break;

case 'W':
return true;
break;

case 'Z':
return true;
break;
}
return false;
}

int Pig::find_kill() {
if (kill_used && !crossbow) return 0;
switch (status) {
case 'M':
for (int i = array_index % n + 1; i != array_index; i = i % n + 1) {
if (pig[i].dead) continue;
if (pig[i].antilike || (pig[i].status == 'F' && pig[i].jumped))
return i;
else
return 0;
}
break;
case 'Z':
for (int i = array_index % n + 1; i != array_index; i = i % n + 1) {
if (pig[i].dead) continue;
if (pig[i].status == 'F' && pig[i].jumped)
return i;
else
return 0;
}
break;
case 'F':
for (int i = array_index % n + 1; i != array_index; i = i % n + 1) {
if (pig[i].dead) continue;
if (pig[i].status != 'F' && pig[i].jumped)
return i;
else
return 0;
}
break;
}
}

void Pig::kill(Pig &that) {
jump();
exit_antilike();
kill_used = true;
if (!that.kill_respond()) hurt(that);
}

int Pig::find_duel() {
switch (status) {
case 'M':
for (int i = array_index % n + 1; i != array_index; i = i % n + 1) {
if (pig[i].dead) continue;
if (pig[i].antilike || (pig[i].status == 'F' && pig[i].jumped))
return i;
}
break;
case 'Z':
for (int i = array_index % n + 1; i != array_index; i = i % n + 1) {
if (pig[i].dead) continue;
if (pig[i].status == 'F' && pig[i].jumped) return i;
}
break;
case 'F':
return king;
break;
}
return 0;
}

void Pig::duel(Pig &that) {
jump();
if (status == 'F') exit_antilike();
if (invulnerability(that)) return;
if (status == 'M' && that.status == 'Z') {
hurt(that);
return;
}
int tmp;
while (1) {
if (!that.find_card('K')) {
tmp = 1;
break;
}
if (!find_card('K')) {
tmp = 2;
break;
}
}
if (tmp == 1)
hurt(that);
else
that.hurt(*this);
}

void Pig::south_attack() {
for (int i = array_index % n + 1; i != array_index; i = i % n + 1) {
if (pig[i].dead) continue;
if (debug) {
printf("%d suffers a South Attack.\n", pig[i].array_index);
}
if (invulnerability(pig[i])) continue;
if (!pig[i].south_attack_respond()) {
hurt(pig[i]);
if (!jumped && pig[i].status == 'M') become_antilike();
if (gameover) return;
}
}
}

void Pig::arrows_shot() {
for (int i = array_index % n + 1; i != array_index; i = i % n + 1) {
if (pig[i].dead) continue;
if (invulnerability(pig[i])) continue;
if (debug) {
printf("%d suffers a Arrow.\n", pig[i].array_index);
}
if (!pig[i].arrows_shot_respond()) {
hurt(pig[i]);
if (!jumped && pig[i].status == 'M') become_antilike();
if (gameover) return;
}
}
}

bool Pig::invulnerability(Pig &that) {
if (!that.jumped) {
return false;
}
bool firstloop = true;
for (int i = array_index; i != array_index || firstloop; i = i % n + 1) {
firstloop = false;
if (pig[i].dead) continue;
if (pig[i].status == 'M' && that.status == 'F') continue;
if (pig[i].status == 'Z' && that.status == 'F') continue;
if (pig[i].status == 'F' && that.status == 'M') continue;
if (pig[i].status == 'F' && that.status == 'Z') continue;
if (pig[i].find_card('J')) {
pig[i].jump();
return !pig[i].invulnerability_respond();
}
}
return false;
}

bool Pig::invulnerability_respond() {
for (int i = array_index % n + 1; i != array_index; i = i % n + 1) {
if (pig[i].dead) continue;
if (status == 'M' && pig[i].status == 'Z') continue;
if (status == 'Z' && pig[i].status == 'M') continue;
if (status == 'Z' && pig[i].status == 'Z') continue;
if (status == 'F' && pig[i].status == 'F') continue;
if (pig[i].find_card('J')) {
pig[i].jump();
return !pig[i].invulnerability_respond();
}
}
return false;
}
# 模拟

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×