Uvalive 6663 count the regions (discretization, dyeing, DFS)

Source: Internet
Author: User

Https://icpcarchive.ecs.baylor.edu/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 4675


Question:

A number of rectangles are given in a two-dimensional plane. The plane is divided into several areas by the side of the rectangle, and the total number of areas is obtained.

Analysis:

Since there are only 50 rectangles, the plane after discretization is about 100*100. The area covered by each rectangle may be stained (with binary bit pressure). The connected block of the same color is a region, as long as the DFS finds the number of connected blocks. It is worth noting that the original plane needs to be stained, instead of adding 1 directly to the final number (the outer plane is infinite), so that the hollow area in the middle cannot be calculated.

Another problem is the open and closed interval. After the data is read during the competition, convert the data to X2 --; Y2 --; when the dyeing is performed, I <= x2; j <= Y2; get wa; then remove X2 --; Y2 --; after dyeing, it is changed to I <X2; j <Y2. It may be due to discretization, but I still haven't found any counterexamples. Please let me know if you know the reason.


#include <bits/stdc++.h>#define maxn 233#define INF 0x3f3f3f3fusing namespace std;struct _rectangle{  int x1,y1,x2,y2;}rec[64];long long G[maxn][maxn];int dx[]={0,0,-1,1};int dy[]={1,-1,0,0};int x[maxn],y[maxn];int XN,YN;inline boolin_range(int x,int y){  return 0<=x && x<XN && 0<=y && y<YN;}voiddfs(int x,int y,long long status){  G[x][y]=0;  for (int i=0;i<4;i++)  {    int tx=x+dx[i],ty=y+dy[i];    if (in_range(tx,ty) && G[tx][ty]==status) dfs(tx,ty,status);  }}intmain(){#ifdef FCBRUCE  freopen("/home/fcbruce/code/t","r",stdin);#endif // FCBRUCE  int n;  while (scanf("%d",&n),n)  {    int cnt=0;    int minx=INF,miny=INF,maxx=-1,maxy=-1;    for (int i=0,x1,y1,x2,y2;i<n;i++)    {      scanf("%d%d%d%d",&x1,&y2,&x2,&y1);            minx=min(minx,min(x1,x2));      maxx=max(maxx,max(x1,x2));      miny=min(miny,min(y1,y2));      maxy=max(maxy,max(y1,y2));            rec[i]=(_rectangle){x1,y1,x2,y2};      x[cnt]=x1;y[cnt]=y1;      cnt++;      x[cnt]=x2;y[cnt]=y2;      cnt++;    }        minx--;miny--;    maxx++;maxy++;    rec[n++]=(_rectangle){minx,miny,maxx,maxy};    x[cnt]=minx;y[cnt]=miny;    cnt++;    x[cnt]=maxx;y[cnt]=maxy;    cnt++;    sort(x,x+cnt);sort(y,y+cnt);    XN=unique(x,x+cnt)-x;    YN=unique(y,y+cnt)-y;    memset(G,0,sizeof G);    for (int k=0,x1,y1,x2,y2;k<n;k++)    {      x1=lower_bound(x,x+XN,rec[k].x1)-x;      y1=lower_bound(y,y+YN,rec[k].y1)-y;      x2=lower_bound(x,x+XN,rec[k].x2)-x;      y2=lower_bound(y,y+YN,rec[k].y2)-y;      for (int i=x1;i<x2;i++)      {        for (int j=y1;j<y2;j++)        {          G[i][j]|=(1ll<<k);        }      }    }    int _cnt=0;    for (int i=0;i<XN;i++)    {      for (int j=0;j<YN;j++)      {        if (G[i][j]!=0ll)        {          dfs(i,j,G[i][j]);          _cnt++;        }      }    }    printf("%d\n",_cnt);  }  return 0;}


Uvalive 6663 count the regions (discretization, dyeing, DFS)

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.