题目描述:链接:https://www.nowcoder.com/questionTerminal/1183548cd48446b38da501e58d5944eb
来源:牛客网
二货小易有一个W*H的网格盒子,网格的行编号为0~H-1,网格的列编号为0~W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。对于两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根
小易想知道最多可以放多少块蛋糕在网格盒子里。
解题思路:每4行4列为1个周期pattern相同如下的图案:
** ** **
** ** **
** ** **
** ** **
** ** **
** ** **
只要数清楚,每个4行中,
i或i+1行的蛋糕数为(n/4)*2+(n%4<2?n%4:2)
i+2或i+3行的蛋糕数为((n-2)/4)*2+((n-2)%4<2?(n-2)%4:2)
然后算清楚一共有多少个完整的4行就可以,当然是共有m/4个。唯一要注意的就是处理行数除4有余数的情况,把最后可能剩余的1~3行加进去即可。
代码示例:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt(), n = sc.nextInt();
int evenICount = (n / 4) * 2 + (n % 4 < 2 ? n % 4 : 2);
int oddICount = ((n - 2) / 4) * 2 + ((n - 2) % 4 < 2 ? (n - 2) % 4 : 2);
int ans = m / 4 * (evenICount + oddICount) * 2;
if(m % 4 > 0) ans += evenICount;
if(m % 4 > 1) ans += evenICount;
if(m % 4 > 2) ans += oddICount;
System.out.println(ans);
}
}
public class NoTwo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int w=sc.nextInt();
int h=sc.nextInt();
int[][]arr=new int[h][w];
System.out.println(notwo(w,h));
}
private static int notwo(int w, int h) {
if((w%4==0&&h%4!=0)||((w%4!=0)&&(h%4==0))){
return w*h/2;
}
if((w%2==0)&&(h%2==0)){
return w*h/2+2;
}
return w*h/2+1;
}
}