B. OR in Matrix
1 second
256 megabytes
standard input
standard output
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.
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).
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.
2 2
1 0
0 0
NO
2 3
1 1 1
1 1 1
YES
1 1 1
1 1 1
2 3
0 1 0
1 1 1
YES
0 0 0
0 1 0
wa了几发才过、
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
using namespace std; int a[110][110], b[110][110]; int main() {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
int m, n;
cin >> m>> n;
for (int i = 0; i<m; i++) {
for (int j = 0; j<n; j++) {
cin >> a[i][j];
b[i][j] = 1;
}
}
if (m == n && m == 1) {
cout << "YES" << endl;
cout << a[0][0] << endl;
return 0;
}
int flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 1;
for (int i = 0; i<m; i++) {
for (int j = 0; j<n; j++) {
if (a[i][j] == 1) {
flag4 = 0;
flag1 = 0, flag2 = 0;
for (int k = 0; k<n; k++) {
if (a[i][k] != 1)
flag1 ++ ;
}
for (int k = 0; k<m; k++) {
if (a[k][j] != 1)
flag2 ++ ;
}
if (flag1 == 0 && flag2 == 0)
flag3 ++ ;
else if (flag1>0 && flag2>0) {
cout << "NO"<< endl;
return 0;
}
}
}
}
if (flag4 == 1) {
cout << "YES" << endl;
for (int i = 0; i<m; i++) {
for (int j = 0; j<n-1; j++) {
cout << a[i][j] << " ";
}
cout << a[i][n-1] << endl;
}
cout << endl;
return 0;
}
if (flag3 > 0) {
cout << "YES"<< endl;
for (int i = 0; i<m; i++) {
for (int j = 0; j<n; j++) {
if (a[i][j] == 0) {
for (int k = 0; k<n; k++)
b[i][k] = 0;
for (int k = 0; k<m; k++)
b[k][j] = 0;
}
}
}
for (int i = 0; i<m; i++) {
for (int j = 0; j<n-1; j++) {
cout << b[i][j] << " ";
}
cout << b[i][n-1] << endl;
}
cout << endl;
}
else {
cout << "NO" << endl;
} return 0;
}
B. OR in Matrix的更多相关文章
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- Atitit Data Matrix dm码的原理与特点
Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...
- Android笔记——Matrix
转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...
- 通过Matrix进行二维图形仿射变换
Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Set Matrix Zeroes 矩阵赋零
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...
随机推荐
- C++11新语法糖之尾置返回类型
C++11的尾置返回类型初衷是为了方便复杂函数的声明和定义,但是当复杂度稍微提升一些的时候很明显能注意到这种设计的作用微乎其微. 首先考虑如下代码: C++ //返回指向数组的指针 auto func ...
- Oracle数据库部分迁至闪存存储方案
Oracle数据库部分迁至闪存存储方案 1.实施需求 2.确认迁移表空间信息 3.确认redo信息 4.确认undo信息 5.表空间迁移到闪存 6.redo迁移到闪存 7.undo迁移到闪存 8.备库 ...
- Java I/O---File类
1.File概述 File(文件)类可能有一定的误导性,它不仅仅指代的是文件.它既能代表一个特定的文件的名称,又能代表一个目录下的一组文件集的名称,这样就可以用集合List方法调用和遍历.实际上Fil ...
- headfirst设计模式(4)—工厂模式
开篇 天天逛博客园,就是狠不下心来写篇博客,忙是一方面,但是说忙能有多忙呢,都有时间逛博客园,写篇博客的时间都没有?(这还真不好说) 每次想到写一篇新的设计模式,我总会问自己: 1,自己理解了吗? 2 ...
- bzoj 2109: [Noi2010]Plane 航空管制
Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频 发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此, 小X表示很不满意. 在这次来烟台的 ...
- Python函数篇(7)-正则表达式
1.正则表达式 正则表达式为高级的文本模式匹配,抽取,与/或文本形式的搜索和替换功能提供了基础,简单的来说,正则表达式是由一些字符和特殊符号组成的字符串.Python通过标准库中的re模块来支持正 ...
- Android与javascript中事件分发机制的简单比较
在前面两篇博客中,我们讨论了Android中的事件分发的相关内容,那么在本篇博客当中,我们就简单探讨一下html或javascript中的事件分发机制,并进行简单的对比. 在前端中,对事件进行绑定有三 ...
- Robot Framework 学习笔记(二)-------第一个脚本
robot Framework环境搭建好之后先来一个简单的脚本跑一下 一.新建项目 二.新建测试套件 三.创建测试用例 四.导入Selenium2Library库 因为RF框架编写基于web 的测试 ...
- [js高手之路] vue系列教程 - 事件专题(4)
本文主要讲解事件冒泡,事件绑定的简写,事件默认行为,按键码等一系列与事件相关的知识. 一.事件绑定的简写,@事件类型. 之前我的[js高手之路] vue系列教程 - vue的事件绑定与方法(2) 用 ...
- php isset和empty方法的区别
我总结了下面几点区别,直接上代码: empty方法: 变量不存在,返回true 变量存在,值为空,返回true 变量存在,值不为空,返回false function empty1(){ //变量不存在 ...