简体   繁体   English

将所有可能的二进制矩阵生成为字符串或生成所有可能的矩阵

[英]Generate all possible binary matrix as string or generate all possible matrix

I have 12*3 matrix as below 我有如下的12 * 3矩阵

matrix = 0- [0,0,0]
         1- [0,0,0]
         2- [0,0,0]
            .......
        11- [0,0,0]

Each row must have only one selection as value 1 每行只能有一个选择作为值1

matrix = 0- [1,0,0]
         1- [1,0,0]
         2- [1,0,0]
            .......
        11- [1,0,0]

My answer array in this case is 在这种情况下,我的答案数组是

Ans = {1,0,0,1,0,0,1,0,0,....,1,0,0}

I want to generate all answers, like this: 我想生成所有答案,像这样:

Ans = {1,0,0,1,0,0,1,0,0,....,1,0,0}
Ans = {0,1,0,1,0,0,1,0,0,....,1,0,0}
Ans = {0,0,1,1,0,0,1,0,0,....,1,0,0}
Ans = {1,0,0,0,1,0,1,0,0,....,1,0,0}

Could you please help me to select best algorithm. 您能帮我选择最佳算法吗?

Edit: preferable language is C# 编辑:首选语言是C#

You can think of each such an array as coding a number in base 3 in the range 0 to 3^12 - 1. The following Python script (with comments suggesting how to convert to C# -- a language I don't use) shows how you can go from such a number to the array: 您可以将每个这样的数组视为在0到3 ^ 12-1范围内以3为底的数字编码。下面的Python脚本(带有有关如何转换为C#的注释,这是我不使用的语言)如何从这样的数字转到数组:

def decode(n):
     A = [0]*36 #initialize array of 36 zeros
     for i in range(0,12): #for(int i = 0; i < 12; i++) in C#
          r = n % 3
          n = n // 3 #integer division, just use / in C# if n is an int
          A[3*i+r] = 1
     return A

#test:
n = 1*3**0 + 2*3**1 + 1*3**2 + 2*3**3 + 1*3**5
A = decode(n)
print(A)

Output: 输出:

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

Note that the 1 is in the second position in the first group of 3, the third position in the second group of 3, the second position in the next, the fist position in the fourth, the third position in the fifth, and then in the first position in the remaining blocks. 请注意,1在第一组3中处于第二位置,在第二组3中处于第三位置,第二位置在第二组中,拳头位置在第四中,第三位置在第五中,然后在其余块中的第一个位置。

To make a full solution I used c++: 为了提供完整的解决方案,我使用了c ++:

#include <stdio.h>
void printarr(int arr[]);
FILE *fileptr;
FILE **file;
int main()
{
    int finarr[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
    int aux=0;
    file=&fileptr;
    fopen_s(file,"fichero.txt","w");

    while(finarr[11]!=3)
    {
        finarr[0]=aux;
        printarr(finarr);
        aux++;
        if(aux==3)
        {
            aux=0;
            finarr[0]++;
            for(int idx=0;idx<11;idx++)
            {
                if(finarr[idx]==3)
                {
                    finarr[idx]=0;
                    finarr[idx+1]++;
                }
            }
        }

    }

    printarr(finarr);

    return 0;
}

void printarr(int arr[])
{
    if(arr[11]==3)
        return;
    fprintf(fileptr,"Ans = {");
    for(int idx=0;idx<12;idx++)
    {
        if(idx!=0)
            fprintf(fileptr,",");

        switch(arr[idx])
        {
        case 0:
            fprintf(fileptr,"1,0,0");
            break;
        case 1:
            fprintf(fileptr,"0,1,0");
            break;
        case 2:
            fprintf(fileptr,"0,0,1");
            break;
        }

    }
    fprintf(fileptr,"}\n");
}

Solution in c# that write all the answers in a file with the format that you asked. c#中的解决方案,以您要求的格式将所有答案写入文件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace DozeFilastres
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> lines = new List<string>();
            int[] finarr = new int[]{0,0,0,0,0,0,0,0,0,0,0,0};
            int aux=0;


            while(finarr[11]!=3)
            {
                finarr[0]=aux;
                lines.Add(printarr(finarr));
                aux++;
                if(aux==3)
                {
                    aux=0;
                    finarr[0]++;
                    for(int idx=0;idx<11;idx++)
                    {
                        if(finarr[idx]==3)
                        {
                            finarr[idx]=0;
                            finarr[idx+1]++;
                        }
                    }
                }

            }

            File.WriteAllLines("fichero.txt",lines.ToArray());
        }

        static string printarr(int[] arr)
        {
            string all="Ans = {";
            for(int idx=0;idx<12;idx++)
            {
                if(idx!=0)
                    all+=",";

                switch(arr[idx])
                {
                case 0:
                    all+="1,0,0";
                    break;
                case 1:
                    all+="0,1,0";
                    break;
                case 2:
                    all+="0,0,1";
                    break;
                }

            }
            all+="}";

            return all;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM