题目:杀蚂蚁
源代码 - C++
#include<bits/stdc++.h>
using namespace std;
int n,m,s,d,r; //n,m?????,s?????,d???,s?????
int vis_Ant[35][35];
int Ant_cnt=0,times=1;
int mp[35][35]; //map,信息素
bool cake=true;
int number=0;
struct Ant
{
int age=1;
int level;
int HP;
int x=0,y=0;
bool died=false;
bool istarget=false;
bool born=false;
int lastx=-1,lasty=-1;
} ant[10005];
struct Hull
{
int x,y;
} hull[10005];
bool vis_p[35][35]; //??????
int vis_m[35][35]; //??????
int dx[]={0,0,1,0,-1};
int dy[]={0,1,0,-1,0}; //????
#define vis vis_p
inline void _init()
{
for(int i=1;i<=6;i++)
{
vis_m[ant[i].x][ant[i].y]=1;
}
}
void move(int x,int y,int id)
{
if(ant[id].age%5==0)
{
int fangxiang=1;
fangxiang=(fangxiang%4)+1;
int cishu=1;
int nx=x+dx[fangxiang];
int ny=y+dy[fangxiang];
while(vis_m[nx][ny] or vis[nx][ny]==1 or nx<0 or nx>n or ny<0 or ny>m or nx==0 and ny==0 or ant[id].lastx==nx and ant[id].lasty==ny and cishu<5)
{
fangxiang=(fangxiang%4)+1;
cishu++;
}
vis_m[ant[id].x][ant[id].y]=0;
ant[id].lastx=ant[id].x,ant[id].lasty=ant[id].y;
vis_Ant[ant[id].x][ant[id].y]-=1;
vis_m[x+dx[fangxiang]][y+dy[fangxiang]]=id;
ant[id].x=x+dx[fangxiang];
ant[id].y=y+dy[fangxiang];
vis_Ant[x+dx[fangxiang]][y+dy[fangxiang]]+=1;
//
if(ant[id].istarget) mp[x+dx[fangxiang]][y+dy[fangxiang]]+=5;
else mp[x+dx[fangxiang]][y+dy[fangxiang]]+=2;
}
else
{
int fangxiang=1,maxxx=0;
for(int i=1;i<=4;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(vis_m[nx][ny] or vis[nx][ny]==1 or nx<0 or nx>n or ny<0 or ny>m or nx==0 and ny==0 or ant[id].lastx==nx and ant[id].lasty==ny)
{
continue;
}
if(mp[nx][ny]>maxxx)
{
fangxiang=i,maxxx=mp[nx][ny];
}
if(nx==n and ny==m and cake)
{
ant[id].istarget=true;
cake=false;
}
}
vis_m[ant[id].x][ant[id].y]=0;
ant[id].lastx=ant[id].x,ant[id].lasty=ant[id].y;
vis_Ant[ant[id].x][ant[id].y]-=1;
vis_m[x+dx[fangxiang]][y+dy[fangxiang]]=id;
ant[id].x=x+dx[fangxiang];
ant[id].y=y+dy[fangxiang];
vis_Ant[x+dx[fangxiang]][y+dy[fangxiang]]+=1;
//
if(ant[id].istarget) mp[x+dx[fangxiang]][y+dy[fangxiang]]+=5;
else mp[x+dx[fangxiang]][y+dy[fangxiang]]+=2;
}
}
void timegoing()
{
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
if(mp[i][j]) mp[i][j]--;
}
}
}
void attack()
{
for(int i=1;i<=s;i++)
{
int mx=n+1,my=m+1,did,tid=0; //mx,my表示目前最近的蚂蚁与炮台的距离差
int sx=hull[i].x;
int sy=hull[i].y;
for(int x=0;x<=r;x++)
{
if(tid) continue;
for(int y=0;y<=r;y++)
{
if(tid) continue;
if(vis_Ant[sx+x][sy+y])
{
if((mx>x or my>y) and mx+my>x+y)
{
mx=x,my=y,did=vis_Ant[sx+x][sy+y];
}
if(ant[vis_Ant[sx+x][sy+y]].istarget)
{
ant[vis_Ant[sx+x][sy+y]].HP-=d;
tid=1;
}
}
if(vis_Ant[sx-x][sy-y])
{
if((mx>sx-x or my>sy-y) and mx-my>x-y)
{
mx=x,my=y,did=vis_Ant[sx-x][sy-y];
}
if(ant[vis_Ant[sx-x][sy-y]].istarget)
{
ant[vis_Ant[sx-x][sy-y]].HP-=d;
tid=1;
}
}
}
}
ant[did].HP-=d;
}
}
void birthAnt()
{
if(!vis_Ant[0][0] and Ant_cnt<6)
{
Ant_cnt++;
number++;
ant[number].born=true;
ant[number].level=(Ant_cnt+5)/6;
ant[number].HP=floor(4*pow(1.1,ant[number].level));
vis_Ant[0][0]+=1;
}
}
void Antmove()
{
for(int i=1;i<=Ant_cnt;i++)
{
move(ant[i].x,ant[i].y,i);
}
}
void ifAntdie()
{
for(int i=1;i<=10005;i++)
{
if(ant[i].died) continue;
else if(ant[i].HP<0)
{
ant[i].died=true;
Ant_cnt--;
}
}
}
void Antbig()
{
for(int i=1;i<=number;i++)
{
if(ant[i].died) continue;
else ant[i].age++;
}
}
bool checkgame()
{
int idx=vis_Ant[0][0];
if(!idx) return false;
else if(ant[idx].istarget)
{
return true;
}
else return false;
}
void update()
{
birthAnt();
Antmove();
attack();
Antbig();
checkgame();
if(checkgame())
{
cout<<"Game over after "<<times<<" seconds";
exit(0);
}
else times++;
}
int main()
{
memset(mp,0,sizeof mp);
cin>>n>>m;
cin>>s>>d>>r;
birthAnt();
for(int i=1;i<=s;i++)
{
int x,y;
cin>>x>>y;
hull[i].x=x,hull[i].y=y;
vis[x][y]=1;
}
int count;
cin>>count;
while(count--) update();
for(int i=1;i<=number;i++)
{
if(ant[i].HP<0) Ant_cnt--;
}
cout<<"The game is going on\n";
cout<<Ant_cnt<<endl;
for(int i=1;i<=number;i++)
{
if(ant[i].HP<0) continue;
cout<<ant[i].age-1<<' '<<ant[i].level<<' '<<ant[i].HP<<' '<<ant[i].x<<' '<<ant[i].y<<endl;
}
return 0;
}
哪位大佬能看一下attack函数不攻击蚂蚁的问题