2025-01-10 污渍棋
This commit is contained in:
parent
5257d175b4
commit
6be199baf1
79
src/dataANDcalc/java/UziChess.java
Normal file
79
src/dataANDcalc/java/UziChess.java
Normal file
@ -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<sum+1; i++){
|
||||||
|
for(int j = 0; j<3; j++){
|
||||||
|
System.out.print(sparaseArr[i][j] + ";");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
UziChess uziChess = new UziChess(10,10);
|
||||||
|
Scanner scan = new Scanner(System.in);
|
||||||
|
while(true) {
|
||||||
|
System.out.println("y继续, n退出:");
|
||||||
|
String exit = scan.next();
|
||||||
|
if ("n".equals(exit)){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
System.out.println("请输入行:");
|
||||||
|
int row = scan.nextInt();
|
||||||
|
System.out.println("请输入列:");
|
||||||
|
int col = scan.nextInt();
|
||||||
|
System.out.println("请输入黑子或白子:");
|
||||||
|
int val = scan.nextInt();
|
||||||
|
uziChess.chessToSparse(uziChess.chessArray, row, col, val);
|
||||||
|
}
|
||||||
|
scan.close();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user