2018 华中科技大学校赛 L Fresh Air 思维BFS

链接:https://www.nowcoder.com/acm/contest/106/L
来源:牛客网

It’s universally acknowledged that there’re innumerable trees in the campus of HUST.


And you know that, trees have the special ability to refresh the air. If an area, which is surrounded by trees, is separated from the outer atmosphere, you can call it “the supercalifragilisticexpialidocious area”. Students can enjoy the healthiest air there. Then, you happened to know that HUST will plant N trees in a bare plain, so you want to calculate the total size of “the supercalifragilisticexpialidocious area” after each operation.

We describe the position of trees with a coordinate.(X i,Y i).
For example, after 9 trees were planted, the green area is a supercalifragilisticexpialidocious area, its size is 3.


After planting a new tree in (3,2), its size is 2.

输入描述:

 
The first line is an integer N as described above.
Then following N lines, each line contains two integer X i and Y i, indicating a new tree is planted at (X i,Y i) .

输出描述:Output N lines, each line a integer indicating the total size of supercalifragilisticexpialidocious areas after each operation.示例1输入90 11 02 02 11 23 22 3100 1001 1输出000011221

倒着BFS,先把树种好,然后每拔一棵树,判断封闭是否被打破。

#include<bits/stdc++.h>
#define maxn 100010
#define ll long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
int M[2010][2010];
 
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int ans[maxn];
int Ans;
struct node
{
    int x,y;
};
 
void bfs(int x,int y)
{
    node nod;
    nod.x=x;
    nod.y=y;
    //cout<<nod.x<<" "<<nod.y<<endl;
    M[nod.x][nod.y]=2;
    queue<node> q;
    q.push(nod);
    Ans--;
    while(q.size())
    {
        nod=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            node nnod;
            nnod.x=nod.x+dx[i];
            nnod.y=nod.y+dy[i];
            if(nnod.x>=0&&nnod.x<=2000&&nnod.y>=0&&nnod.y<=2000&&M[nnod.x][nnod.y]==0)
            {
                Ans--;
                M[nnod.x][nnod.y]=2;
                q.push(nnod);
            }
        }
    }
}
int main()
{
    int x[maxn],y[maxn];
    //memset(M,0,sizeof(M));
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>x[i]>>y[i];
        x[i]+=1000;
        y[i]+=1000;
        M[x[i]][y[i]]=1;
    }
    Ans=2001*2001-n;
    bfs(0,0);
    for(int i=n-1;i>=0;i--)
    {
        ans[i]=Ans++;
        M[x[i]][y[i]]=0;
        for(int j=0;j<4;j++)
        {
            int nx=x[i]+dx[j];
            int ny=y[i]+dy[j];
            if(nx>=0&&nx<=2000&&ny>=0&&ny<=2000&&M[nx][ny]==2)
            {
                bfs(x[i],y[i]);
                break;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        cout<<ans[i]<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值