题目
思路
暴力搜可还行,建议降红黄
搜的时候要考虑是否有必要扩展以优化
code:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
inline int read()
{
int ret,c,f=1;
while (((c=getchar())> '9'||c< '0')&&c!='-');
if (c=='-') f=-1,ret=0;
else ret=c-'0';
while ((c=getchar())>='0'&&c<='9') ret=(ret<<1)+(ret<<3)+(c^48);
return ret*f;
}
int n,ox,oy,m,a[4000][4000],ans,q[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
struct f{
int x,y;
bool type;
} p;
queue<f> qq;
void dfs(int x,int y,bool type)
{
p.x=x,p.y=y,p.type=type;
qq.push(p);
while (qq.size())
{
x=qq.front().x,y=qq.front().y,type=qq.front().type;
qq.pop();
int u=0,ux,uy;
for (int j=0;j<4;j++)
{
int dx=x+q[j][0],dy=y+q[j][1];
if (a[dx][dy]) u++;
else ux=dx,uy=dy;
}
if (u==3)
{
ans++;
a[ux][uy]=2;
p.x=ux,p.y=uy,p.type=1;
qq.push(p);
}
if (type)
{
for (int j=0;j<4;j++)
{
int dx=x+q[j][0],dy=y+q[j][1];
if (u==3&&dx==ux&&dy==uy) continue;
if (a[dx][dy]) p.x=dx,p.y=dy,p.type=0,qq.push(p);
}
}
}
return;
}
int main()
{
n=read();
for (int i=1;i<=n;i++)
{
int x=read()+1100,y=read()+1100;
ox=x,oy=y;
if (a[x][y]) a[x][y]=1,ans--;
else a[x][y]=1,dfs(x,y,1);
printf("%d\n",ans);
}
return 0;
}