Fresh Air (reverse thinking + bfs)

Link: https://www.nowcoder.com/acm/contest/106/L
Source: Niuke.com

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.

Enter description:

 
 
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 description:

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

Ideas:

If you plant trees on a map, it is not easy for you to judge whether it forms a ring. However, if you plant all the trees well and cut down the trees one by one, then the problem is much simpler. Places do not count as green areas, so an outermost layer can be added to subtly mark all such areas. After planting all the trees, you can cut down the trees one by one. If there is a border area next to it, go to 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 years[100005];
intmain()
{
    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;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325806730&siteId=291194637
Recommended