Calabash and Landlord

 

Problem Description
Calabash is the servant of a landlord. The landlord owns a piece of land, which can be regarded as an infinite 2D plane.

One day the landlord set up two orthogonal rectangular-shaped fences on his land. He asked Calabash a simple problem: how many nonempty connected components is my land divided into by these two fences, both finite and infinite? Calabash couldn't answer this simple question. Please help him! 

Recall that a connected component is a maximal set of points not occupied by the fences, and every two points in the set are reachable without crossing the fence.
 

 

Input
The first line of input consists of a single integer  T (1T10000), the number of test cases. 

Each test case contains two lines, specifying the two rectangles. Each line contains four integers x1,y1,x2,y2 (0x1,y1,x2,y2109,x1<x2,y1<y2), where (x1,y1),(x2,y2) are the Cartesian coordinates of two opposite vertices of the rectangular fence. The edges of the rectangles are parallel to the coordinate axes. The edges of the two rectangles may intersect, overlap, or even coincide.
 

 

Output
For each test case, print the answer as an integer in one line.
 

 

Sample Input
3 0 0 1 1 2 2 3 4 1 0 3 2 0 1 2 3 0 0 1 1 0 0 1 1
 

 

Sample Output
3 4 2
 
解题报告:这道题目一开始队友和我的思路全部跑歪了,直接开始根据数字(2-6) 分类讨论每个数字对应的情况,最后大模拟队友成功自闭,后来转化了一下思路,其实就是离散化后求解连通分量的数目,然后dfs/bfs跑一下就可以。
 
ac代码:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<queue>
 5 using namespace std;
 6 typedef long long ll;
 7 const int N=50;
 8 struct node{
 9     int x1,y1,x2,y2;
10 }a[3];
11 struct Node{
12     int x,y;
13     Node()
14     {
15     }
16     Node(int xx,int yy)
17     {
18         x=xx;
19         y=yy;
20     }
21 };
22 int b[N],c[N],cnt,ne[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
23 bool vis[N][N];
24 int getid(int x)
25 {
26     return (lower_bound(b,b+cnt,x)-b)*2+2;
27 }
28 void bfs(int x,int y)
29 {
30     vis[x][y]=1;
31     queue<Node>q;
32     q.push(Node(x,y));
33     while(!q.empty())
34     {
35         int xx=q.front().x;
36         int yy=q.front().y;
37         q.pop();
38         for(int i=0;i<4;i++)
39         {
40             int tx=xx+ne[i][0];
41             int ty=yy+ne[i][1];
42             if(tx<1||tx>=N||ty<1||ty>=N)
43                 continue;
44             if(vis[tx][ty])continue;
45             vis[tx][ty]=1;
46             q.push(Node(tx,ty));
47         }
48     }
49     return;
50 }
51 int main()
52 {
53     int T;
54     cin>>T;
55     while(T--)
56     {
57         scanf("%d%d%d%d",&a[0].x1,&a[0].y1,&a[0].x2,&a[0].y2);
58         scanf("%d%d%d%d",&a[1].x1,&a[1].y1,&a[1].x2,&a[1].y2);
59         cnt=0;
60         for(int i=0;i<N;i++)
61             for(int j=0;j<N;j++)
62                 vis[i][j]=0;
63         for(int i=0;i<2;i++)
64         {
65             b[cnt++]=a[i].x1;
66             b[cnt++]=a[i].y1;
67             b[cnt++]=a[i].x2;
68             b[cnt++]=a[i].y2;
69         }
70         sort(b,b+cnt);
71         for(int k=0;k<2;k++)
72         {
73             int a1=getid(a[k].x1);
74             int b1=getid(a[k].y1);
75             int a2=getid(a[k].x2);
76             int b2=getid(a[k].y2);
77             for(int i=a1;i<=a2;i++)
78                 vis[i][b1]=vis[i][b2]=1;
79             for(int i=b1;i<=b2;i++)
80                 vis[a1][i]=vis[a2][i]=1;
81         }
82         /*for(int i=1;i<15;i++)
83         {
84             for(int j=1;j<15;j++)
85                 printf("%d ",vis[i][j]);
86             printf("\n");
87         }*/
88         int ans=0;
89         for(int i=1;i<N;i++)
90             for(int j=1;j<N;j++)
91                 if(!vis[i][j])
92                 {
93                     ans++;
94                     bfs(i,j);
95                 }
96         printf("%d\n",ans);
97     }
98     return 0;
99 }

 

转载于:https://www.cnblogs.com/Spring-Onion/p/11354805.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值