diff --git a/src/dataANDcalc/java/UziChess.java b/src/dataANDcalc/java/UziChess.java new file mode 100644 index 0000000..bf8053d --- /dev/null +++ b/src/dataANDcalc/java/UziChess.java @@ -0,0 +1,79 @@ +import java.util.Scanner; + +public class UziChess { + + private int[][] chessArray; + private int totalRow; + private int totalCol; + + private UziChess(int i, int y){ + this.totalRow = i; + this.totalCol = y; + chessArray = new int[totalRow][totalCol]; + } + + private void chessToSparse(int[][] chessArray, int row, int col, int val){ + chessArray[row][col] = val; + System.out.println("棋盘:"); + for(int i = 0; i < totalRow; i++) { + for(int j = 0; j < totalCol; j++) { + System.out.print(chessArray[i][j] + ";"); + } + System.out.println(); + } + int sum = 0; + // 统计有效值个数 + for(int i = 0; i < totalRow; i++) { + for(int j = 0; j < totalCol; j++) { + if(chessArray[i][j] != 0) { + sum ++; + } + } + } + // 初始化稀疏数组 + int sparaseArr[][] = new int[sum+1][3]; + sparaseArr[0][0] = this.totalRow; + sparaseArr[0][1] = this.totalCol; + sparaseArr[0][2] = sum; + // 存放值进入稀疏数组 + int count = 1; + for(int i = 0; i < totalRow; i++) { + for(int j = 0; j < totalCol; j++) { + if(chessArray[i][j] != 0) { + sparaseArr[count][0] = i; + sparaseArr[count][1] = j; + sparaseArr[count][2] = chessArray[i][j]; + count ++; + } + } + } + + System.out.println("稀疏数组:"); + for(int i = 0; i