OR in Matrix

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

Let‘s define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:

 where  is equal to 1 if some ai = 1, otherwise it is equal to 0.

Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:

.

(Bij is OR of all elements in row i and column j of matrix A)

Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.

Input

The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively.

The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).

Output

In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.

Sample Input

Input

2 21 00 0

Output

NO

Input

2 31 1 11 1 1

Output

YES1 1 11 1 1

Input

2 30 1 01 1 1

Output

YES0 0 00 1 0
/*
题意:定义一个异或,a1^a2...^an如果有一个ai=1那么值为1,否则为零,给出你一个矩阵B,是由矩阵A得来的,Bij等于A的i行元素
    异或j列元素。给出你矩阵B问你是否有这样的矩阵A

初步思路:将矩阵初始化为1,然后先按照矩阵B中有零的元素,将对应A矩阵中的元素设置成零,然后在反过来验证B矩阵
*/
#include <bits/stdc++.h>
using namespace std;
int n,m;
int a[110][110];
int b[110][110];
int main(){
    // freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%d",&b[i][j]);
            a[i][j]=1;
        }
    }
    //按照B矩阵进行置0
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(b[i][j]==0){
                for(int k=1;k<=m;k++)
                    a[i][k]=0;
                for(int k=1;k<=n;k++)
                    a[k][j]=0;
            }
        }
    }
    //验证然后按照1的位置验证B矩阵
    bool f=false;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(b[i][j]==1){
                bool flag=false;
                for(int k=1;k<=n;k++){
                    if(a[i][k]==1){
                        flag=true;
                        break;
                    }
                }
                if(flag==false)
                for(int k=1;k<=m;k++){
                    if(a[k][j]==1){
                        flag=true;
                        break;
                    }
                }
                if(flag==false){
                    f=true;
                    break;
                }
            }
        }
    }
    if(f){
        puts("NO");
    }else{
        puts("YES");
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                printf(j==1?"%d":" %d",a[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
时间: 2024-10-11 06:14:24

OR in Matrix的相关文章

hdu 5015 233 Matrix (矩阵快速幂)

题意: 有一种矩阵,它的第一行是这样一些数:a  0,0 = 0, a 0,1 = 233,a 0,2 = 2333,a 0,3 = 23333... 除此之外,在这个矩阵里, 我们有 a i,j = a i-1,j +a i,j-1( i,j ≠ 0).现在给你 a 1,0,a 2,0,...,a n,0, 你能告诉我a n,m 是多少吗? n,m(n ≤ 10,m ≤ 10 9)输出 a n,m mod 10000007. 思路:首先我们观察n和m的取值范围,会发现n非常小而m却非常大,如果

【数组】Spiral Matrix II

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 思路: 本质上和上一题是一样的,这里我们要用数字螺旋的去填充矩阵.同理,我们也是逐个环

Spiral Matrix(LintCode)

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 难得的一次AC! 虽然感觉题

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

HDU 2686 Matrix(最大费用流)

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1890    Accepted Submission(s): 1005 Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer numbe

UVA 11992(Fast Matrix Operations-线段树区间加&amp;改)[Template:SegmentTree]

Fast Matrix Operations There is a matrix containing at most 106 elements divided into r rows and c columns. Each element has a location (x,y) where 1<=x<=r,1<=y<=c. Initially, all the elements are zero. You need to handle four kinds of operati

U3D开发者福利 MATRIX : UNITY 游戏技术咨询免费开放

UNITE 2015 BEIJING 于2015年4月18日-20日,在北京国家会议中心隆重举行.在这场被媒体誉为"行业风向标"的大会上,Unity 大中华区总裁符国新提到2015年Unity 将在全球范围内着重发展线上增值服务,并宣布Unity 将在大中华区开启"Matrix 游戏技术咨询". Matrix -最专业的游戏技术咨询平台 Matrix 是由Unity 大中华区的技术咨询团队研发的,旨在帮助游戏团队更加方便.准确地定位和解决游戏开发过程中所遇到的性能问

LeetCode—*Spiral Matrix问题,主要是用到了方向矩阵,很创意

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 这是一个螺旋排序的问题 这里遇到一个比较巧妙的

POJ 3422 kaka&#39;s matrix trvals(费用流)

#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <cma