入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

Matrix

创建时间:2018-10-13 投稿人: 浏览次数:252
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lzyws739307453/article/details/83040849

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description

Give you a matrix(only contains 0 or 1),every time you can select a row or a column and delete all the "1" in this row or this column .

Your task is to give out the minimum times of deleting all the "1" in the matrix.

Input

There are several test cases.

The first line contains two integers n,m(1<=n,m<=100), n is the number of rows of the given matrix and m is the number of columns of the given matrix.
The next n lines describe the matrix:each line contains m integer, which may be either ‘1’ or ‘0’.

n=0 indicate the end of input.

Output

For each of the test cases, in the order given in the input, print one line containing the minimum times of deleting all the "1" in the matrix.

Sample Input

3 3
0 0 0
1 0 1
0 1 0
0

Sample Output

2

Solving Ideas

题意概括: 为您提供一个01矩阵,你可以选择一行或一列删除此行或此列中的所有"1",求删除矩阵中所有"1"的最少操作。

解题思路: 最小点覆盖,直接套代码。

#include <stdio.h>
#include <string.h>
#define N 110
int map[N][N], vis[N], match[N], n;
int dfs(int u)
{
    for (int i = 1; i <= n; i++)
    {
        if (!vis[i] && map[u][i])
        {
            vis[i] = 1;
            if (!match[i] || dfs(match[i]))
            {
                match[i] = u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int m, a, b, ans;
    while (scanf("%d", &m), m)
    {
    	ans = 0;
        scanf("%d", &n);
        memset(map, 0, sizeof(map));
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                scanf("%d", &map[i][j]);
        memset(match, 0, sizeof(match));
        for (int i = 1; i <= m; i++)
        {
            memset(vis, 0, sizeof(vis));
            if (dfs(i))
                ans++;
        }
        printf("%d
", ans);
    }
    return 0;
}
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像