题目链接: BZOJ - 1033

题目分析

模拟!纯粹按照题目描述模拟!

这是一道喜闻乐见的经典模拟题!

我一共写了2遍,Debug 历时2天的所有晚自习 ... 时间超过 8h ... 我真是太弱了啊 ...

最终对着数据和 std 终于找到错误了!

错误:好好读题!不要忽略题意中的细节!在函数中提前退出的时候想想会不会有什么事情还没有做。

Warning!Warning!Warning!

注意在给 sort 写 Cmp 的时候,一定一定一定要保证比较结果双向统一! 若 Cmp(a, b) == false , 那么 Cmp(b, a) 一定要为 true !!!

否则就会跪掉!一定要注意!Cmp里有一些特殊条件判断的时候一定要注意这一点!

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; typedef double DB; const int MaxN = 8 + 3, MaxA = 6 + 3, MaxB = 20 + 3, INF = 999999999;
const int Dx[5] = {0, 1, 0, -1}, Dy[5] = {1, 0, -1, 0}; int n, m, B_s, B_d, B_r, Time, Ant_Index, Target, Ant_s, Over_Time, Lev_Now;
int Msg[MaxN][MaxN], Tgt[MaxB]; DB Hp_Now; bool Map[MaxN][MaxN]; inline int gmin(int a, int b) {return a < b ? a : b;}
inline int gmax(int a, int b) {return a > b ? a : b;} struct Battery
{
int x, y;
} B[MaxB]; inline bool OKP(int x, int y) {
if (x < 0 || x > n) return false;
if (y < 0 || y > m) return false;
return (!Map[x][y]);
} struct Ant
{
int Hp, Hp_Ceiling, Lev, Age, x, y, Lx, Ly;
bool Alive, Cake; Ant() {
x = y = 0;
Lx = Ly = -1;
Age = 0;
Lev = Lev_Now;
Hp = Hp_Ceiling = (int)Hp_Now;
Alive = true; Cake = false;
} void Move() {
int MaxMsg = -1, nk = -1, xx, yy;
for (int i = 0; i < 4; ++i) {
xx = x + Dx[i]; yy = y + Dy[i];
if (!OKP(xx, yy) || (xx == Lx && yy == Ly)) continue;
if (Msg[xx][yy] > MaxMsg) {
MaxMsg = Msg[xx][yy];
nk = i;
}
}
if (MaxMsg == -1) {
Lx = x; Ly = y;
return;
}
if ((Age + 1) % 5 == 0) {
while (true) {
nk = ((nk - 1) + 4) % 4;
xx = x + Dx[nk]; yy = y + Dy[nk];
if (!OKP(xx, yy) || (xx == Lx && yy == Ly)) continue;
break;
}
}
Lx = x; Ly = y;
x += Dx[nk]; y += Dy[nk];
Map[Lx][Ly] = false;
Map[x][y] = true;
} void GetCake() {
Cake = true;
Hp = gmin(Hp_Ceiling, Hp + (Hp_Ceiling >> 1));
} void Print() {
printf("%d %d %d %d %d\n", Age, Lev, Hp, x, y);
}
} A[MaxA]; void Init() {
for (int i = 1; i <= 6; ++i) {
A[i].Alive = false;
A[i].Cake = false;
}
Hp_Now = 4.4; Lev_Now = 1;
Ant_Index = 0;
memset(Map, 0, sizeof(Map));
for (int i = 1; i <= B_s; ++i) Map[B[i].x][B[i].y] = true;
memset(Msg, 0, sizeof(Msg));
Ant_s = 0;
Target = -1;
Over_Time = -1;
} void New_Ant_Born() {
if (Map[0][0] || Ant_s == 6) return;
++Ant_Index;
if (Ant_Index == 7) {
Ant_Index = 1;
Hp_Now *= 1.1;
++Lev_Now;
}
A[++Ant_s] = Ant();
Map[0][0] = true;
} void Add_Message() {
for (int i = 1; i <= Ant_s; ++i) {
if (Target == i) Msg[A[i].x][A[i].y] += 5;
else Msg[A[i].x][A[i].y] += 2;
}
} void Ant_Move() {
for (int i = 1; i <= Ant_s; ++i) {
A[i].Move();
if (Target == -1 && A[i].x == n && A[i].y == m) {
Target = i;
A[i].GetCake();
}
}
} inline int Sqr(int x) {return x * x;}
inline int Abs(int x) {return x < 0 ? -x : x;} int SqrDis(int x, int y, int xx, int yy) {
return Sqr(x - xx) + Sqr(y - yy);
} int Nearest(int x, int y) {
int MinDis = INF, Dx, ret;
for (int i = 1; i <= Ant_s; ++i) {
Dx = SqrDis(A[i].x, A[i].y, x, y);
if (Dx < MinDis) {
ret = i;
MinDis = Dx;
}
}
return ret;
} bool Intersect(int x, int y, int xa, int ya, int xb, int yb) {
if (x < gmin(xa, xb) || x > gmax(xa, xb) || y < gmin(ya, yb) || y > gmax(ya, yb)) return false;
int S2;
S2 = Abs(xa * yb + y * xb + x * ya - y * xa - x * yb - xb * ya);
S2 = Sqr(S2);
return (S2 * 4 <= SqrDis(xa, ya, xb, yb));
} void Battery_Attack() {
if (Target == -1)
for (int i = 1; i <= B_s; ++i) Tgt[i] = Nearest(B[i].x, B[i].y);
else
for (int i = 1; i <= B_s; ++i)
if (SqrDis(B[i].x, B[i].y, A[Target].x, A[Target].y) <= Sqr(B_r)) Tgt[i] = Target;
else Tgt[i] = Nearest(B[i].x, B[i].y);
for (int i = 1; i <= B_s; ++i) {
if (SqrDis(B[i].x, B[i].y, A[Tgt[i]].x, A[Tgt[i]].y) > Sqr(B_r)) continue;
for (int j = 1; j <= Ant_s; ++j)
if (Intersect(A[j].x, A[j].y, B[i].x, B[i].y, A[Tgt[i]].x, A[Tgt[i]].y))
A[j].Hp -= B_d;
}
int Temp = 0;
for (int i = 1; i <= Ant_s; ++i) {
if (A[i].Hp >= 0) continue;
A[i].Alive = false;
Map[A[i].x][A[i].y] = false;
if (A[i].Cake) {
Target = -1;
A[i].Cake = false;
}
++Temp;
}
Ant_s -= Temp;
} bool Check_Over() {
if (Target == -1) return false;
return (A[Target].x == 0 && A[Target].y == 0);
} void Decrease_Message() {
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= m; ++j)
if (Msg[i][j] > 0) --Msg[i][j];
} bool Cmp(Ant a1, Ant a2) {
if (a1.Alive == false && a2.Alive) return false;
if (a1.Alive && a2.Alive == false) return true;
return a1.Age > a2.Age;
} void Update_Ant() {
sort(A + 1, A + 6 + 1, Cmp);
for (int i = 1; i <= Ant_s; ++i) {
++A[i].Age;
if (A[i].Cake) Target = i;
}
} void Print_Situation() {
printf("%d\n", Ant_s);
for (int i = 1; i <= Ant_s; ++i) A[i].Print();
} int main()
{
scanf("%d%d", &n, &m);
scanf("%d%d%d", &B_s, &B_d, &B_r);
for (int i = 1; i <= B_s; ++i)
scanf("%d%d", &B[i].x, &B[i].y);
scanf("%d", &Time); Init();
for (int i = 1; i <= Time; ++i) {
New_Ant_Born();
Add_Message();
Ant_Move();
Battery_Attack();
if (Check_Over()) {
sort(A + 1, A + 6 + 1, Cmp);
Over_Time = i;
break;
}
Decrease_Message();
Update_Ant();
}
if (Over_Time != -1) printf("Game over after %d seconds\n", Over_Time);
else printf("The game is going on\n");
Print_Situation();
return 0;
}

  

[BZOJ 1033] [ZJOI2008] 杀蚂蚁antbuster 【模拟!】的更多相关文章

  1. BZOJ 1033: [ZJOI2008]杀蚂蚁antbuster(模拟)

    坑爹的模拟题QAQ DEBUG多了1kb QAQ 按题意做就行了 注意理解题意啊啊啊啊 尼玛输出忘换行wa了3次QAQ CODE: #include<cstdio>#include< ...

  2. [BZOJ 1033][ZJOI2008]杀蚂蚁antbuster

    1033: [ZJOI2008]杀蚂蚁antbuster Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1200  Solved: 507[Submi ...

  3. BZOJ1033:[ZJOI2008]杀蚂蚁antbuster(模拟)

    Description 最近,佳佳迷上了一款好玩的小游戏:antbuster.游戏规则非常简单:在一张地图上,左上角是蚂蚁窝,右 下角是蛋糕,蚂蚁会源源不断地从窝里爬出来,试图把蛋糕搬回蚂蚁窝.而你的 ...

  4. [ZJOI2008]杀蚂蚁antbuster

    [ZJOI2008]杀蚂蚁antbuster 题目 最近,佳佳迷上了一款好玩的小游戏:antbuster.游戏规则非常简单:在一张地图上,左上角是蚂蚁窝,右下角是蛋糕,蚂蚁会源源不断地从窝里爬出来,试 ...

  5. P2586 [ZJOI2008]杀蚂蚁(模拟)

    P2586 [ZJOI2008]杀蚂蚁 大模拟. 什么都不想补了. 看变量名感性理解吧 #include<iostream> #include<cstdio> #include ...

  6. 【BZOJ 1033】 [ZJOI2008]杀蚂蚁antbuster

    Description 最近,佳佳迷上了一款好玩的小游戏:antbuster.游戏规则非常简单:在一张地图上,左上角是蚂蚁窝,右下角是蛋糕,蚂蚁会源源不断地从窝里爬出来,试图把蛋糕搬回蚂蚁窝.而你的任 ...

  7. bzoj千题计划121:bzoj1033: [ZJOI2008]杀蚂蚁antbuster

    http://www.lydsy.com/JudgeOnline/problem.php?id=1033 经半个下午+一个晚上+半个晚上 的 昏天黑地调代码 最终成果: codevs.洛谷.tyvj上 ...

  8. [bzoj1033] [ZJOI2008]杀蚂蚁antbuster

    Description 最近,佳佳迷上了一款好玩的小游戏:antbuster.游戏规则非常简单:在一张地图上,左上角是蚂蚁窝,右下角是蛋糕,蚂蚁会源源不断地从窝里爬出来,试图把蛋糕搬回蚂蚁窝.而你的任 ...

  9. [ZJOI2008]杀蚂蚁antbuster 题解

    一个题目的可读版本:https://www.zybuluo.com/Jerusalem/note/221811 这两天做的又一道大模拟题,感觉这玩意有毒,会上瘾啊…… 比起猪国杀这道题真心不知道高到哪 ...

随机推荐

  1. Android-自己定义图像资源的使用(1)

    Android-自己定义图像资源的使用 2014年4月28日 周一 天气晴朗 心情平静 本篇博文给大家介绍一下,在Android开发中经经常使用到的一些图像资源,具体内容麻烦请各位认真查看官网,下面附 ...

  2. Linux之TCPIP内核参数优化

    /proc/sys/net目录 所有的TCP/IP参数都位于/proc/sys/net目录下(请注意,对/proc/sys/net目录下内容的修改都是临时的,任何修改在系统重启后都会丢失),例如下面这 ...

  3. MySQL Error Handling in Stored Procedures---转载

    This tutorial shows you how to use MySQL handler to handle exceptions or errors encountered in store ...

  4. Android开发报错系列(一),java.lang.NullPointerException,at android.widget.ListView.setupChild

    问题描述:运行代码是报空指针错误,java.lang.NullPointerException,at Android.widget.ListView.setupChild 问题定位:listview控 ...

  5. IE兼容问题

    1.IE下event事件没有target属性,只有srcElement属性,解决方法:使用srcObj = event.srcElement ? event.srcElement : event.ta ...

  6. Java-strurs总结

    这里是自己对自学的struts2 的一个整体 的脉络进行的一个概括,需要学习哪些东西,注重哪些东西: struts2 是主流框架SSH 中的一个"S" ,准备MVC开发标准的一个框 ...

  7. 分离数据库(Detach database).

    Many times, we often needs to detach our databases if we want to copy it to another database instanc ...

  8. MSSQL批量替换网址字符串语句

    1.如何批量替换ntext字段里面的数据 问题描述: 我想把数据库中News表中的字段content中的一些字符批量替换. 我的content字段是ntext类型的. 我想替换的字段是content字 ...

  9. Deep Learning 学习随记(六)Linear Decoder 线性解码

    线性解码器(Linear Decoder) 前面第一章提到稀疏自编码器(http://www.cnblogs.com/bzjia-blog/p/SparseAutoencoder.html)的三层网络 ...

  10. 【HDU4348】【主席树】To the moon

    Problem Description BackgroundTo The Moon is a independent game released in November 2011, it is a r ...