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 ...
随机推荐
- 使用Libmicrohttpd搭建内嵌(本地)服务器
Libmicrohttpd简介 GNU Libmicrohttpd是一个用来在项目中内嵌http服务器的C语言库,它具有以下几个非常鲜明的特点: C语言库,小而快. API非常简单,且都是可重入的. ...
- Mac下nvm管理node.js版本问题
本篇文章主要是针对已经安装了node.js和nvm管理工具小伙伴遇到的问题. 管理工具有两个,一个是nvm,还有一个是nnvm的好处就是可以管理多个node版本,而且可以切换想要的版本,可以安装一个稳 ...
- centOS7 mini配置linux服务器(五) 安装和配置tomcat和mysql
配置java运行环境,少不了服务器这一块,而tomcat在服务器中占据了很大一部分份额,这里就简单记录下tomcat安装步骤. 下载 首先需要下载tomcat7的安装文件,地址如下: http://t ...
- Notepad++使用教程
Notepad++ 快捷键 大全 Ctrl+C 复制Ctrl+X 剪切Ctrl+V 粘贴Ctrl+Z 撤消Ctrl+Y 恢复Ctrl+A 全选Ctrl+F 键查找对话框启动Ctrl+H 查找/替换对话 ...
- springCloud系列教程01:Eureka 注册中心集群搭建
springCloud系列教程包含如下内容: springCloud系列教程01:Eureka 注册中心集群搭建 springCloud系列教程02:ConfigServer 配置中心server搭建 ...
- 浅谈JavaScript的面向对象程序设计(一)
面向对象的语言有一个标志,他们都有类的概念,通过类可以创建多个具有相同属性和方法的对象.但是JavaScript中没有类的概念,因此JavaScript与其他的面向对象语言还是有一定区别的.JavaS ...
- Maven的下载、安装与环境配置
在创建一个项目时,搭建环境往往是编写具体代码的先决条件,而获取到所有需要的jar包是其中的重中之重.起初,人们在需要jar包的时候总会在网上四处查找,而且如果不知道某jar包版本的更迭,写出的代码或许 ...
- Robot Framework学习笔记(三)------常用关键字介绍
下面关键字全部由 Builtin 库提供,Builtin 为 Robot Framework 标准类库.Builtin库提供常用的关键字 1.log log 关键字就是编程语言里的"prin ...
- mac SecureCRT设置
参考: http://www.2cto.com/os/201407/320292.html SecureCRT 设置 1)每次登陆都要输入密码: Global Option -> General ...
- TurnipBit之DIY无线遥控智能小车
一.准备工作 TurnipBit 开发板 2块 TurnipBit 扩展板 1块 数据线 1条 智能小车器件 1套 电机驱动模块(L298N) 1个 在线可视化编程 点击进入 二.思路设计 2 ...