Counting Squares

来源:互联网 发布:java 算术运算符 编辑:程序博客网 时间:2024/05/21 06:42

Counting Squares

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size:

Problem Description

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.

Input

The 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.

Output

Your 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 106 9 7 86 8 8 11-1 -1 -1 -10 0 100 10050 75 12 9039 42 57 73-2 -2 -2 -2

Sample Output

810000
#include<stdio.h>#include<string.h>#include<stdlib.h>#define N 105int ax,bx,ay,by,a;bool xy[N][N];int i,j,cover;int main(){    while(~scanf("%d%d%d%d",&ax,&ay,&bx,&by))    {        if(ax==ay&&ax==bx&&ax==by&&(ax==-1||ax==-2))        {            for(i=0,cover=0;i<N;i++) for(j=0;j<N;j++)                cover+=xy[i][j];            printf("%d\n",cover);            if(ax==-2) break;            memset(xy,0,sizeof(xy));        }        else        {            if(ax>bx) {a=ax;ax=bx;bx=a;}            if(ay>by) {a=ay;ay=by;by=a;}            for(i=ax;i<bx;i++) for(j=ay;j<by;j++) xy[i][j]=1;        }    }    system("pause");    return 0;}

原创粉丝点击