二维数组Java

时间:2016-04-08 07:01:31

标签: java

我有一个二维数组:

0 0 0 0 0 0 
0 0 0 0 0 0 
1 1 0 0 0 0 
0 0 0 0 1 1 
1 0 1 1 0 0 
1 0 0 0 1 1 

所有块水平或垂直占据两个相邻的单元格(单元格具有值= 1)。现在,我放下所有块,然后我得到一个新阵列:

0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 
1 1 0 0 0 0 
1 0 0 0 1 1 
1 0 1 1 1 1 

这是我的源代码:

package IOJava;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class ReadFile {
    public static int rows, cols;
    public static int[][] cells;

    public static void main(String[] args) throws IOException {

        try {
            File file = new File("input.txt");
            Scanner input = new Scanner(file);
            int rows = 0; // i chinh la so hang
            int cols = 0;
            int[][] newArr = new int[100][100];
            ArrayList string = new ArrayList();

            while (input.hasNextLine()) {
                String line = input.nextLine();
                line = line.trim();
                String[] arrLine = line.split(" ");
                cols = arrLine.length;
                for (int i = 0; i < arrLine.length; i++) {
                    newArr[rows][i] = Integer.parseInt(arrLine[i]);
                }
                rows++;
            }
            int[][] newCopyArr = twoDimensionalArrayClone(newArr);
            for (int i = 0; i <rows; i++) {
                for (int j = 0; j <cols; j++) {
                    if(i+1<rows && j+1<cols){
                        if(newCopyArr[i][j]==1 && newCopyArr[i][j+1]==1){
                            if(newCopyArr[i+1][j]==0 && newCopyArr[i+1][j+1]==0){
                                newCopyArr[i][j]=0 ; newCopyArr[i][j+1]=0;
                                newCopyArr[i+1][j]=1 ; newCopyArr[i+1][j+1]=1;
                            }
                        }
                        if(newCopyArr[i][j]==1 && newCopyArr[i+1][j]==1){
                            if(i+2<rows){
                                if(newCopyArr[i+2][j]==0){
                                    newCopyArr[i][j]=0;
                                    newCopyArr[i+2][j]=1;
                                }
                            }
                        }
//                      if(newCopyArr[i][j]==1 && newCopyArr[i+1][j]==1 && newCopyArr[i+1][j]==1){
//                          
//                      }
//                      
                    }
                }
            }
            //sysout

            for (int k = 0; k < rows; k++) {
                for (int h = 0; h < cols; h++) {
                    System.out.print(" " + newCopyArr[k][h]);
                }
                System.out.println();
            }
            input.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static int[][] twoDimensionalArrayClone(int[][] a) {
        int[][] b = new int[a.length][];
        for (int i = 0; i < a.length; i++) {
            b[i] = a[i].clone();
        }
        return b;
    }
}

但是,当我尝试用其他2d array []执行此代码时,它是不对的。

示例:

0 0 0 0 0 0 
0 1 1 0 0 0 
1 1 0 0 0 0   
0 0 0 0 1 1
1 0 1 1 0 0
1 0 0 0 1 1

0 个答案:

没有答案