安特布斯特
人话:P2586 解题报告。
首先题面很良心地整理好了一秒内发生的所有事。
整理一下1秒钟内发生的事件:
1秒的最初,如果地图上蚂蚁数不足6,一只蚂蚁就会在洞口出生。
接着,蚂蚁们在自己所在点留下一些信息素后,考虑移动。先出生的蚂蚁先移动。
移动完毕后,如果有蚂蚁在蛋糕的位置上并且蛋糕没被拿走,它把蛋糕扛上,血量增加,并在这时被所有塔设成target。
然后所有塔同时开始攻击。如果攻击结束后那只扛着蛋糕的蚂蚁挂了,蛋糕瞬间归位。
攻击结束后,如果发现扛蛋糕的蚂蚁没死并在窝的位置,就认为蚂蚁抢到了蛋糕。
游戏也在此时结束。最后,地图上所有点的信息素损失1单位。所有蚂蚁的年龄加1。漫长的1秒到此结束。
这个东西不管怎么目测都是难以直接一套数组函数莽的(至少我不太行),所以肯定要用·结构体之类的东西。蚂蚁肯定是一个,炮的话本质上只需要一个位置(但是因为要计算距离所以其实记录的是一个向量,可以加减和算模的那种)。年龄加一有点小难受(其实还好),可以直接记录出生时间戳。
为了方便判断蚂蚁移动的合法性,直接用一个数组记录目标位置是否可达。为了懒得判边界,把所有坐标统一平移一下。蚂蚁出生点也就变成了
struct vec {
int x, y;
int mod() const { return x * x + y * y; }
};
vec operator+(const vec a, const vec b) { return {a.x + b.x, a.y + b.y}; }
vec operator-(const vec a, const vec b) { return {a.x - b.x, a.y - b.y}; }
bool operator==(const vec a, const vec b) { return a.x == b.x && a.y == b.y; }
bool isstb[19][19]; //是蚂蚁或障碍或边界
int infcnt[19][19]; //信息素
bool ckfree = true;
int n, m, s, d, r;
constexpr vec mov[4]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
struct ant {
vec pos, lstpos; //位置,上一秒位置
int tpos; //出生时间戳
int hp; //血量
int bthhp; //初始血量
bool hasck; //搬着蛋糕
int lvl; //等级
};
int cnt; //蚂蚁数量
vector<ant> ats; //蚂蚁
vector<vec> lsr; //激光炮
我们同时可以把一秒内发生的事情分成三部分来处理:蚂蚁行动,激光炮行动,回合结束结算,逐个击破。
激光炮的部分最烦,可以先行处理掉。
首先判断点到线段的距离是否超过
-
点到线段的垂足是否在线段上;
-
点到线段的距离是否
\le\frac{1}{2} 。
推推就能写了。注意到其实可以规定线段的一个端点在原点(也就是用向量做),写起来舒服。
bool oln(vec a, vec o) {
int delt = a.x * o.x + a.y * o.y, bs = a.x * a.x + a.y * a.y;
if (delt < 0 || delt > bs) return false;
delt = (a.x * o.y - a.y * o.x) << 1, delt = delt * delt;
return delt <= bs;
}
然后接下来炮的行动就可以直接模拟了:
-
选择目标;
-
开火。
-
杀蚂蚁
显然第二部分好写。
void atkant(const vec& cnl, const vec& pos) {
for (ant& a : ats)
if (oln(pos - cnl, a.pos - cnl)) a.hp -= d;
}
至于第一部分,其实也就是遍历所有蚂蚁判断距离,看到蛋糕在射程以内就直接出击。
for (const vec& cnl : lsr) {
int dst = 0;
bool ok = false;
vec pos{0, 0};
for (const ant& a : ats) {
int ndst = (a.pos - cnl).mod();
if (ndst <= r * r) {
if (a.hasck) {
pos = a.pos;
ok = true;
break;
}
if (!ok || ndst < dst) {
dst = ndst;
ok = true;
pos = a.pos;
}
}
}
if (ok) atkant(cnl, pos);
}
最后处理一下蚂蚁死亡的情况。
for (auto it = ats.begin(); it != ats.end();)
if (it->hp < 0) {
if (it->hasck) ckfree = true;
isstb[it->pos.x][it->pos.y] = false;
it = ats.erase(it);
} else
++it;
最难的一部分搞定了,接下来处理很烦的蚂蚁行动。
首先蚂蚁出生 & 信息素增加极其好写。
if (int(ats.size()) != 6 && !isstb[1][1]) {
int lv = (cnt++) / 6 + 1, hp = 4 * powl(1.1, lv);
ats.push_back({{1, 1}, {0, 0}, t, hp, hp, false, lv});
isstb[1][1] = true;
}
for (const ant& a : ats) {
infcnt[a.pos.x][a.pos.y] += 2;
if (a.hasck) infcnt[a.pos.x][a.pos.y] += 3;
}
蚂蚁移动的话想了想还是决定丢到类里面。
for (ant& a : ats) a.chsdmov(t);
这里需要我们调用这段函数的时候传入当前时间戳。
在蚂蚁的类里,方便起见先写个辅助函数判断当前移动是否合法。
bool movlgl(int id) const {
vec npos = pos + mov[id];
if (!(pos == lstpos) && npos == lstpos) return false;
if (isstb[npos.x][npos.y]) return false;
return true;
}
然后进入到移动。先按照基础规则选个方向(没有的话记为负一)。
int cinf = -1, ret = -1;
for (int i = 0; i < 4; ++i)
if (movlgl(i)) {
int ninf = infcnt[pos.x + mov[i].x][pos.y + mov[i].y];
if (ninf > cinf) ret = i, cinf = ninf;
}
然后不管动没动蛋糕都是要处理的。
if (ckfree && pos == vec{n + 1, m + 1}) {
hasck = true;
ckfree = false;
hp = min(bthhp, hp + (bthhp >> 1));
}
最后处理一下转向和前进。
if (ret != -1) {
if (!((t - tpos + 1) % 5)) do
ret = ret ? ret - 1 : 3;
while (!movlgl(ret));
isstb[pos.x][pos.y] = false;
lstpos = pos;
pos = pos + mov[ret];
isstb[pos.x][pos.y] = true;
} else
lstpos = pos;
这部分也就告一段落了。
接下来是回合结算的部分,极其好写。这个函数返回一个值用以标记结束了没。
bool endsec() {
for (int i = 1; i <= n + 1; ++i)
for (int j = 1; j <= m + 1; ++j)
if (infcnt[i][j]) --infcnt[i][j];
for (const ant& a : ats)
if (a.hasck && a.pos == vec{1, 1}) return true;
return false;
}
最后完善一下主函数。
signed main() {
cin >> n >> m >> s >> d >> r, lsr.resize(s);
for (int i = 0; i <= n + 2; ++i) isstb[i][0] = isstb[i][m + 2] = true;
for (int i = 0; i <= m + 2; ++i) isstb[0][i] = isstb[n + 2][i] = true;
for (int i = 0; i < s; ++i)
cin >> lsr[i].x >> lsr[i].y, isstb[++lsr[i].x][++lsr[i].y] = true;
int T;
cin >> T;
for (int i = 1; i <= T; /* printants(i),*/ ++i)
if (begsec(i), cnlatk(), endsec()) {
cout << "Game over after " << i << " seconds\n";
printants(i - 1);
return 0;
}
cout << "The game is going on\n";
printants(T);
return 0;
}
发现还需要补一个输出蚂蚁信息的工具函数。
void printants(int t) {
cout << ats.size() << '\n';
for (const ant& a : ats)
cout << t - a.tpos + 1 << ' ' << a.lvl << ' ' << a.hp << ' ' << a.pos.x - 1
<< ' ' << a.pos.y - 1 << '\n';
}
把以上所有合到一起,这道紫色的大膜你也就迎刃而解了~
完整代码:
#include <bits/stdc++.h>
using namespace std;
struct vec {
int x, y;
int mod() const { return x * x + y * y; }
};
vec operator+(const vec a, const vec b) { return {a.x + b.x, a.y + b.y}; }
vec operator-(const vec a, const vec b) { return {a.x - b.x, a.y - b.y}; }
bool operator==(const vec a, const vec b) { return a.x == b.x && a.y == b.y; }
bool isstb[19][19];
int infcnt[19][19];
bool ckfree = true;
int n, m, s, d, r;
constexpr vec mov[4]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
struct ant {
vec pos, lstpos;
int tpos;
int hp;
int bthhp;
bool hasck;
int lvl;
bool movlgl(int id) const {
vec npos = pos + mov[id];
if (!(pos == lstpos) && npos == lstpos) return false;
if (isstb[npos.x][npos.y]) return false;
return true;
}
void chsdmov(int t) {
int cinf = -1, ret = -1;
for (int i = 0; i < 4; ++i)
if (movlgl(i)) {
int ninf = infcnt[pos.x + mov[i].x][pos.y + mov[i].y];
if (ninf > cinf) ret = i, cinf = ninf;
}
if (ret != -1) {
if (!((t - tpos + 1) % 5)) do
ret = ret ? ret - 1 : 3;
while (!movlgl(ret));
isstb[pos.x][pos.y] = false;
lstpos = pos;
pos = pos + mov[ret];
isstb[pos.x][pos.y] = true;
} else
lstpos = pos;
if (ckfree && pos == vec{n + 1, m + 1}) {
hasck = true;
ckfree = false;
hp = min(bthhp, hp + (bthhp >> 1));
}
}
};
int cnt;
vector<ant> ats;
vector<vec> lsr;
bool oln(vec a, vec o) {
int delt = a.x * o.x + a.y * o.y, bs = a.x * a.x + a.y * a.y;
if (delt < 0 || delt > bs) return false;
delt = (a.x * o.y - a.y * o.x) << 1, delt = delt * delt;
return delt <= bs;
}
void atkant(const vec& cnl, const vec& pos) {
for (ant& a : ats)
if (oln(pos - cnl, a.pos - cnl)) a.hp -= d;
}
void cnlatk() {
for (const vec& cnl : lsr) {
int dst = 0;
bool ok = false;
vec pos{0, 0};
for (const ant& a : ats) {
int ndst = (a.pos - cnl).mod();
if (ndst <= r * r) {
if (a.hasck) {
pos = a.pos;
ok = true;
break;
}
if (!ok || ndst < dst) {
dst = ndst;
ok = true;
pos = a.pos;
}
}
}
if (ok) atkant(cnl, pos);
}
for (auto it = ats.begin(); it != ats.end();)
if (it->hp < 0) {
if (it->hasck) ckfree = true;
isstb[it->pos.x][it->pos.y] = false;
it = ats.erase(it);
} else
++it;
}
bool endsec() {
for (int i = 1; i <= n + 1; ++i)
for (int j = 1; j <= m + 1; ++j)
if (infcnt[i][j]) --infcnt[i][j];
for (const ant& a : ats)
if (a.hasck && a.pos == vec{1, 1}) return true;
return false;
}
void begsec(int t) {
if (int(ats.size()) != 6 && !isstb[1][1]) {
int lv = (cnt++) / 6 + 1, hp = 4 * powl(1.1, lv);
ats.push_back({{1, 1}, {0, 0}, t, hp, hp, false, lv});
isstb[1][1] = true;
}
for (const ant& a : ats) {
infcnt[a.pos.x][a.pos.y] += 2;
if (a.hasck) infcnt[a.pos.x][a.pos.y] += 3;
}
for (ant& a : ats) a.chsdmov(t);
}
void printants(int t) {
cout << ats.size() << '\n';
for (const ant& a : ats)
cout << t - a.tpos + 1 << ' ' << a.lvl << ' ' << a.hp << ' ' << a.pos.x - 1
<< ' ' << a.pos.y - 1 << '\n';
}
signed main() {
cin >> n >> m >> s >> d >> r, lsr.resize(s);
for (int i = 0; i <= n + 2; ++i) isstb[i][0] = isstb[i][m + 2] = true;
for (int i = 0; i <= m + 2; ++i) isstb[0][i] = isstb[n + 2][i] = true;
for (int i = 0; i < s; ++i)
cin >> lsr[i].x >> lsr[i].y, isstb[++lsr[i].x][++lsr[i].y] = true;
int T;
cin >> T;
for (int i = 1; i <= T; /* printants(i),*/ ++i)
if (begsec(i), cnlatk(), endsec()) {
cout << "Game over after " << i << " seconds\n";
printants(i - 1);
return 0;
}
cout << "The game is going on\n";
printants(T);
return 0;
}