二货小易有一个W*H的网格盒子,网格的行编号为0~H-1,网格的列编号为0~W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。
对于两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根
小易想知道最多可以放多少块蛋糕在网格盒子里。
输入描述:
每组数组包含网格长宽W,H,用空格分割.(1 ≤ W、H ≤ 1000)
输出描述:
输出一个最多可以放的蛋糕数
输入例子:
3 2
输出例子:
4
这个题可以先考虑一维的情况,可以发现是以4为周期,4个位置连续放2个,空2个,多出来的几个特殊处理。
推广的二维,可以发现是以4X4的方格为循环的,先从左上角开始,找出有多少个4x4的矩形,
然后再分别计算下方和右方的矩形,最后再加上右下角剩余的那个小矩形即可。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
int main()
{
int w,h;
scanf("%d %d",&w,&h);
int sum=(w/4)*(h/4)*4*2;
sum+=(w%4)*2*(h/4);
sum+=(h%4)*2*(w/4);
int tmp=(w%4)*(h%4);
if(tmp<=4){
sum+=tmp;
}else if(tmp==6){
sum+=4;
}else if(tmp==9){
sum+=5;
}
printf("%d\n",sum);
return 0;
}