HDU 1264(Counting Squares)

Calculating the area of ​​a rectangular footprint, and each region can only be calculated once. Represented with a two-dimensional array in each region covered, uncovered represents 0, 1 are covered. Finally, through the entire two-dimensional array, calculating the number of regions can be covered.

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 105;

int mp[MAXN][MAXN]; //整个区域

int main()
{
	memset(mp, 0, sizeof(mp));
	int x1, y1, x2, y2; //对角的坐标
	while (cin >> x1 >> y1 >> x2 >> y2)
	{
		if (x1 == -1)
		{
			int sum = 0; //被覆盖区域的个数
			for (int i = 0; i < MAXN; i++)
			{
				for (int j = 0; j < MAXN; j++)
					if (mp[i][j] == 1)
						++sum;
			}
			cout << sum << endl;
			memset(mp, 0, sizeof(mp));
		}
		if (x1 == -2)
		{
			int sum = 0; //被覆盖区域的个数
			for (int i = 0; i < MAXN; i++)
			{
				for (int j = 0; j < MAXN; j++)
					if (mp[i][j] == 1)
						++sum;
			}
			cout << sum << endl;
			break;
		}	
		for (int i = min(x1, x2); i < max(x1, x2); i++)
		{
			for (int j = min(y1, y2); j < max(y1, y2); j++)
				mp[i][j] = 1;
		}
	}
	return 0;
}

Keep up.

Published 206 original articles · won praise 1 · views 8999

Guess you like

Origin blog.csdn.net/Intelligence1028/article/details/104790021