码迷,mamicode.com
首页 > 其他好文 > 详细

D - Counting Squares

时间:2020-01-31 19:08:57      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:rap   points   div   rect   otherwise   opp   code   content   integer   

Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in the range 0 to 100. For example, the line 
5 8 7 10 
specifies the rectangle who‘s corners are(5,8),(7,8),(7,10),(5,10). 
If drawn on graph paper, that rectangle would cover four squares. Your job is to count the number of unit(i.e.,1*1) squares that are covered by any one of the rectangles given as input. Any square covered by more than one rectangle should only be counted once. 

InputThe input format is a series of lines, each containing 4 integers. Four -1‘s are used to separate problems, and four -2‘s are used to end the last problem. Otherwise, the numbers are the x-ycoordinates of two points that are opposite corners of a rectangle. 
OutputYour output should be the number of squares covered by each set of rectangles. Each number should be printed on a separate line. 
Sample Input

5 8 7 10
6 9 7 8
6 8 8 11
-1 -1 -1 -1
0 0 100 100
50 75 12 90
39 42 57 73
-2 -2 -2 -2

Sample Output

8
10000

数据太小了,直接暴力

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 bool mp[105][105];
 8 
 9 int main()
10 {
11     int a,b,c,d;
12     memset(mp,0,sizeof(mp));
13     while(scanf("%d %d %d %d",&a,&b,&c,&d)!=EOF)
14     {
15         // 每一组结束,输出并更新
16         if(a<0 || b<0 || c<0 || d<0)
17         {
18             int ans=0;
19             for(int i=0;i<=100;++i)
20             {
21                 for(int j=0;j<=100;++j)
22                 {
23                     if(mp[i][j])
24                     {
25                         ++ans;
26                     }
27                 }
28             }
29             printf("%d\n",ans);
30             memset(mp,0,sizeof(mp));
31             continue;
32         }
33         // 暴力覆盖
34         if(a>c)
35         {
36             swap(a,c);
37         }
38         if(b>d)
39         {
40             swap(b,d);
41         }
42         for(int i=a;i<c;++i)
43         {
44             for(int j=b;j<d;++j)
45             {
46                 mp[i][j]=true;
47             }
48         }
49     }
50 
51     return 0;
52 }

 

D - Counting Squares

标签:rap   points   div   rect   otherwise   opp   code   content   integer   

原文地址:https://www.cnblogs.com/jishuren/p/12246027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!