删除列表列表中列中的冲突

本文关键字:列表 冲突 删除 删除列 | 更新日期: 2023-09-27 18:01:09

我正在尝试一些简单的方法来删除数据结构中列中的任何冲突。

例如,我有一个int列表列表!

0 0 0 2 3 4
1 0 2 3 0 0
0 2 1 4 0 0

由于您只看到5个数字0、1、2、3、4,我希望每个列中都有相同的数字或。它应该是什么样子,例如上面的例子:

0 0 0 2 3 4 0 0 0 0 0 0
1 0 2 0 0 0 2 3 0 0 0 0
0 2 0 0 0 0 0 0 1 4 0 0

因此,我在C#中为此制作了一个简单的程序:

for (int t1 = 0; t1 < myList.Count; t1++)
      {
        for (int t2 = 0; t2 < myList[t1].Count; t2++)
            {
               for (int t3 = 0; t3 < myList.Count; t3++) 
                    {
                        if (myList[t1][t2] != myList[t3][t2])
                              {
                                 if (myList[t1][t2] != 0)
                                     {
                                        if (myList[t3][t2] != 0)
                                           { 
                                             myList[t3].Insert(t2, 0);
                                            for (int uui = 0; uui < myList.Count; uui++)
                                                {
                                                    if (uui != t3)
                                                    {
                                                        myList[uui].Add(0);
                                                    }
                                                }
                                            }
                                        }  
                                    }
                                }
                            }
                        }

正如你所看到的,我是按列搜索的,它会给每个子列表添加零,除了带插入的子列表,以避免超出范围的异常。有趣的是,它通常有效!但在某些情况下,仍然存在冲突。我的问题是,为什么它只适用于某些情况(通常是小情况,最大5x5(?

此数据集的另一个示例:

0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 4 3 4 3 3 
1 0 0 0 0 0 1 4 3 2 2 0 0 0 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 0 2 0 3 2 2 4 0 0 0 0 0 0 0 
0 0 3 0 0 0 0 0 0 2 0 3 0 0 0 3 4 3 0 0 0 0 
0 4 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 1 2 4 1 
0 0 3 0 3 0 1 0 3 0 0 0 0 0 4 3 0 0 0 0 0 0

结果是:

0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 4 3 4 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 1 4 3 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 0 2 0 3 2 2 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 3 0 0 0 0 0 0 2 0 3 0 0 0 3 4 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 4 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 1 2 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 3 0 0 3 0 0 0 0 0 0 0 0 0 0 0 1 0 3 0 0 0 0 0 0 4 3 0 0 0 0 0 0 0 0 0 0 0 0 

不幸的是,冲突仍然存在:第一个列表中从右起的第一个"3"在第四个列表中的编号为1。第一个列表中的"4"在第4个列表中有"3"。

删除列表列表中列中的冲突

当您遇到冲突时,您会在行的末尾添加。试着解决整列的冲突。因此,较低的行不能添加冲突,这些冲突在以后的迭代中无法解决。

完整的测试程序:

void Main()
{
//  var s = "0 0 0 2 3 4'r'n1 0 2 3 0 0'r'n0 2 1 4 0 0";
    var s = @"
    0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 4 3 4 3 3 
    1 0 0 0 0 0 1 4 3 2 2 0 0 0 0 0 0 0 0 0 0 0 
    1 0 0 0 0 0 0 0 0 2 0 3 2 2 4 0 0 0 0 0 0 0 
    0 0 3 0 0 0 0 0 0 2 0 3 0 0 0 3 4 3 0 0 0 0 
    0 4 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 1 2 4 1 
    0 0 3 0 3 0 1 0 3 0 0 0 0 0 4 3 0 0 0 0 0 0
";
    var myList = s.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Select (x => x.Split(new char [] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select (y => Int32.Parse(y)).ToList()).ToList();
    Print(myList, "Before");
    int rows = myList.Count;    
    for (int row = 0; row < rows; row++)
    for (int col = 0; col < myList[row].Count; col++)
    for (int t3 = 0; t3 < rows; t3++)
    if (myList[row][col] != 0 && myList[t3][col] != 0 && myList[row][col] != myList[t3][col])
    {           
        //Print(myList, "Before Step");
        for (int uui = 0; uui < rows; uui++)
        {
            if (uui < t3) // yours: if (uui != t3)
            {
                // yours: myList[uui].Add(0);
                myList[uui].Insert(col + 1, 0);
            }
            else
            {
                myList[uui].Insert(col, 0);
            }
        }
//      Print(myList, "After Step");
        break;      
    }       
    Print(myList, "After");
}
public void Print(List<List<int>> list, string header)
{   
    Console.WriteLine(header);
    Console.WriteLine(ToString(list));
}
public string ToString(List<List<int>> list)
{
    return String.Join(Environment.NewLine, list.Select (l => String.Join(" ", l)));
}

在每列中查找相同的数字或零,如图所示在上面

一个非常简单的算法:

public int countNonZero(int[][] myList, int row)
{
   var res = 0;
   for (int i = 0; i < numColumns; ++i)
     if (myList[row][i] != 0) ++res;
   return res;
}
public void removeConflicts(int[][] myList)
{
   for (int i = 0; i < numRows; ++i)
   {
      var iNum = countNonZero(myList, i);
      for (int j = i + 1; j < numRows; ++j)
      {
         var jNum = countNonZero(myList, j);
         if (iNum == jNum)
         {
             // row #i conflicts with row #j
         }
      }
   }
}