第十四届华中科技大学程序设计竞赛-L—Fresh Air,bfs拓展,倒着bfs

题目描述

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 theair. If an area, which is surrounded by trees, is separated from the outeratmosphere, you can call it “the supercalifragilisticexpialidocious area”. Students can enjoy the healthiest air there. Then, you happened toknow that HUST will plant N trees in a bare plain, so you want to calculate thetotal size of “thesupercalifragilisticexpialidocious area” after eachoperation.

 

We describe the position of trees with a coordinate.(Xi,Yi).

 

For example, after 9 trees were planted, the green area is asupercalifragilisticexpialidocious area, its size is 3.

 

 

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

输入描述:

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

输出描述:

Output N lines, each line a integer indicating the total size of supercalifragilisticexpialidocious areas after each operation.

示例1

输入

9
0 1
1 0
2 0
2 1
1 2
3 2
2 3
100 100
1 1

输出

0
0
0
0
1
1
2
2
1

这题就是倒着dfs就ok了,贴下代码吧:

说明以下,这份代码不是我敲的,是赛后官方写的,我只是解释一下,也为我以后回看时用。

 

 
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

const int maxn=2e3+10;
const int maxm=1e5+10;

struct coord{int x,y;}tmp,nxt;
queue<coord>q;

int T,m,res;
int x[maxm],y[maxm],ans[maxn];
int state[maxn][maxn];
int dir[4][2]={{0,-1},{0,1},{-1,0},{1,0}};

void bfs(int x,int y)
{
    state[tmp.x=x][tmp.y=y]=2; ///将搜过空地0表示为空地2
    res--;///因为此时的坐标能走到,所以这个方格不可能被围,故要减1
    q.push(tmp);

    while(!q.empty())
    {
        tmp=q.front(),q.pop();
        for(int i=0;i<4;i++)
        {
            nxt.x=tmp.x+dir[i][0];
            nxt.y=tmp.y+dir[i][1];
            if(nxt.x<0||nxt.x>2000||nxt.y<0||nxt.y>2000)continue;
            if(state[nxt.x][nxt.y]!=0)continue;///碰到tree或者已经走过的小方格(空格2),不操作
            state[nxt.x][nxt.y]=2;
            res--,q.push(nxt);
        }
    }
}

bool check(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int xx=x+dir[i][0],yy=y+dir[i][1];
        if(xx<0||xx>2000||yy<0||yy>2000)continue;
        if(state[xx][yy]==2)return 1;
    }
    return 0;
}

int main()
{
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x[i],&y[i]);
        x[i]+=1000,y[i]+=1000;
        state[x[i]][y[i]]=1;
    }
                    ///res表示被围了有多少个方格(空地0)
    res=2001*2001-m;///整个矩阵有2001*2001个方格,先减去围着方格的tree的总数量
                    ///搜完的res就是整个矩阵中被围成的小方格数(也就是空地0的总数)
                    
    bfs(0,0); ///这里为什么从(0,0)开始呢?看我后面的解释
    
    for(int i=m;i;i--)///从后往前
    {
        ans[i]=res++; ///将res赋给靠后的
        state[x[i]][y[i]]=0;///将此tree的坐标变成空地0

        if(check(x[i],y[i]))///这里res++,有点意思,首先先查询此时的坐标四方向是否有空地2,若都没有空地2,因为此时的tree坐标已经变成空地了
            bfs(x[i],y[i]);///那也就是说此时的空地0被围住了,故要res++,不bfs;若有空地2,说名此时的空地0没有被围住,故要dfs,但res好像已经加了,
    }                       ///没关系,进去的时候会减去的,接着的最后跟上面类似,你懂的。这样我们就算出来了题目要求的答案

    for(int i=1;i<=m;i++)
        printf("%d\n",ans[i]);

    return 0;
}

 

 

这里解释一下为什么从(0,0)开始,我们知道坐标范围是,坐标在输入时处理了一下,都加了1000,

 

 

那么处理后的坐标范围就是(1<Xi,Yi<2000),我们要想在图中都把没被围这空地0走一遍,不能在坐标范围(1<Xi,Yi<2000)中随便找一个坐标就走,不实际,为什么?因为你选的坐标可能走着走着就不能继续走了,但还有空地0(没被围的)没去走并赋值为空地2。这时那我们该怎么解决这个问题呢?很简单,将坐标范围(1<Xi,Yi<2000)的圈外多加单位为1的圈,围住坐标范围(1<Xi,Yi<2000),这时我们可以在多加的一圈内随便找一个点去搜(哪个坐标顺眼就选哪个),这样就能保证能从范围(1<Xi,Yi<2000)圈外的任意个位置进去搜,嗯呢,搞定。啊,我这么热心肠,还是画个图吧!

 

我的标签:做个有情怀的程序员。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值