HDU 1866 A + B forever!

Problem Description

As always, A + B is the necessary problem of this warming-up contest. But the patterns and contents are different from the previous ones. Now I come up with a new “A + B” problem for you, the top coders of HDU.
As we say, the addition defined between two rectangles is the sum of their area . And you just have to tell me the ultimate area if there are a few rectangles.
Isn’t it a piece of cake for you? Come on! Capture the bright “accepted” for yourself.

Input

There come a lot of cases. In each case, there is only a string in one line. There are four integers, such as “(x1,y1,x2,y2)”, describing the coordinates of the rectangle, with two brackets distinguishing other rectangle(s) from the string. There lies a plus symbol between every two rectangles. Blanks separating the integers and the interpunctions are added into the strings arbitrarily. The length of the string doesn’t exceed 500.
0<=x1,x2<=1000,0<=y1,y2<=1000.

Output

For each case, you just need to print the area for this “A+B” problem. The results will not exceed the limit of the 32-signed integer.

Sample Input

(1,1,2,2)+(3,3,4,4)
(1,1,3,3)+(2,2,4,4)+(5,5,6,6)

Sample Output

2

8

简单的求面积并而已。注意读入方式。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<cstdlib>
#include<functional>
#include<string>
using namespace std;
const int maxn = 1005; 
int a[4], m[maxn][maxn], ans, cnt;
char s[maxn >> 1];

void begin()
{
	memset(m, 0, sizeof(m));
	ans = cnt = 0;
}

int main()
{
	while (gets(s))
	{
		begin();
		for (int i = 0; s[i]; i++)
			if (s[i] <= '9'&&s[i] >= '0')
			{
				while (s[i] >= '0'&&s[i] <= '9') a[cnt] = a[cnt] * 10 + s[i++] - '0';
				cnt++;
				if (cnt == 4)
				{
					cnt = 0;
					for (int i = min(a[0], a[2]); i < max(a[0], a[2]); i++)
					for (int j = min(a[1], a[3]); j < max(a[1], a[3]); j++)
					if (!m[i][j]){ m[i][j] = 1; ans++; }
					memset(a, 0, sizeof(a));
				}
			}
		printf("%d\n", ans);
	}
}


你可能感兴趣的:(HDU)