-
Notifications
You must be signed in to change notification settings - Fork 0
/
ORMatrix.java
57 lines (53 loc) · 1.45 KB
/
ORMatrix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* https://codeforces.com/problemset/problem/486/B #greedy create 1 temporary result based on input
* -> verify that result based on re-creating input matrix.
*/
import java.util.Arrays;
import java.util.Scanner;
public class ORMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int[][] arr = new int[m + 1][n + 1];
// create a temp result
int[][] result = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) Arrays.fill(result[i], 1);
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
arr[i][j] = sc.nextInt();
if (arr[i][j] == 0) {
for (int k = 1; k <= m; k++) {
result[k][j] = 0;
}
for (int k = 1; k <= n; k++) {
result[i][k] = 0;
}
}
}
}
// check re-create arr from result;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
int tmp = 0;
for (int k = 1; k <= m; k++) {
tmp |= result[k][j];
}
for (int k = 1; k <= n; k++) {
tmp |= result[i][k];
}
if (arr[i][j] != tmp) {
System.out.println("NO");
return;
}
}
}
System.out.println("YES");
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}