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.

思路:

如果在一个地图上种树让你判断是否成环不太容易,但是,若把所有的树种好,依次去伐树,那么问题就简单不少,这个题目中,与边界相邻的没树的地方不算在绿化区,因此可以增加最外围一层巧妙地标记所有的这种区域。种好所有树后就可以依次砍树,如果旁边有边界区,就去bfs。

 
 
#include<stdio.h>
#include<string.h>
#include<string>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
 
const int maxn = 4e6+7;
 
bool mp[2001][2001];
bool mk[2001][2001];
int sz;

int Next[4][2] = {1,0,-1,0,0,1,0,-1};

void bfs(int x,int y)
{
	queue <pair<int,int> > q;
	q.push(make_pair(x,y));
	mk[x][y] = 1;
	while(q.size())
	{
		pair <int,int> p = q.front();
		q.pop();
		x = p.first,y=p.second;
		for(int i = 0; i < 4; i++)
		{
			int x_ = x+Next[i][0];
			int y_ = y+Next[i][1];
			if(x_ < 0 || y_ < 0 || x_ >= 2000 || y_ >= 2000)
            {
                continue;
            }
            if(mp[x_][y_] || mk[x_][y_]) continue;
            mk[x_][y_] = 1;
            sz--;
            q.push(make_pair(x_,y_));
		}
	}
}

int qx[100005],qy[100005];
int ans[100005];
int main()
{
    int n;
    scanf("%d",&n);
    int m = n;
    int i = 0;
    while(m--)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        i++;
        x+=1000;
        y+=1000;
        qx[i] = x;
        qy[i] = y;
        mp[x][y] = 1;
    }
    sz = 2000*2000 - n-1;
    mk[0][0] = 1;
    bfs(0,0);
    
    ans[n] = sz;
    for(int i = n; i >= 1; i--)
    {
        int x = qx[i];
        int y = qy[i];
        sz++;
        int yes = 0;
        for(int k = 0; k < 4; k++)
        {
            int x_ = x+Next[k][0];
            int y_ = y+Next[k][1];
            if(mp[x_][y_]) continue;
            if(mk[x_][y_]) yes = 1; 
        }
        if(yes) sz--,bfs(x,y);
        ans[i-1] = sz; 
        mp[x][y] = 0;
    }
     
    for(int i = 1; i <= n; i++)
    {
        printf("%d\n",ans[i]);
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/sunmoonvocano/article/details/80145099