4342024 2023-10-21 07:48 采纳率: 46.7%
浏览 22
已结题

c#大数据处理问题,选最少的行数使所有的列都至少有1个0,输出最少行所有的组合

| |1|2|3|
|A | 0 | 1 |0 |
|B| 1 | 0 | 1 |
|C |1 | 0 | 0 |
|D| 0 | 1 | 0 |
从ABCD中选出最少的行数,使得每一列都有一个0 ,比如选了AC后,就把BD行去掉,
之前数据就变成
|行名|1|2|3|
|A | 0 | 1 0 |
|C |1 |0 |0 |

然后1 2 3列依然都有至少一个0,且小于这个行数的都不满足可使1 2 3列都有一个0,那么AC就是其中一组满足条件的组合

但是如果为100行 20000列类似
|列名|列1|列2|...列20000|
|A | 0 | 1 |0|1|
|B| 1 | 0 | 1 ||0|
|C |1 | 0 |0 ||1
|D| 0 | 1 | 0|.|0

|行100|0|1|0|
这种如何处理?要求输出所有符合条件的组合,速度快,占内存小,应该怎么做,可以讲思路,算法,最好用c#写出来

  • 写回答

5条回答 默认 最新

  • 社区专家-Monster-XH 2023-10-21 09:12
    关注

    基于Monster 组和GPT的调写:

    
    using System;
    using System.Collections.Generic;
    
    class Program
    {
        static void Main()
        {
            int[,] matrix = {
                { 0, 1, 0 },
                { 1, 0, 1 },
                { 1, 0, 0 },
                { 0, 1, 0 }
            };
    
            int minRows = matrix.GetLength(0);
            var solutions = new List<List<int>>();
            FindCombinations(matrix, new List<int>(), 0, solutions, ref minRows);
    
    
            
            // 打印所有解决方案
            foreach(var solution in solutions)
            {
                foreach(var row in solution)
                {
                    Console.Write((char)('A' + row));
                }
                Console.WriteLine();
            }
        }
    
        static void FindCombinations(int[,] matrix, List<int> current, int start, List<List<int>> solutions, ref int minRows)
        {
            if (current.Count > minRows)
                return;
    
            if (IsValidCombination(matrix, current))
            {
                if (current.Count < minRows)
                {
                    solutions.Clear();
                    minRows = current.Count;
                }
                solutions.Add(new List<int>(current));
                // 不要在这里返回; 继续寻找其他可能的解决方案
            }
    
            for (int i = start; i < matrix.GetLength(0); i++)
            {
                current.Add(i);
                FindCombinations(matrix, current, i + 1, solutions, ref minRows);
                current.RemoveAt(current.Count - 1);
            }
        }
    
    
    
        static bool IsValidCombination(int[,] matrix, List<int> combination)
        {
            if (combination.Count == 0) return false;
    
            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                bool hasZero = false;
                foreach (int row in combination)
                {
                    if (matrix[row, col] == 0)
                    {
                        hasZero = true;
                        break;
                    }
                }
                if (!hasZero) return false;
            }
            return true;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 10月29日
  • 已采纳回答 10月21日
  • 创建了问题 10月21日

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题