Fill a Matrix with Random Number with no repeat vertically or horizontally

ADM :

This is more of a logical question . Problem IS:

I need to fill a Matrix with number (1-9) In such a way so that :

  1. No number should repeat in row
  2. No number should repeat in column
  3. Matrix can be from 3X3 to 8X8
  4. Matrix should contain Random numbers not in some particular order

I am not good at putting logic what i have tried is below :

public class RandMatrix {
static int max=8;
static ArrayList<Integer> numbers=new ArrayList<>();
static  int[][] arr=new int[max][max];
public static void main(String[] a){
    // To fill number
    for (int i = 1; i <=9; i++) {
        numbers.add(i);
    }
    // Shuffle number
    Collections.shuffle(numbers);
    call();
}

public static void call(){
    for (int i = 0; i < max; i++) {
        for (int j = 0; j <max ; j++) {
            for (int k = 0; k <max ; k++) {
                int num=numbers.get(k);
                if(!isExist(num,i,j)){
                    arr[i][j]=num;
                    break;
                }
            }
        }
        Collections.shuffle(numbers);
    }
}

private static boolean isExist(int num,int row, int col){
    for (int i = row; i >=0; i--) {
        if(arr[i][col]==num){
            return true;
        }
    }
    for (int j = col; j >=0; j--) {
        if(arr[row][j]==num){
            return true;
        }
    }
    return false;
}
}

When i print the 2-d array i see in some places there is still 0 as value . Seems like my code breaks. at some point there is no random number left which can be filled. Output is something like :

enter image description here

I know my algo is not right i just can not find a way to make it done . Can i get some help on this.

snr :

I've saved and modified some the code a while ago so as to use if I need another time. I think it's for you ;)

import java.util.Arrays;
import java.util.Random;

class Test {
    public static void main(String[] args){
        int size = 9;

        int[][] matrix= new int[size][];
        matrix[0] = MatrixOps.createOrderedArray(size, 1);

        for(int x=0; x < size; x++) {
            matrix[x] = MatrixOps.createOrderedArray(size, 1);
            do {
                MatrixOps.shuffle(matrix[x]);
            } while(! MatrixOps.compare2DArray(matrix[x], matrix, 0, x));
        }
        MatrixOps.print(matrix);
    }
}

class MatrixOps {

    public static void shuffle(int[] arr){
        Random random = new Random();
        for(int x = 0; x < arr.length; x++)
            swap(arr, x, random.nextInt(arr.length));
    }

    public static int[] createOrderedArray(int size, int startValue) {
        int[] num = new int[size];
        for (int x = 0; x < num.length; x++)
            num[x] = x + startValue;
        return num;
    }

    public static boolean compare2DArray(int[] arr1, int[][] arr2, int begin, int end) {
        for (int x = begin; x < end; x++)
            if (!compareArray(arr1, arr2[x]))
                return false;
        return true;
    }

    // https://stackoverflow.com/questions/19648240/java-best-way-to-print-2d-array/41533179#41533179
    public static void print(int[][] array) {
        for (int[] x: array) {
            for (int y: x) {
                System.out.print(y + " ");
            }
            System.out.println();
        }
    }

    private static boolean compareArray(int[] arr1, int[] arr2){
        if(arr1.length != arr2.length)
            return false;
        for(int x=0; x<arr1.length; x++)
            if(arr1[x] == arr2[x])
                return false;
        return true;
    }

    private static void swap(int[] arr, int a, int b){
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
}

Example output:

5 1 7 2 3 8 9 4 6 
4 3 1 5 7 9 2 6 8 
9 7 3 8 6 2 4 5 1 
6 8 4 3 5 7 1 9 2 
1 5 8 9 2 6 7 3 4 
7 9 2 6 4 1 5 8 3 
8 6 9 4 1 5 3 2 7 
3 2 6 7 9 4 8 1 5 
2 4 5 1 8 3 6 7 9 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=76062&siteId=1