用于填充二维数组的逻辑

时间:2016-09-25 15:02:02

标签: c algorithm matrix logic

我正在寻找一个通用逻辑来填充双维数组,只提供0' s和1提供Row_wise_sum和Column_wise_sum。

以下是我的示例代码,但逻辑无效。

请注意:Col的安排顺序并不重要。说Row的顺序很重要,RowSum和ColSum应该完全符合预期。

另一个例外/条件是All Row是独一无二的。

即使RowSize和ColSize更改为任何值,也需要适合的逻辑。让我们说10x10或4x8或2x9或7x4 ......等等。

#include <stdio.h>

/*

0   1   1   1   1   | 4  --> Row wise totals, order is important
1   0   0   1   0   | 2
1   1   0   1   1   | 4
0   1   0   1   1   | 3
1   1   1   1   0   | 4
---------------------
3   4   2   5   3       --> Column wise totals, order is not important

*/


int main()                      
{

    int i = 0;
    int j = 0;

    int RowSum[5] = {4, 2, 4, 3, 4};
    int ColSum[5] = {3, 4, 2, 5, 3};

    int matrix[5][5] = {0};

    // Fill up the matrix
    for(i=0; i<5; ++i)
    {
        for(j=0; j<5; ++j)
        {
            if(RowSum[i]>0 && ColSum[j]>0)
            {
                matrix[i][j] = 1;
                RowSum[i]--;
                ColSum[j]--;
            }
        }

    }


    // Print matrix
    for(i=0; i<5; ++i)
    {
        printf("\n");
        for(j=0; j<5; ++j)
        {
            printf(" %d ", matrix[i][j]);
        }   
    }


    // Validate RowSum
    printf("\n\n");
    for(i=0; i<5; ++i)
    {
        printf("\n RowSum[%d] = %d ", i+1, RowSum[i]);
        if(RowSum[i] != 0)
            printf("Invalid, must be 0");
    }


    // Validate ColSum
    printf("\n\n");
    for(i=0; i<5; ++i)
    {
        printf("\n ColSum[%d] = %d ", i+1, ColSum[i]);
        if(ColSum[i] != 0)
            printf("Invalid, must be 0");
    }

    printf("\n\n");

    return 0;

}

/* Output


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


 RowSum[1] = 0 
 RowSum[2] = 0 
 RowSum[3] = 0 
 RowSum[4] = 0 
 RowSum[5] = 2 Invalid, must be 0


 ColSum[1] = 0 
 ColSum[2] = 0 
 ColSum[3] = 0 
 ColSum[4] = 1 Invalid, must be 0
 ColSum[5] = 1 Invalid, must be 0


*/

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果没有其他方法你可以想到通过每个可能的数组(2 ^(RowSum.length * ColSum.length)可能性,并且检查数组是否满足Row / Col sums。这个想法相当简单,所以可能有几种方法可以获得所有组合,这是我在java中做的非常粗略的尝试,但上面的想法应该很容易扩展到C

public class f {

    static int[] RowSum={4,2,4,3,4};
    static int[] ColSum={3,4,2,5,3};
    static int[] tmp = new int[RowSum.length*ColSum.length];


    /**
     *
     * @param A array w/ 1s and 0s
     * @return true if A is a valid solution
     */
    static boolean sums(int[] A){
        for(int i=0;i<ColSum.length;i++){
            int sum = 0;
            for(int j=0;j<RowSum.length;j++){
                sum+=A[i+j*ColSum.length];
            }
            if(sum!=ColSum[ColSum.length-i-1]) return false;
        }

        for(int i=0;i<RowSum.length;i++){
            int sum = 0;
            for(int j=0;j<ColSum.length;j++){
                sum+=A[j+i*ColSum.length];
            }
            if (RowSum[RowSum.length-1-i]!=sum) return false;
        }
        return true;
    }


    /**
     *
     * @param A array length = RowSum*ColSum initially filled with only 0s
     * @param i initialize to 0
     * @param j initialize to A.length-1
     */
    static void g(int[] A,int i, int j){
        if(sums(A)) {
            // copy A to tmp
            System.arraycopy(A, 0, tmp, 0, A.length);
            return;
        }
        if(i<=j){
            A[i]=0;
            g(A,i+1,j);
            A[i]=1;
            g(A,i+1,j);
        }

    }

    /**
     *
     * @param A array w/ length l
     * @return 2d array with length = l/RowSum and height = l/ColSum
     */
    static int[][] h(int[] A){
        int[][] result = new int[RowSum.length][ColSum.length];
        for(int i=0;i<result.length;i++){
            for(int j=0;j<result[0].length;j++){
                result[i][j]=A[i*result[0].length+j];
            }
        }
        return result;
    }

    static int[] reverse(int[] A){
        for(int i = 0; i < A.length / 2; i++) {
            int temp = A[i];
            A[i] = A[A.length - i - 1];
            A[A.length - i - 1] = temp;
        }
        return A;
    }

    // print 2d array
    static void print(int[][] A){
        for(int[] i:A) System.out.println(Arrays.toString(i));
    }

    static int[][] solve(){
        tmp=reverse(tmp); // reverse to make array look like array in question
        return h(tmp); // change to 2d array and return result
    }



    public static void main(String [] args) {
        //int A[] = {0,1,1,1,1,1,1,0,1,0,1,1,0,1,1,0,1,0,0,1,1,1,1,1,0};
        int Arr[] = new int[ColSum.length*RowSum.length];
        g(Arr,0,Arr.length-1);
        print(solve());

    }



}

输出我得到:

[1, 1, 1, 1, 0]
[1, 0, 0, 1, 0]
[1, 1, 0, 1, 1]
[0, 1, 0, 1, 1]
[0, 1, 1, 1, 1]