hdu1866 A + B forever!(面积并)题解

A + B forever!

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1295    Accepted Submission(s): 326


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

思路:

一开始想到用一个二维数组当做坐标轴平面,然后一个一个方块去标记,最后数出来有多少方块。但是输入很坑,看着很麻烦。面积填涂也很坑,不能直接把x1,x2,y1,y2填上,因为数组中(x1,y1)是一个方块而坐标轴中(x1,y1)是一个点。

Code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<cctype>
#include<queue>
#include<math.h>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define N 1005
using namespace std;
int map[N][N];
int num(char x){
	if(x>='0' && x<='9') return 1;
	return 0;
}
int main(){
	int len,x[4],count,sum;
	char s[510];
	while(gets(s)){
		memset(map,0,sizeof(map));
		len=strlen(s);
		count=0;
		sum=0;
		for(int i=0;i<len;i++){
			if(num(s[i])==1){
				x[count]=s[i]-'0';
				i++;
				while(num(s[i])==1){
					x[count]=x[count]*10+(s[i]-'0');
					i++;
				}
				count++;
			}
			if(count==4){
				int x1=min(x[0],x[2]);    //保证后面遍历大小不会错
				int x2=max(x[0],x[2]);
				int y1=min(x[1],x[3]);
				int y2=max(x[1],x[3]);
				for(int j=x1;j<x2;j++){    //这里要注意后面的<而不是<=,理由就是上面说到的
					for(int k=y1;k<y2;k++){
						map[j][k]=1;
					}
				}
				count=0;
			}
		}
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				if(map[i][j]==1) sum++;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}


posted @ 2018-02-27 18:28  KirinSB  阅读(231)  评论(0编辑  收藏  举报