D - Counting Squares
Posted jishuren
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D - Counting Squares相关的知识,希望对你有一定的参考价值。
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.
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 ",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的主要内容,如果未能解决你的问题,请参考以下文章
HDU 1264 Counting Squares(线段树求面积的并)