Nbxnndnx 2022-05-16 22:31 采纳率: 69.2%
浏览 120
已结题

改变一个布尔值使连在一起的true的数量最多并找出那个位置

img


给出一个boolean类型的二维数组,
只有上下左右可以连接,比如上图改变第三行第二列的布尔值可以使连在一起的true数量最大为10,同数量时优先上然后左,该如何找出该位置

  • 写回答

7条回答 默认 最新

  • 太空眼睛 Java领域新星创作者 2022-05-17 22:29
    关注

    完整算法代码如下:

    public class Answer7720122 {
    
        public static void main(String[] args) {
            boolean[][] ccMatrix = {
                    {true, false, true, true},
                    {true, false, true, false},
                    {true, false, true, false},
                    {false, true, false, true},
                    {false, true, false, true},
                    {true, false, false, true}};
    
            Map<String, Integer> ijCount = new HashMap<>();
            for(int i=0; i<ccMatrix.length; ++i) {
                for(int j=0; j<ccMatrix[i].length; ++j) {
                    if(!ccMatrix[i][j]) {
                        ccMatrix[i][j] = true;
                        List<String> tempList = new ArrayList<>();
                        AtomicInteger count = new AtomicInteger(0);
                        count(ccMatrix, tempList, count, i, j, 1);
                        count(ccMatrix, tempList, count, i, j, 2);
                        count(ccMatrix, tempList, count, i, j, 3);
                        count(ccMatrix, tempList, count, i, j, 4);
                        ijCount.put(i+""+j, count.get());
                        ccMatrix[i][j] = false;
                    }
                }
            }
            List<Integer> count = ijCount.values().stream().sorted().collect(Collectors.toList());
            int max = count.get(count.size()-1);
            for(Map.Entry<String, Integer> entry : ijCount.entrySet()) {
                if(max == entry.getValue()) {
                    String key = entry.getKey();
                    System.out.println("修改第"+key.charAt(0)+"行第"+key.charAt(1)+"列可得到最大为"+max);
                }
            }
            System.out.println("注意:行和列均从0开始计数!");
        }
    
        /**
         *
         * @param arr
         * @param tempList
         * @param count
         * @param i
         * @param j
         * @param direction 递归方向。1:上左;2:上右;3:下左;4:下右;
         */
        private static void count(boolean[][] arr, List<String> tempList, AtomicInteger count, int i, int j, int direction) {
            int row = arr.length;
            int col = arr[0].length;
            if(i<0 || j<0 || i>=row || j>=col) {
                return;
            }
            if(!arr[i][j]) {
                return;
            }
            if(arr[i][j]) {
                String key = i+""+j;
                if(!tempList.contains(key)) {
                    tempList.add(key);
                    count.addAndGet(1);
                }
            }
            switch (direction) {
                case 1:
                    count(arr, tempList, count, i-1, j, direction);
                    count(arr, tempList, count, i, j-1, direction);
                    break;
                case 2:
                    count(arr, tempList, count, i-1, j, direction);
                    count(arr, tempList, count, i, j+1, direction);
                    break;
                case 3:
                    count(arr, tempList, count, i+1, j, direction);
                    count(arr, tempList, count, i, j-1, direction);
                    break;
                case 4:
                    count(arr, tempList, count, i+1, j, direction);
                    count(arr, tempList, count, i, j+1, direction);
                    break;
            }
        }
    }
    

    运行结果如下:

    修改第2行第1列可得到最大为10
    修改第3行第2列可得到最大为10
    注意:行和列均从0开始计数!
    

    调试了很久,请采纳,十分感谢!
    希望可以额外多加几个鸡腿!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(6条)

报告相同问题?

问题事件

  • 系统已结题 5月28日
  • 已采纳回答 5月20日
  • 修改了问题 5月16日
  • 创建了问题 5月16日

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集