Fresh Air (Reverse thinking +bfs) __ Search

Source: Internet
Author: User
Link: Https://www.nowcoder.com/acm/contest/106/L
Source: Niu Ke Net

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


And you know that, trees have the special ability to refresh the air. If a area, which are surrounded by trees, are separated from the outer atmosphere, and can call it "the Supercalifragilisti Cexpialidocious area ". Students can enjoy the healthiest air there. Then, you are happened to know that HUST'll plant N trees in a bare plain and so you want to calculate the total size of the S Upercalifragilisticexpialidocious area "over each operation.
We describe the position of trees with a coordinate. (X i,y i). For example, over 9 trees were planted, the green area are a supercalifragilisticexpialidocious area, the IT size is 3.

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

The ' The ' is ' an integer N as described above. Then following N lines, each line contains two integers x i and Y I, indicating a new tree are planted at (X i,y i). Output Description:
Output N lines, each line an integer indicating the total size of supercalifragilisticexpialidocious areas after each opera tion.

Ideas:

If you're planting a tree on a map, it's not easy to decide if it's a loop. However, if all the trees are good, in turn to cut down the tree, then the problem is very simple, this topic, with the border adjacent to the tree is not counted in the green belt, so you can increase the outermost layer of subtly marking all of this area. After planting all the trees, you can cut down the trees in turn, if there is a border area next 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_ >= | | Y_ >=) {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; }



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.