Counting squares
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 405 accepted submission (s): 212
Problem descriptionyour 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 wocould 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 shoshould 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 shocould be the number of squares covered by each set of rectangles. Each number shocould be printed on a separate line.
Sample Input
5 8 7 106 9 7 86 8 8 11-1-1-10 0 100 10050 75 12 9039 42 57 73-2-2-2-2
Code :
# Include <stdio. h> # include <stdlib. h> # include <math. h> # include <string. h> # include <time. h> # include <conio. h> // calculate the area of the image. Since it is stored in the hash topic, I want to know how to use the hash method. // each graphic unit is regarded as a hash object, use an array to simulate a two-dimensional space int hash [105] [105]; int main () {int X1 = 1, Y1, X2, Y2, CNT = 0, flag; while (scanf ("% d", & X1, & Y1, & X2, & Y2) {flag = CNT = 0; memset (hash, 0, sizeof (hash); While (x1! =-1 & Y1! =-1 & X2! =-1 & Y2! =-1) {If (x1 =-2 & Y1 =-2 & X2 =-2 & y2 =-2) {flag = 1; break;} If (x1> x2) x1 ^ = x2 ^ = x1 ^ = x2; If (Y1> Y2) Y1 ^ = Y2 ^ = Y1 ^ = Y2; for (INT I = x1; I <X2; ++ I) {for (Int J = Y1; j <Y2; ++ J) hash [I] [J] = 1;} scanf ("% d", & X1, & Y1, & X2, & Y2 );} for (INT I = 0; I <100; ++ I) {for (Int J = 0; j <100; ++ J) if (hash [I] [J]) CNT ++;} printf ("% d \ n", CNT); If (FLAG) break;} return 0 ;}
at first glance, this is a geometric question. However, since it is added to the hash question column, it is of course considered that hash is used to solve this question, the data range given in the question is not large, so we can use a two-dimensional array to simulate a two-dimensional plane, and take a point of four corners to hash the state of the point.