You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

二维数组连通1转7程序故障:触达最右列时停止运行

问题分析与修复方案

核心问题原因

  1. 循环次数严重不足
    你的finder函数里循环仅执行path.length+1次(对应20行数组就是21次),但需要替换的连通1的数量远多于这个次数,循环提前结束导致后续的1未被处理。

  2. 数组越界未彻底解决
    当程序走到数组最右列(col = 19)时,path[row][col+1]会访问索引20,超出数组列的合法范围(0-19),这会触发隐式异常或导致程序逻辑异常终止,直接停止后续处理。

  3. 遍历逻辑存在缺陷

  • 初始位置(0,0)的1未被替换为7,直接跳过
  • 仅判断了右、下、左三个方向,缺少向上的判断,遇到需要回溯向上的路径会无法处理
  • 未标记已访问的位置(比如替换后的7),可能出现重复判断或死循环,同时无法覆盖所有连通的1

修复后的代码

采用**深度优先搜索(DFS)**实现连通区域遍历,这是处理这类问题的标准方案:

public static void run(){ 
    int[][]nums = createPath(); 
    // 遍历所有位置,找到未处理的1并启动DFS
    for(int i=0; i<nums.length; i++){
        for(int j=0; j<nums[i].length; j++){
            if(nums[i][j] == 1){
                dfsFinder(nums, i, j);
            }
        }
    }
    printPath(nums); 
} 

// 深度优先搜索遍历连通区域
public static void dfsFinder(int[][] path, int row, int col){
    // 边界判断:超出范围或当前位置不是1则返回
    if(row < 0 || row >= path.length || col <0 || col >= path[0].length || path[row][col] != 1){
        return;
    }
    // 将当前1替换为7
    path[row][col] =7;
    // 递归遍历上下左右四个方向
    dfsFinder(path, row+1, col); // 下
    dfsFinder(path, row-1, col); // 上
    dfsFinder(path, row, col+1); // 右
    dfsFinder(path, row, col-1); // 左
}

// 保留原createPath函数不变
public static int[][] createPath(){ 
    int[][] path = { 
            {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
            {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
            {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0}, 
            {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
            {0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
            {0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
            {0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
            {0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0}, 
            {0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0}, 
            {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}, 
            {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}, 
            {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}, 
            {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}, 
            {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}}; 
    return path; 
} 

// 保留原printPath函数(移除冗余的createPath调用)
public static void printPath(int[][] path) { 
    for(int i=0; i<path.length; i++) { 
        for(int j=0; j<path[i].length; j++) { 
            System.out.print(path[i][j]+ " "); 
        } 
        System.out.println(); 
    } 
    System.out.println(); 
} 

修复说明

  • 用DFS递归遍历所有连通的1,确保不会遗漏任何连通区域
  • 每次递归前先做边界判断,彻底避免数组越界问题
  • 遍历所有初始位置,保证所有孤立的连通1都被处理
  • 替换当前位置后再递归上下左右,确保所有连通节点都被标记为7

内容的提问来源于stack exchange,提问作者Khariu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 22:05:05