稀疏数组 - 二维数组转稀疏数组

public class SparseArray {
    public static void main(String[] args) {
        //1 先定义一个二维数组,11x11的棋盘,0表示没有棋子,1表示白棋,2表示黑棋
        int[][] chessboard = initChessboard();
        System.out.println("======================初始化二维数组======================");
        for (int[] row : chessboard) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
        //2 转稀疏数组
        int[][] sparseArray = transToSparseArray(chessboard);
        System.out.println("======================转化后的稀疏数组======================");
        for (int[] row : sparseArray) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
    }

    private static int[][] transToSparseArray(int[][] chessboard) {
        // 1. 获取二维数组有多少个值
        int sum = getSum(chessboard);
        // 2. 创建稀疏数组
        int[][] sparseArray = initSparseArray(sum, chessboard);
        // 3. 给稀疏数组赋值
        assembleSparseArray(sparseArray, chessboard);
        return sparseArray;
    }

    private static void assembleSparseArray(int[][] sparseArray, int[][] chessboard) {
        int count = 0;
        int row = chessboard.length;
        int col = chessboard[0].length;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (chessboard[i][j] != 0) {
                    count++;
                    sparseArray[count][0] = i;
                    sparseArray[count][1] = j;
                    sparseArray[count][2] = chessboard[i][j];
                }
            }
        }
    }

    private static int[][] initSparseArray(int sum, int[][] chessboard) {
        int[][] sparseArray = new int[sum + 1][3];
        sparseArray[0][0] = chessboard.length;
        sparseArray[0][1] = chessboard[0].length;
        sparseArray[0][2] = sum;
        return sparseArray;
    }

    private static int getSum(int[][] chessboard) {
        int sum = 0;
        int row = chessboard.length;
        int col = chessboard[0].length;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (chessboard[i][j] != 0) {
                    sum++;
                }
            }
        }
        return sum;
    }

    private static int[][] initChessboard() {
        int[][] chessboard = new int[11][11];
        chessboard[1][2] = 1;
        chessboard[2][3] = 2;
        return chessboard;
    }
}

 

======================初始化二维数组======================
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	

Process finished with exit code 0

  

上一篇:基于微信小程序的五子棋小程序(含简单人机)


下一篇:【算法设计与分析】分治法求解棋盘覆盖问题