Graphic sparse array Java data structures

In programming, the importance of self-evident algorithm, the program algorithm is not without a soul. It shows the importance of the algorithm.
However, before learning algorithm we need to have a data structure, data structure is the basis of the algorithm.
I was in college, the school of the data structure is written in C language to teach, because the C language is not very understanding, it is not particularly good grasp of some learning materials in the Internet is basically also be used in C language teaching data structures.
So, from the beginning of this article, I will use the Java language to introduce a data structure, of course, after the data structure is the algorithm.

Linear and nonlinear structural configuration
  1. Linear structure
    linear structure as the most commonly used data structures, which is characterized by the presence of one linear relationship between the data elements;
    linear structure is stored in two different structures, i.e. sequential storage structure and chain storage structure. Storing a table called a linear sequence table order, the sequence stored in the table element is continuous;
    linked storage table called a linear linked list stored in the element is not necessarily contiguous, element node and an adjacent data elements stored address information element;
    common linear structure: array, queues, linked lists, and stacks
  2. Nonlinear structure
    nonlinear structure comprising: a two-dimensional array, a multidimensional array, the generalized table, the tree structure, the structure of FIG.
Sparse array

After the data structure to have a preliminary understanding, we started some specific data structure detailed analysis.
Let's look at an actual demand:
this is a backgammon program that has the function to save and exit and continue on the disc, as shown below, how the following figure chess game to save it?
Here Insert Picture Description
That this question is very simple, a lot of people might think of using a two-dimensional array to store.
Here Insert Picture Description
As shown above, we express no children with 0, 1 sunspots, 2 basket, but the big problem this program, because a lot of the value of the two-dimensional array are the default value of 0, so that the recording does not make sense a lot of data, this time we will be able to carry out a compression of the two-dimensional array using sparse arrays.
So what sparse array in the end what is it?
When most of the elements in an array is 0, or when a value of the same array, the sparse array can be used to hold the array.
Sparse array of treatment methods are:

  1. An array of records, a total of several odd row, how many different values
  2. The ranks of elements having different values, and the value is recorded in a small array, thereby downsizing the program

So understanding the concept of sparse array, we improved by about 331 procedures a sparse array.
Here Insert Picture Description
After compression sparse array, the original array from November 11 original line turns the three rows and three columns.
Recording the first line of the sparse array is the number of rows and columns and the number of elements of the original array.
Each subsequent row is recorded and the value of the effective position of the element, for example the second row is the original recording element array located on the 1, 2 position; and third rows of the original array is positioned on the 3 position 2 elements.
In summary, the idea of a two-dimensional array to a sparse array:

  1. Traversal of the original two-dimensional array, the effective number of elements to be saved
  2. Create a sparse array sparseArr according to the number of effective elements
  3. The two-dimensional array of valid data can be stored in a sparse array

Sparse array to a two-dimensional array of original ideas:

  1. The first line read first sparse array, create an original two-dimensional array based on the data of the first line
  2. After reading a few lines of data sparse arrays, and can be assigned to the original two-dimensional array

About realization of ideas has been analyzed, the next code is implemented.
The two-dimensional array to a sparse array implemented with the following code:

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;

        System.out.println("原始的二维数组:");

        for (int[] row : chessArr1) {
            for (Integer value : row) {
                System.out.printf("%d\t", value);
            }
            System.out.println();
        }

        // 将二维数组转稀疏数组
        // 先遍历二维数组,得到非0的元素个数
        int sum = 0;
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0; j < chessArr1[i].length; j++) {
                if (chessArr1[i][j] != 0) {
                    sum++;
                }
            }
        }

        // 创建对应的稀疏数组
        int sparseArr[][] = new int[sum + 1][3];
        // 给稀疏数组赋值
        // 稀疏数组第一行存的是原始数组的行数、列数和有效元素个数
        sparseArr[0][0] = chessArr1.length;
        sparseArr[0][1] = chessArr1[0].length;
        sparseArr[0][2] = sum;

        // 遍历二维数组,将非0的值存入到稀疏数组中
        int count = 0; // 用于记录是第几个非0数据
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0; j < chessArr1[i].length; 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[] row : sparseArr) {
            for (Integer value : row) {
                System.out.printf("%d\t", value);
            }
            System.out.println();
        }
    }

Results are as follows:

原始的二维数组:
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   2   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   

稀疏数组:
11  11  2   
1   2   1   
2   3   2   

In this way, we succeeded in a two-dimensional array into a sparse array.

So how do sparse array into a two-dimensional array in code?

        // 将稀疏数组转为二维数组
        // 先读取稀疏数组的第一行,根据第一行的数据创建原始数组
        int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];

        // 读取稀疏数组后几行数据(从第二行开始读取),并赋给原始数组
        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 (Integer value : row) {
                System.out.printf("%d\t", value);
            }
            System.out.println();
        }

After thinking thread clearance, the code is very simple, look at operating results:

原始的二维数组:
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   2   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   

稀疏数组:
11  11  2   
1   2   1   
2   3   2   

恢复后的二维数组:
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   2   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   

Overall code is as follows:

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;

        System.out.println("原始的二维数组:");

        for (int[] row : chessArr1) {
            for (Integer value : row) {
                System.out.printf("%d\t", value);
            }
            System.out.println();
        }

        // 将二维数组转稀疏数组
        // 先遍历二维数组,得到非0的元素个数
        int sum = 0;
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0; j < chessArr1[i].length; j++) {
                if (chessArr1[i][j] != 0) {
                    sum++;
                }
            }
        }

        // 创建对应的稀疏数组
        int sparseArr[][] = new int[sum + 1][3];
        // 给稀疏数组赋值
        // 稀疏数组第一行存的是原始数组的行数、列数和有效元素个数
        sparseArr[0][0] = chessArr1.length;
        sparseArr[0][1] = chessArr1[0].length;
        sparseArr[0][2] = sum;

        // 遍历二维数组,将非0的值存入到稀疏数组中
        int count = 0; // 用于记录是第几个非0数据
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0; j < chessArr1[i].length; 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[] row : sparseArr) {
            for (Integer value : row) {
                System.out.printf("%d\t", value);
            }
            System.out.println();
        }

        // 将稀疏数组转为二维数组
        // 先读取稀疏数组的第一行,根据第一行的数据创建原始数组
        int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];

        // 读取稀疏数组后几行数据(从第二行开始读取),并赋给原始数组
        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 (Integer value : row) {
                System.out.printf("%d\t", value);
            }
            System.out.println();
        }
    }

Guess you like

Origin www.cnblogs.com/blizzawang/p/11411602.html