Algorithm sparse array array optimization array compression two-dimensional array to sparse array algorithm collection (2)

 1. In the backgammon game, players stop fighting for a break halfway through the game. At this time, it is necessary to store the information of the current chess pieces of both sides.

     a. Use two-dimensional array storage:

                                         0 is empty,

                                         1 represents black

                                         2 represents the blue chess piece

      b. The chessboard has 11 rows and 11 columns ==> int [][] chessArray = new int [11][11];

      c. Problem: The entire array only stores two values. It's a bit of a waste of memory space ==>Extend to extract 0 or other identical values ​​to form a new array. It has the same effect as extracting public methods from the code~

2. Sparse array: When most of the elements in an array are 0, or an array of the same value, a sparse array can be used to save the array . (such as saving chessboard data, map information, etc.)

3. The processing method of sparse array is:

                                          1) Record how many rows and columns there are in the array and how many different values ​​there are

                                          2) Record the rows, columns and values ​​of elements with different values ​​in a small array to reduce the size of the program

 

   4. Idea analysis:

                 

        a. Traverse the original two-dimensional array and obtain the valid number of data (sum of non-zero values);

        b. Create a sparse array int [][] new sparseArr = new [sum + 1][3];

        c. The first row of the sparse array stores the original array information. There are 2 valid data in 11 rows and 11 columns, which are stored in the sparse array. So the sparse array needs sum+1 rows.

        c. Loop through the original array and store the current position information of black and blue chess pieces into a sparse array.

        d. Print the sparse array

        e. Convert the sparse array to the original array (I originally thought that it would be a direct two-level loop, and then judge whether the sparse array value is within this loop before using it. It requires multiple levels of loops, which is wrong. It is the conversion (decompression) of the sparse array to the original array. Use a non-sparse array to display the saved chessboard data)

package com.nami.algorithm.study.day01;

/**
 * beyond u self and trust u self.
 *
 * @Author: lbc
 * @Date: 2023-08-28 17:23
 * @email: [email protected]
 * @Description: keep coding
 */
public class SparseArray {


    public static void main(String[] args) {
        // 创建一个原始的二维数组 11 * 11
        // 0: 表示没有棋子, 1 表示 黑子 2 表蓝子
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[4][5] = 2;

        // 输出原始的二维数组
        System.out.println("原始的二维数组~~");
        for (int[] row : chessArr1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

        int sum = 0;
        for (int[] row : chessArr1) {
            for (int data : row) {
                if (data > 0) sum++;
            }
        }

        int sparseArr[][] = new int[sum + 1][3];
        // 给稀疏数组赋值
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;

        // 遍历二维数组,将非 0 的值存放到 sparseArr 中
        int count = 0; //count 用于记录是第几个非 0 数据
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }


        // 输出稀疏数组的形式
        System.out.println();
        System.out.println("得到稀疏数组为~~~~");
        for (int i = 0; i < sparseArr.length; i++) {
            System.out.printf("%d\t%d\t%d\t\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
        }
        System.out.println();
        //将稀疏数组 --》 恢复成 原始的二维数组

        //1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
        int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
        //2. 在读取稀疏数组后几行的数据(从第二行开始),并赋给 原始的二维数组 即可
        for (int i = 1; i < sparseArr.length; i++) {
            chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }

        // 输出恢复后的二维数组
        System.out.println();
        System.out.println("恢复后的二维数组");
        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

    }


}

   Recommend Shang Silicon Valley Algorithm Video

Summarize: 

          Sparse array is an abstraction of the original array, extracting different values ​​for storage. The first row of the sparse array stores the row information, column information, and the number of non-zero values ​​or different values ​​of the original array and saves them. When used, the sparse array is converted to the original array. Its role is equivalent to compressing the array. Decompression, similar to the process of compressing files                                           

Guess you like

Origin blog.csdn.net/qq_33919114/article/details/132555172
Recommended