Topic
Original title Link: Https://www.nowcoder.com/acm/contest/106/L
In 100000 * 10000 of the open space, there are n time points, each time point will be on (xi,yi) planted a tree.
Define green: The number of open spaces surrounded by trees.
Ask how much green is after each point in time.
Ideas
Inverse solution, from the (0,0) position of the outside of the full mark (to the open space plus a circle), in the respective consideration of the current tree, is in the circle or the periphery of the circle.
Since the VIS is a global array, then the BFS is very fast and each point is traversed only once.
Code implementation
1#include <stdio.h>2#include <queue>3#include <iostream>4 using namespacestd;5 6typedef pair<int,int>P;7 Const intMAXN =100000+Ten;//maximum number of points8 Const intSIZE = -+Ten;//Map Size9 Const intoffset = +;//OffsetTen Const intDx[] = {-1,0,1,0 }; One Const intDy[] = {0,1,0,-1 }; A intN,XI[MAXN],YI[MAXN],ANS[MAXN]; - BOOLVis[size][size], maze[size][size]; - the intBFsintXinty) - { - intRET =0; -Queue<p>Q; +Vis[x][y] =true; - Q.push (P (x, y)); + while(!q.empty ()) A { atP p =Q.front (); Q.pop (); -ret++; - for(inti =0; I <4; i++) - { - intNX = P.first + dx[i], NY = P.second +Dy[i]; - if(NX >=0&& NX < SIZE && NY >=0&& NY < SIZE && (!Vis[nx][ny])) in { -Vis[nx][ny] =true; to Q.push (P (NX, NY)); + } - } the } * returnret; $ }Panax Notoginseng - intMain () the { +scanf"%d", &n); A for(inti =1; I <= n;i++) the { +scanf"%d%d", &xi[i], &yi[i]); -Maze[xi[i] + offset][yi[i] + offset] =1; $Vis[xi[i] + offset][yi[i] + offset] =1; $ } - -BFs0,0); the intres =0; - for(inti =0; i < SIZE; i++)Wuyi for(intj =0; J < SIZE; J + +) the if(Vis[i][j] = =false) res++; -Ans[n] =Res; Wu - for(inti = n; I >=4; i--)//It's impossible to surround a vacant space within a tree . About { $ intU = xi[i] +offset; - intv = yi[i] +offset; -MAZE[U][V] =0; - intCNT =0; A for(intj =0; J <4; J + +) + { the intNu = U + dx[j], NV = v +Dy[j]; - if(MAZE[NU][NV] = =0&& VIS[NU][NV] = =1) cnt++; $ } the if(CNT = =0)//cnt = = 0, indicating that the point is in the internal the { theVIS[U][V] =0; theres++; - } in ElseRes-= (BFS (u, v)-1); theAns[i-1] =Res; the } About for(inti =1; I <= N; i++) theprintf"%d\n", Ans[i]); the return 0; the}
Reference Link: https://www.nowcoder.com/acm/contest/view-submission?submissionId=26038731
Fire Air (Hua Ke School Tournament online)