数据结构与算法韩顺平五子棋(源码+恢复数据)

写的有点乱...

package com.atguigu.sparsearray;

import java.io.*;

/**
 * @author Madison
 * @version 1.0
 */
public class SparseArray {
    public static void main(String[] args) throws IOException {
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[4][5] = 2;

        System.out.println("原始的二维数组:");
        for (int[] row : chessArr1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    sum++;
                }
            }
        }

        int sparseArr[][] = new int[sum + 1][3];
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;

        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; 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 i = 0; i < sparseArr.length; i++) {
            System.out.printf("%d\t%d\t%d\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
        }
        //
        toFile(count, sparseArr);
        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("恢复后的二维数组");

        toBoard();
/*        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }*/

    }

    public static void toFile(int count, int[][] sparseArr) throws IOException {
        String path = "src\\1.txt";
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fileWriter = new FileWriter(file);
        for (int i = 0; i < sparseArr.length; i++) {
            for (int j = 0; j < 3; j++) {
                fileWriter.write(sparseArr[i][j] + "\t");
            }
            fileWriter.write("\n");
        }
        fileWriter.close();
    }

    public static int rowNum() throws IOException {
        int num = 0;
        String path = "src\\1.txt";
        BufferedReader br = new BufferedReader(new FileReader(path));
        String len;
        while ((len = br.readLine()) != null) {
            String[] splits = len.split("\t");
            num = Integer.parseInt(splits[2]);
            break;
        }
        br.close();
        return num;
    }

    public static void toBoard() throws IOException {
        int sum = 0;
        int row = rowNum();
        String path = "src\\1.txt";
        BufferedReader br = new BufferedReader(new FileReader(path));
        int[][] arrNew = new int[row + 1][3];
        String len;
        while ((len = br.readLine()) != null) {
            String[] splits = len.split("\t");
            for (int i = 0; i < splits.length; i++) {
                arrNew[sum][i] = Integer.parseInt(splits[i]);
            }
            sum++;
        }
        for (int i = 0; i < arrNew.length; i++) {
            System.out.printf("%d\t%d\t%d\n", arrNew[i][0], arrNew[i][1], arrNew[i][2]);
        }
        br.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值