From 6be199baf1c9f853dc1aa2dcee2d82056125424a Mon Sep 17 00:00:00 2001 From: liangjinglin Date: Fri, 10 Jan 2025 14:14:45 +0800 Subject: [PATCH] =?UTF-8?q?2025-01-10=20=E6=B1=A1=E6=B8=8D=E6=A3=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dataANDcalc/java/UziChess.java | 79 ++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/dataANDcalc/java/UziChess.java 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