题目链接
题目大意
题意是说给你矩形互为对顶角的两个点坐标,每次(-1,-1,-1,-1)则输出这个图形覆盖了多少1*1的正方形,重复覆盖只记一次。 以(-2,-2,-2,-2)结束输入。
解题思路
用hash把每个11的正方形映射到该正方形四个顶角的其中一个,也就意味着用一个点是否被标记来判断这个11的区域是否被覆盖, 这样就可以开一个二维数组来模拟平面。 另外再补充一点,按输入坐标的顺序来说的话,你要求x1 < x2 && y1 < y2其实还是同一个矩形·····比如7 5 8 10这个输入。
参考代码
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;
/** Constant List .. **/ //{
const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
int hash[1111][1111];
int main()
{
#ifdef DoubleQ
freopen("in.txt","r",stdin);
#endif
int x1 , x2 , y1 , y2 , flag;
while(~scanf("%d%d%d%d" , &x1 , &y1 , &x2 , &y2))
{
memset(hash , 0 , sizeof(hash));
flag = 0;
int cnt = 0;
while(x1 != -1 && y1 != -1 && x2 != -1 && y2 != -1)
{
if(x1 == -2 && y1 == -2 && x2 == -2 && y2 == -2)
{
flag = 1;
break;
}
if(x1 > x2)
{
int t = x1;
x1 = x2;
x2 = t;
}
if(y1 > y2)
{
int t = y1;
y1 = y2;
y2 = t;
}
for(int i = x1 ; i < x2 ; i ++)
{
for(int j = y1 ; j < y2 ; j ++)
{
hash[i][j] = 1;
}
}
scanf("%d%d%d%d" , &x1 , &y1 , &x2 , &y2);
}
for(int i = 0 ; i < 1110 ; i ++)
{
for(int j = 0 ; j < 1110 ; j ++)
{
if(hash[i][j])
cnt ++;
}
}
printf("%d\n",cnt);
if(flag)
break;
}
}
暂无评论