Matrix
发布日期:2021-07-01 00:14:28 浏览次数:2 分类:技术文章

本文共 1765 字,大约阅读时间需要 5 分钟。

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 
#include
#define N 110int 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\n", ans); } return 0;}

转载地址:https://lzyws739307453.blog.csdn.net/article/details/83040849 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:棋盘游戏
下一篇:Girls and Boys

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年05月02日 02时27分15秒