图中的最大匹配

发布于 2024-12-17 00:38:39 字数 684 浏览 0 评论 0原文

我有一个有趣的问题:
我的程序必须找到 1 的最大数量。 但这还不是全部! 如果程序“看到”1,那么它应该清除1所在的整个列和行。

我遇到的问题:
我找不到 1 的最大数量,我不知道该怎么做。

我给你举了一个小例子,希望你能清楚。该程序必须像这样工作:

有一个矩阵:

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

程序找到1(位置[0][0]我已用黑色突出显示它),并清除行和列:

Example 2

在此之后我们找到下一个 1、清除行列等:

Example 3

最后,程序应打印黑色单元格的数量。

在我的示例中,它是 4

如何在 C++ 代码中执行此操作?请帮我!谢谢。

I have an interesting problem:
My program must to find the maximum number of 1.
But that's not all!.
If the program has "seen" 1, then it should clear the entire column and row in which the 1 is located.

The problem I have:
I can not find the maximum number of 1, I do not know how to do that.

For you I made a small example, I hope it will be clear to you. The program must work like this:

There is a matrix:

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

The program found 1 (position [0][0] I've highlighted it in black), and cleared the row and column:

Example 2

After this we find the next 1, cleared the row and columnand so on:

Example 3

At the end, the program should print the number of black cells.

In my example it's 4

How to do it in C++ code? Please help me! Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烂柯人 2024-12-24 00:38:39

我更喜欢这样做(参见下面的代码):使用两个“for”循环,并在第二个循环中使用条件“if”添加第三个“for”循环以设置为 0。

for(int i=0;i<m;i++)
    for(int j=0;j<n;j++)
    {
        if(cow[j][i]==1)
        {
            cnt++;
            for(int k=0;k<n;k++)
                cow[k][i]=cow[j][k]=0;
            break;
        }
    }

I prefer to do it like this (see code below): Use two "for" loops and inside the second use conditional "if" to add the third "for" loop to set to 0.

for(int i=0;i<m;i++)
    for(int j=0;j<n;j++)
    {
        if(cow[j][i]==1)
        {
            cnt++;
            for(int k=0;k<n;k++)
                cow[k][i]=cow[j][k]=0;
            break;
        }
    }
乄_柒ぐ汐 2024-12-24 00:38:39

目前尚不清楚如何在矩阵中搜索“下一个”1,以及矩阵是否只能包含 0 和 1。但是,如果“下一个”有明确的定义,那么您只需按照您所描述的方式进行编码即可多于。可能的代码片段如下所示(未经测试,甚至未编译):

 bool find_next_one(int&x, int&y, matrix const&M)
 {
   // next is in (col,row) order
   for(; x!=M.size(0); ++x)
     for(; y!=M.size(1); ++y)
       if(M(x,y)==1) return 1;
   return 0;
 }
 int count_one(matrix const&M_original)
 {
   matrix M(M_original); // make copy where we can set elements to 0
   int count=0;
   int x=0,y=0;
   while(find_next_one(x,y,M)) {
     ++count;
     for(int i=0; i!=M.size(1); ++i) M(x,i) = 0;
     for(int i=0; i!=M.size(0); ++i) M(i,y) = 0;
   }
   return count;
 }

it's not clear how you search for the 'next' 1 in your matrix and if the matrix can only contain 0 and 1. But if there is a clear definition of what 'next' is, then you just code exactly as you have described it above. A possible code snippet looks like this (not tested, not even compiled):

 bool find_next_one(int&x, int&y, matrix const&M)
 {
   // next is in (col,row) order
   for(; x!=M.size(0); ++x)
     for(; y!=M.size(1); ++y)
       if(M(x,y)==1) return 1;
   return 0;
 }
 int count_one(matrix const&M_original)
 {
   matrix M(M_original); // make copy where we can set elements to 0
   int count=0;
   int x=0,y=0;
   while(find_next_one(x,y,M)) {
     ++count;
     for(int i=0; i!=M.size(1); ++i) M(x,i) = 0;
     for(int i=0; i!=M.size(0); ++i) M(i,y) = 0;
   }
   return count;
 }
太阳公公是暖光 2024-12-24 00:38:39

注意到这看起来像矩阵奇点类型检查 - 特别是如果 1 和 0 是唯一要使用的东西。

您可以检查矩阵的行列式。非零意味着它等于行数和列数(如果矩阵始终是方阵)。如果 det(0),则使用您想要将矩阵简化为简化形式的任何技术来查看有多少 0 列你有 - 或者只是先进行归约,然后沿着对角线计数。

哎呀,按附加值对列进行排序,将为您提供对角线形式。这将使检查 0 列变得非常容易。

Noticed this looks like a matrix singularity type check - especially if 1s and 0s are the only thing to be used.

You can check the determinate of the matrix. Non zero means it is equal to the number of rows and columns (if the matrix is always square.) If det(0), then use any technique you want to bring the matrix down to reduced form to see how many 0'd columns you have - or just do the reduction first and walk down the diagonal counting.

Heck sorting the columns by their added value, will put it in diagonal form for you. That would make it pretty easy also to check for 0 columns.

半寸时光 2024-12-24 00:38:39

我不会为您编写所有代码,但会提出一些建议来帮助您走上正轨。您应该了解如何迭代二维数组(矩阵)以及如何迭代该矩阵中的单个行或列。

给定一个(硬编码的)矩阵定义,如下所示:

struct Matrix4x4
{
    int        m[4][4];
};

要迭代所有元素,您需要编写如下内容:

   Matrix4x4 matrix;

   for (size_t row = 0; row < 4; ++row)
   {
       for (size_t col = 0; col < 4; ++col)
       {
           // do something with 'matrix.m[row][col]'
       }
   }

这将从左上角 (0,0) 到右下角 (3,3) 迭代矩阵。我假设这是您被告知要使用的遍历顺序。

要处理一行,您需要编写如下内容:

   void FunctionThatOperatesOnARow(Matrix4x4& matrix, size_t row)
   {
       for (size_t col = 0; col < 4; ++col)
       {
           // do something with 'matrix.m[row][col]'
       }
   }

要处理一列,您需要编写如下内容:

   void FunctionThatOperatesOnAColumn(Matrix4x4& matrix, size_t col)
   {
       for (size_t row = 0; row < 4; ++row)
       {
           // do something with 'matrix.m[row][col]'
       }
   }

现在您需要做的是修改迭代所有元素的代码的第一位,并让它检查是否为 1然后需要调用相应的函数来清除当前的列和行(可以基于后两个示例)。

对于最终结果,您只需在每次检测到 1 时增加本地计数器变量即可。

I won't write all the code for you, but will suggest some things to get you on track. You should understand how to iterate over a two dimensional array (the matrix) and also how to iterate over a single row or column within that matrix.

Given a (hard coded) definition of matrix that looks like this:

struct Matrix4x4
{
    int        m[4][4];
};

To iterate over all elements you want to write something like this:

   Matrix4x4 matrix;

   for (size_t row = 0; row < 4; ++row)
   {
       for (size_t col = 0; col < 4; ++col)
       {
           // do something with 'matrix.m[row][col]'
       }
   }

This will iterate over your matrix from top left (0,0) to bottom right (3,3). I am assuming that this is the traversal order you have been told to use.

To process a row you want to write something like this:

   void FunctionThatOperatesOnARow(Matrix4x4& matrix, size_t row)
   {
       for (size_t col = 0; col < 4; ++col)
       {
           // do something with 'matrix.m[row][col]'
       }
   }

To process a column you want to write something like this:

   void FunctionThatOperatesOnAColumn(Matrix4x4& matrix, size_t col)
   {
       for (size_t row = 0; row < 4; ++row)
       {
           // do something with 'matrix.m[row][col]'
       }
   }

What you need to do now is modify the first bit of code that iterates over all elements and get it to check for a 1. You then need to call the appropriate functions to clear the current column and row (which you can base on the latter two examples).

For the final result you can simply increment a local counter variable each time you detect a 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文