BZOJ1033 杀蚂蚁

https://www.zybuluo.com/Jerusalem/note/221811

模拟题就不提示了……
注意:
1. 蛋糕在谁手上这个信息一定要随时维护
2. 不是所有时刻都可以产生新蚂蚁的 。 总数==6自然不能 , 还有如果在蚂蚁窝里已经存在了一只蚂蚁 , 这时候也是不能产生新蚂蚁的。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <deque>
#include <stack>
#include <algorithm>
#include <cassert>

using namespace std;
const double eps = 1e-10;
int dcmp(double a) { return fabs(a)<eps?0:(a<0?-1:1); }


const int maxn = 10;
const int antsLimit = 6;
const int towerLimit = 21;
const int INF = 0x3f3f3f3f;

inline int re() { int n; scanf("%d",&n); return n; }

struct ant
{
    int age , lev , blo , x , y , lx , ly , ori , c;
    ant(int lev = 0):lev(lev)
    {
        blo = ori = (int)(4.0*pow(1.1, lev));
        c = x = y = 0;
        lx = ly = -1;
        age = 1;
    }

    bool operator ==(const ant& b)const { return b.age==age; }
    bool operator <(const ant& b)const { return b.age<age; }
};
struct points { double x , y; points(double x = 0 , double y =0):x(x),y(y){} };
typedef  points Vector;

Vector operator +(Vector a , Vector b){ return Vector(a.x+b.x , a.y+b.y); }
Vector operator -(Vector a , Vector b){ return Vector(a.x-b.x , a.y-b.y); }
Vector operator *(Vector a , double b){ return Vector(a.x*b   , a.y*b  ); }
Vector rotate(Vector a , double rad) { return Vector(a.x*cos(rad)-a.y*sin(rad) , a.x*sin(rad)+a.y*cos(rad)); }

double Dot(Vector a , Vector b) { return a.x*b.x+a.y*b.y; }
double Cross(Vector a , Vector b) { return a.x*b.y-a.y*b.x; }
double Length(Vector a){ return sqrt(Dot(a, a)); }
double distanceToLine(points p , points a , points b) { return Cross(p-a, p-b)/Length(a-b); }


int n , m , s , d , r;
int an , target = -1;
ant a[antsLimit];
points tower[towerLimit];

int g[maxn][maxn];
int livings;
void giveBirth()
{
    if(an>=antsLimit) return;

    for(int i=0;i<an;i++) if(a[i].x==0 && a[i].y==0) return; 

    livings++;  a[an++] = ant((livings+5)/6); 
    for(int i=0;i<an;i++) for(int j=i+1;j<an;j++) if(a[j]<a[i])
    {
        swap(a[j], a[i]);
        if(target==i) target = j;
        if(target==j) target = i;
    }
}

void Mark()
{
    for(int i=0;i<an;i++) if(i==target) g[a[i].x][a[i].y]+= 5; else g[a[i].x][a[i].y]+=2;
}

const int mn = 4;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};

bool judge(int x , int y) { return x>=0 && x<=n && y>=0 && y<=m; }
void Move(int w)
{
    ant& now = a[w];
    bool sp = (a[w].age%5==0);

    int f[mn];
    int Max = -INF , face = -1;

    int book[maxn][maxn]={};

    for(int i=0;i<an;i++) book[a[i].x][a[i].y] = 1;
    for(int i=0;i<=n;i++) for(int j=0;j<=m;j++) if(g[i][j]==-1) book[i][j] = 1;

    for(int i=0;i<mn;i++)
    {
        int nx = now.x+dx[i];
        int ny = now.y+dy[i];

        f[i] = -INF;
        if(!judge(nx, ny) || book[nx][ny] || (nx==now.lx && ny==now.ly)) continue;

        Max = max(Max , f[i] = g[nx][ny]);
    }

    now.lx = now.x , now.ly = now.y;
    if(Max<0) return;

    for(int i=0;i<mn;i++) if(f[i]==Max) { face = i; break; }
    if(sp)
        for(int j=(face-1+mn)%mn;j!=face;j = (j-1+mn)%mn) if(f[j]!=-INF) { face = j; break; }

    now.x+= dx[face];
    now.y+= dy[face];
}


void getCake()
{
    for(int i=0;i<an;i++) if(a[i].x==n && a[i].y==m)
    {
        target = i;
        a[i].c = 1;
        a[i].blo += a[i].ori/2;
        if(a[i].blo>a[i].ori) a[i].blo = a[i].ori;
        return;
    }
}



points getLineIntersection(points p , Vector v , points q , Vector w)
{
    Vector u = p-q;
    double t = Cross(w, u)/Cross(v, w);
    return p+v*t;
}

bool inCircle(points p , points o , double r){ return dcmp(Length(p-o)-r)<=0; }
bool onSegment(points p , points a , points b) { return dcmp(Dot(a-p, b-p))==-1; }

bool isReachable(points a , points b , points o)
{
    if(inCircle(a, o, 0.5) || inCircle(b, o, 0.5)) return true;
    if(dcmp(distanceToLine(o, a, b)-0.5)>0) return false;
    points h = getLineIntersection(a, b-a, o, rotate(b-a, acos(-1)/2.0));
    Vector unit = (b-a)*(1.00000/Length(b-a));
    double len = sqrt(0.25-Length(h-o)*Length(h-o));
    points p1 = h+unit*len;
    points p2 = h-unit*len;
    return onSegment(p1, a, b) || onSegment(p2, a, b);
}

void lazer(points A , points B)
{
    for(int i=0;i<an;i++)
            if(isReachable(A, B, points(a[i].x , a[i].y))) a[i].blo -=d;
//  {
//      int x1 = A.x , x2 = B.x , x3 = a[i].x , y1 = A.y , y2 = B.y , y3 = a[i].y;
//      if (x3 < (min(x1 , x2)) || x3 > (max(x1 , x2)) || y3 < min(y1 , y2) || y3 > max(y1 , y2))  continue;
//      int tmp = abs(x1 * y2 + x2 * y3 + x3 * y1 - x2 * y1 - x3 * y2 - x1 * y3);
//      tmp *= tmp;
//      int len = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
//      if ((tmp << 2) <= len) a[i].blo -= d;
//  }
}

void KillNearest(points o)
{
    int who = -1;
    double Min = INF;
    for(int i=0;i<an;i++)
        if(dcmp(Min-Length(o-points(a[i].x , a[i].y)))==1) who = i , Min = Length(o-points(a[i].x , a[i].y));
        else if(!dcmp(Min-Length(o-points(a[i].x , a[i].y))) && a[i]<a[who]) who = i;

    if(who==-1 || dcmp(Min - r)>0) return;
    a[who].blo -= d;
}

void attack()
{
    points t;
    if(target!=-1) t = points(a[target].x , a[target].y);

    for(int i=0;i<s;i++)
    {
        if(target!=-1 && dcmp(Length(t-tower[i])-r)<=0) lazer(tower[i], t);
        else KillNearest(tower[i]);
    }
}

ant na[antsLimit];
void sweapDeath()
{
    int cnt = 0;
    ant theone;
    if(target!=-1) theone = a[target];

    for(int i=0;i<an;i++) if(a[i].blo>=0) na[cnt++] = a[i];

    an = cnt;
    for(int i=0;i<an;i++) a[i] = na[i];
    if(target!=-1)
    {
        target = -1;
        for(int i=0;i<an;i++) if(a[i]==theone) { target = i; break; }
    }
}


bool over()
{
    return target!=-1 && a[target].x==0 && a[target].y==0;
}

void add()
{
    for(int i=0;i<=n;i++) for(int j=0;j<=m;j++) if(g[i][j]>0) g[i][j]--;
    for(int i=0;i<an;i++) a[i].age++;
}

int main(int argc, char *argv[]) {
//  freopen("antbuster2.in" , "r",stdin);
    n = re(); m = re();

    s = re(); d = re(); r = re();

    for(int i=0;i<s;i++) tower[i].x = re() , tower[i].y = re() , g[(int)tower[i].x][(int)tower[i].y] = -1;

    int T = re() , t;
    for(t=1;t<=T;t++)
    {
        giveBirth();
        Mark();

        for(int i=0;i<an;i++) Move(i);
        if(target==-1) getCake();

        attack();
        sweapDeath();

        if(over()) break;
        add();
    }

    if(t==T+1) puts("The game is going on");
    else printf("Game over after %d seconds\n", t);

    sort(a, a+an);
    printf("%d\n",an);
    for(int i=0;i<an;i++) printf("%d %d %d %d %d\n", a[i].age-1 , a[i].lev , a[i].blo , a[i].x , a[i].y);

    return 0;
}

原谅我这个题代码不漂亮……

提供数据几组:

Test Input 7:
4 7
0 0 0
60200

Test Output 7:
Game over after 60200 seconds
6
60199 1 4 0 0
60198 1 4 2 6
60197 1 4 4 7
60196 1 4 2 1
60195 1 4 3 5
60194 1 4 4 5

TestInput 2:
4 4
4 10 5
1 1
2 2
1 2
2 1
1000

TestOutput 2:
Game over after 831 seconds
6
110 49 86 2 3
88 49 26 0 1
77 49 286 3 1
58 50 29 0 0
22 50 419 4 3
10 50 469 3 2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值