首页 > 其他 > 详细

HDOJ Matrix

时间:2015-03-21 09:54:25      阅读:304      评论:0      收藏:0      [点我收藏+]

Matrix

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2112    Accepted Submission(s): 932


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

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
 

Author
Wendell
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int map[110][110],vis[110];
int N,M,point[110];
bool DFS(int a){
    for(int i=0;i<M;++i){
        if(!vis[i]&&map[a][i]){
            vis[i]=1;
            if(point[i]<0||DFS(point[i])){
                point[i]=a;
                return true;
            }
        }
    }
    return false ;
}
int count(){
    int ans=0;
    for(int i=0;i<N;++i){
        memset(vis,0,sizeof(vis));
        if(DFS(i)){
            ans++;
        }
    }
    return ans;
}
int main()
{
    int i,j;
    while(scanf("%d",&N),N){
        cin>>M;
        memset(point,-1,sizeof(point));
        memset(map,0,sizeof(map));
        for(i=0;i<N;++i){
            for(j=0;j<M;++j){
                cin>>map[i][j];
            }
        }
        cout<<count()<<endl;
    }
    return 0;
}


 

HDOJ Matrix

原文:http://blog.csdn.net/r1986799047/article/details/44497443

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!