早训的cf题目,当时一直没写出来但是知道怎么写一直调不出来就很烦,因为可能是负数,所以我们把(0,0)变成(1000,1000)结合题目给定的蚂蚁数量就再也不可能有负数出现了,先bfs预处理出所有蚂蚁的位置就很舒服了,可是细节不到位,写撮了。
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<deque>
#include<map>
#include<stdlib.h>
#include<set>
#include<iomanip>
#include<stack>
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define ull unsigned long long
#define se second
#define endl "\n"
#define bug cout<<"----acac----"<<endl
#define IOS ios::sync_with_stdio(false), cin.tie(0),cout.tie(0)
using namespace std;
const int maxn =1e5 + 5;
const int maxm = 1.5e5+50;
const double eps = 1e-7;
const double inf = 0x3f3f3f3f;
const double lnf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const double pi=3.141592653589;
struct node
{
int x;
int y;
};
int dp[2005][2005];
int dir[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};
void bfs(int s)
{
queue<node>q;
q.push({1000,1000});
dp[1000][1000]=s;//初始位置等于s
while(!q.empty())
{
node temp=q.front();
q.pop();
if(dp[temp.x][temp.y]<4)
{
continue ;
}
int T=dp[temp.x][temp.y]/4;
dp[temp.x][temp.y]=dp[temp.x][temp.y]%4;
for(int i=0; i<4; i++)
{
int x=temp.x+dir[i][0];
int y=temp.y+dir[i][1];
int z=T;
q.push({x,y});
dp[x][y]+=T;//更新该点的蚂蚁的个数。
}
}
}
int main()
{
int n,t;
scanf("%d%d",&n,&t);
bfs(n);
for(int i=1; i<=t; i++)
{
int x,y;
scanf("%d %d",&x,&y);
if(x<=-1000||y<=-1000||x>=1000||y>=1000)
{
printf("0\n");
}
else
{
printf("%d\n",dp[1000+x][1000+y]);
}
}
}