链接:https://www.nowcoder.com/questionTerminal/1183548cd48446b38da501e58d5944eb?toCommentId=5410037
来源:牛客网
二货小易有一个W*H的网格盒子,网格的行编号为0H-1,网格的列编号为0W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。
对于两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根
小易想知道最多可以放多少块蛋糕在网格盒子里。
import java.util.*;
public class Main{
static class Cake {
int x;
int y;
boolean isFlag;
public Cake(int x, int y, boolean isFlag) {
this.x = x;
this.y = y;
this.isFlag = isFlag;
}
public void setFlag(boolean flag) {
isFlag = flag;
}
}
static int count = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int w = scanner.nextInt();
int h = scanner.nextInt();
count = w*h;
List<List<Cake>> lists = new ArrayList<>();
for (int i = 0; i < h; i++) {
List<Cake> list = new ArrayList<>();
for (int j = 0; j < w; j++) {
list.add(new Cake(i,j,true));
}
list.add(null);
list.add(null);
lists.add(list);
}
lists.add(null);
lists.add(null);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
Cake curCake = lists.get(i).get(j);
if (!curCake.isFlag ){
continue;
}
Cake rightCake = lists.get(i).get(j + 2);
if (rightCake != null && rightCake.isFlag){
rightCake.setFlag(false);
count--;
}
List listLevel = lists.get(i + 2);
if (listLevel != null ){
Cake topCake = lists.get(i + 2).get(j);
if (topCake.isFlag){
topCake.setFlag(false);
count--;
}
}
}
}
System.out.println(count);
}
}