算法01 二维数组的环形填充

package link;

public class RingSetArray {

    int[][] array;
    private int startValue = 0;
    private int row = 0;
    private int col = 0;

    public RingSetArray(int row, int col) {

        this.row = row;
        this.col = col;
        array = new int[this.row][this.col];
        setArrayValue(this.row, this.col, 0);
    }

    @Override
    public String toString() {
        StringBuffer buff = new StringBuffer();
        for (int i = 0; i < this.row; i++) {
            for (int j = 0; j < this.col; j++) buff.append(array[i][j] + "\t");
            buff.append("\n");
        }
        return buff.substring(0, buff.length()-2);
    }

    private void setArrayValue(int row, int col, int start) {

        if (row >= 1 && col >= 2) {
            //上边界
            System.out.print("上:");
            for (int i = start; i < col; i++) {
                // 数组的索引为 [start][i]
                array[start][i] = startValue++;
                System.out.print(start + "" + i + " ");
            }

            //右边界
            System.out.print("右:");
            for (int i = start + 1; i < row; i++) {
                // 数组的索引为 [i][col-1]
                array[i][col-1] = startValue++;
                System.out.print(i + "" + (col-1) + " ");
            }

            //下边界
            System.out.print("下:");
            if(row > 1)
                for (int i = (col - 2); i >= start; i--) {
                    // 数组的索引为 [row-1][i]
                    array[row - 1][i] = startValue++;
                    System.out.print((row - 1) + "" + i + " ");
                }

            //左边界
            System.out.print("左:");
            for (int i = (row - 2); i > start; i--) {
                // 数组的索引为 [i][start]
                array[i][start] = startValue++;
                System.out.print(i + "" + start + " ");
            }

            //row - 1, col - 1, start + 1
            // 递归调用本函数
            System.out.println();
            setArrayValue(row - 1, col - 1, start + 1);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用遗传算法生成二维数组的步骤如下: 1. 创建初始种群pop,即一个二维矩阵,其大小为POP_SIZE行,每行有DNA_SIZE*2个元素,元素的值为0或1。可以使用numpy库的random.randint函数生成随机的初始种群。例如,可以使用以下代码生成一个初始种群pop: ``` pop = np.random.randint(0, 2, size=(POP_SIZE, DNA_SIZE*2)) ``` 这里的POP_SIZE是种群的大小,DNA_SIZE是每个个体的基因长度。 2. 定义适应度函数来评估种群中每个个体的适应度。适应度函数可以根据具体问题进行定义,例如对于二维数组生成问题,可以根据全1行数最多的原则来定义适应度函数。 3. 进行选择操作,选择较优秀的个体作为下一代的父代。选择操作可以使用轮盘赌算法、锦标赛选择等方法。 4. 进行交叉操作,通过基因的交叉组合产生新的个体。交叉操作可以使用单点交叉、多点交叉等方法。 5. 进行变异操作,对个体的基因进行变异。变异操作可以随机改变个体的某些基因值。 6. 重复步骤3至5,直到达到停止条件(例如达到最大迭代次数或找到满足要求的解)。 最终,遗传算法生成的最优个体即为所求的二维数组。 需要注意的是,具体的实现细节会因问题的不同而有所差异,以上是一般的遗传算法生成二维数组的步骤概述。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [1.遗传算法求解二维函数最大值(动态展示)](https://blog.csdn.net/clear1205/article/details/125217839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatgptT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [论坛:二维数组中找最大特征数组](https://blog.csdn.net/goldenhawking/article/details/116240710)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatgptT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值