格子放鸡蛋

X星球的母鸡很聪明。它们把蛋直接下在一个 N * N 的格子中,每个格子只能容纳一枚鸡蛋。它们有个习惯,要求:每行,每列,以及每个斜线上都不能有超过2个鸡蛋。如果要满足这些要求,母鸡最多能下多少蛋呢,有多少种摆放方法呢?

    下面的程序解决了这个问题,请仔细分析程序逻辑,推断划线处缺少的代码。

public class A
{
static int max = 0;
static int T = 0;
static final int N = 6; // 只能在(r,c) 以及其右,其下放置
static void f(int[][] da, int r, int c)
{
if(r>=N){
int n = count(da);
if(n>max) {
max = n;
T = 0;
}
if(n==max) T++;
return;
} //计算一下步放哪
int r_next = r;
int c_next = c + 1;
if(c_next>=N){
c_next = 0;
r_next++;
} if(____________________){ // 填空位置
da[r][c] = 1;
f(da, r_next, c_next);
} da[r][c] = 0;
f(da, r_next, c_next);
} static int count(int[][] da)
{
int n = 0; for(int i=0; i<da.length; i++)
for(int j=0; j<da[i].length; j++)
if(da[i][j]==1) n++; return n;
} static int spy(int[][] da, int r, int c)
{
int m=0; // 该行
int n=0;
for(int i=0; i<N; i++) if(da[r][i]==1) n++;
if(n>m) m = n; //该列
n=0;
for(int i=0; i<N; i++) if(da[i][c]==1) n++;
if(n>m) m = n; //右斜线
n=0;
for(int i=0; i<N; i++){
if(r-i<0 || c-i<0) break;
if(da[r-i][c-i]==1) n++;
}
for(int i=1; i<N; i++){
if(r+i>=N || c+i>=N) break;
if(da[r+i][c+i]==1) n++;
}
if(n>m) m = n; //左斜线
n=0;
for(int i=0; i<N; i++){
if(r-i<0 || c+i>=N) break;
if(da[r-i][c+i]==1) n++;
}
for(int i=1; i<N; i++){
if(r+i>=N || c-i<0) break;
if(da[r+i][c-i]==1) n++;
}
if(n > m) m = n; return m;
} public static void main(String[] args)
{
int[][] da = new int[N][N]; f(da, 0, 0); System.out.println(max);
System.out.println(T);
}
} 注意:通过浏览器提交答案。只填写缺少的内容,不要填写任何多余的内容(例如:说明性文字或已有符号)。 spy(da, r, c) < 2

最新文章

  1. java 设计模式
  2. new bird in github
  3. Java多线程 3 线程同步
  4. java继承与多态-3个小题
  5. 爬网页?--Chrome帮你计算XPath
  6. 转:Eclipse 一直不停 building workspace... 完美解决总结
  7. 第一部分 mongodb 基础篇
  8. iptables常用操作
  9. 对BFC的理解
  10. python函数参数的pack与unpack
  11. Jquery对选取到的元素显示指定的长度,对于的字符串用“...”显示
  12. 虚拟Linux 訪问win7共享文件夹方法
  13. eMMC的MMC模式与SPI模式
  14. Oracle Dedicated server 和 Shared server(专用模式 和 共享模式) 说明(转)
  15. 【Nginx】epoll事件驱动模块
  16. iOS上传图片详解
  17. pytorch下的lib库 源码阅读笔记(2)
  18. 【LeetCode】9. 回文数
  19. Oracle的执行计划(来自百度文库)
  20. 因为链接服务器 &quot;SQLEHR&quot; 的 OLE DB 访问接口 &quot;SQLNCLI10&quot; 无法启动分布式事务

热门文章

  1. java.sql.SQLException: The server time zone value &#39;&#214;&#208;&#185;&#250;&#177;&#234;&#215;&#188;&#202;&#177;&#188;&#228;&#39; is unrecognized的报错问题
  2. Vue中跨域问题解决方案1
  3. 这么简单的ES索引生命周期管理,不了解一下吗~
  4. mysql+redis缓存策略常见的错误
  5. webpack使用babel
  6. 把iview中的table组件写成了一个公用组件,在另一个组件里去引用它的时候rander函数里的this指向不正确
  7. Template模式C++实现
  8. css概述三
  9. 轻松解决python异常处理,你值得拥有
  10. 04.PageNumberPagination分页