Codeforces 486B OR in Matrix【水题】
2016-10-02 19:21
260 查看
B. OR in Matrix
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
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 mrows consisting of n integers
representing matrix A that can produce given matrix B.
If there are several solutions print any one.
Examples
input
output
input
output
input
output
题意:
一个新的矩阵的元素Bij为 矩阵A的第i行和A的第j列的元素相互按位与运算的结果。现在给你一个矩阵B,求矩阵A。如果存在输出YES,就把矩阵打印出来,否则输出NO。
思路:
当矩阵元素是0的时候最为特殊,现将储存答案的矩阵置为1,遇到交叉点是0的情况就把矩阵交叉点所在的行列全部置为0。处理结束后再查一遍1的位置是否合法。
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
//typedef long long LL;
//typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 1e5;
int c[110][110], b[110][110];
int main() {
int n, m;
while (scanf("%d%d", &n, &m) != EOF) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &b[i][j]);
c[i][j] = 1;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!b[i][j]) {
for (int k = 0; k < m; k++) {
c[i][k] = 0;
}
for (int k = 0; k < n; k++) {
c[k][j] = 0;
}
}
}
}
bool flag;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
flag = false;
if (b[i][j]) {
flag = true;
for (int k = 0; k < m; k++) {
if (c[i][k]) flag = false;
}
for (int k = 0; k < n; k++) {
if (c[k][j]) flag = false;
}
}
if (flag) break;
}
if (flag) break;
}
if (flag) printf("NO\n");
else {
printf("YES\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m - 1; j++) {
printf("%d ", c[i][j]);
}
printf("%d\n", c[i][m - 1]);
}
}
}
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
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 mrows consisting of n integers
representing matrix A that can produce given matrix B.
If there are several solutions print any one.
Examples
input
2 2 1 0 0 0
output
NO
input
2 3 1 1 1 1 1 1
output
YES 1 1 1 1 1 1
input
2 3 0 1 0 1 1 1
output
YES 0 0 0 0 1 0
题意:
一个新的矩阵的元素Bij为 矩阵A的第i行和A的第j列的元素相互按位与运算的结果。现在给你一个矩阵B,求矩阵A。如果存在输出YES,就把矩阵打印出来,否则输出NO。
思路:
当矩阵元素是0的时候最为特殊,现将储存答案的矩阵置为1,遇到交叉点是0的情况就把矩阵交叉点所在的行列全部置为0。处理结束后再查一遍1的位置是否合法。
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
//typedef long long LL;
//typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 1e5;
int c[110][110], b[110][110];
int main() {
int n, m;
while (scanf("%d%d", &n, &m) != EOF) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &b[i][j]);
c[i][j] = 1;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!b[i][j]) {
for (int k = 0; k < m; k++) {
c[i][k] = 0;
}
for (int k = 0; k < n; k++) {
c[k][j] = 0;
}
}
}
}
bool flag;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
flag = false;
if (b[i][j]) {
flag = true;
for (int k = 0; k < m; k++) {
if (c[i][k]) flag = false;
}
for (int k = 0; k < n; k++) {
if (c[k][j]) flag = false;
}
}
if (flag) break;
}
if (flag) break;
}
if (flag) printf("NO\n");
else {
printf("YES\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m - 1; j++) {
printf("%d ", c[i][j]);
}
printf("%d\n", c[i][m - 1]);
}
}
}
return 0;
}
相关文章推荐
- Codeforces 479A Expression(水题)
- 【CodeForces 618C】Constellation(几何水题)
- 【codeforces】A. Array(思维水题)
- CodeForces 534A-A - Exam-构造水题
- codeforces 672A A. Summer Camp(水题)
- Codeforces 75A:Life Without Zeros(水题)
- CodeForces 785B Anton and Classes【水题】
- Codeforces 599A Patrick and Shopping 【水题】
- Codeforces水题集合[14/未完待续]
- CodeForces 916A Jamie and Alarm Snooze (水题)
- Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)
- CodeForces-761A-Dasha and Stairs [水题]
- [codeforces]A - Brain's Photos 水题
- codeforces 686A A. Free Ice Cream(水题)
- 【打CF,学算法——一星级】CodeForces 74A Room Leader (水题)
- codeforces 690C1 C1. Brain Network (easy)(水题)
- 【打CF,学算法——二星级】CodeForces 374A Inna and Pink Pony (水题/坑题)
- Codeforces 626A Robot Sequence 【水题】
- Codeforces 233A Perfect Permutation(完美序列,水题)
- Codeforces 492A Vanya and Cubes【水题】