304 North Cardinal St.
Dorchester Center, MA 02124

OR Matrix CodeChef Solution

OR Matrix CodeChef Solution in C++14

``````#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--){
int n,m;
cin >> n >> m;
vector<bool> rowOne(n, false);
vector<bool> columnOne(m, false);
bool onePresent = false;
vector<vector<int>> arr(n, vector<int>(m, 0));
for(int i = 0;i < n;i++){
string s;
cin >> s;
for(int j = 0;j < m;j++){
if(s[j] == '1'){
onePresent = true;
columnOne[j] = true;
rowOne[i] = true;
arr[i][j] = 1;
}
}
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(arr[i][j] == 1){
cout << 0 << " ";
}
else if(columnOne[j] || rowOne[i]){
cout << 1 << " ";
}
else if(onePresent) cout << 2 << " ";
else cout << "-1" << endl;
}
cout << endl;
}
}
return 0;
}``````

OR Matrix CodeChef Solution in PYTH 3

``````# cook your dish here
t = int(input())
for _ in range(t):
n,m = map(int,input().split())
arr = []
for _ in range(n):
s = input()
arr.append(s)
ans = [[-1]*(m) for _ in range(n)]
row = [0]*(n)
col = [0]*(m)
for i in range(n):
for j in range(m):
if arr[i][j]=='1':
row[i]=1
col[j] =1
if sum(row)+sum(col)!=0:
for i in range(n):
for j in range(m):
if arr[i][j]=='1':
ans[i][j] = 0
else:
if row[i]==1 or col[j]==1:
ans[i][j] =1
else:
ans[i][j] =2
for item in ans:
print(*item)``````

OR Matrix CodeChef Solution in C

``````#include <stdio.h>
int main(void) {
int t;
scanf("%d", &t);
char arr[1001][1001];
while(t--){
int arr1[1001] = {0};
int arr2[1001] = {0};
int n, m;
scanf("%d%d\n", &n, &m);
int check = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
scanf("%c", &arr[i][j]);
if(arr[i][j] == '1'){
arr1[i] = 1;
arr2[j] = 1;
check = 1;
}
}
scanf("\n");
}
// for(int i = 0; i < n; i++){
//     for(int j = 0; j < m; j++){
//         printf("%c", arr[i][j]);
//     }
//     printf("\n");
// }
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(check == 0){
printf("-1 ");
}else{
if(arr[i][j] == '1'){
printf("0 ");
}else if(arr1[i] == 1 || arr2[j] == 1){
printf("1 ");
}else{
printf("2 ");
}
}
}
printf("\n");
}

}
return 0;
}``````

OR Matrix CodeChef Solution in JAVA

``````/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{

StringTokenizer st;

{
}

String next()
{
while (st == null || !st.hasMoreElements()) {
try {
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}

int nextInt() { return Integer.parseInt(next()); }

long nextLong() { return Long.parseLong(next()); }

double nextDouble()
{
return Double.parseDouble(next());
}

String nextLine()
{
String str = "";
try {
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static boolean prime[]= new boolean[10000001];
public static void  sieve()
{

for(int i=0;i<=10000000;i++)
prime[i] = true;
prime[0] = prime[1] = false;
for(int p = 2; p*p <=10000000; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[p] == true)
{
// Update all multiples of p
for(int i = p*p; i <= 10000000; i += p)
prime[i] = false;
}
}

}
static ArrayList<Integer> primeFactors(int n)
{
ArrayList<Integer> pflist=new ArrayList<>();
int c = 2;
while (n > 1) {
if (n % c == 0) {
n /= c;
}
else
c++;
}
return pflist;
}

public static void main (String[] args) throws java.lang.Exception
{
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

int cases = sc.nextInt();
//sieve();
while(cases-->0)
{
int n = sc.nextInt();
int m=sc.nextInt();
int[] row=new int[n];
int[] col=new int[m];
String[] s=new String[n];
for(int i=0;i<n;i++)
{
s[i]=sc.next();
for(int j=0;j<m;j++)
{
if(s[i].charAt(j)=='1')
{
row[i]=1;
col[j]=1;
}
}
}
int sum=0;
int sum1=0;
for(int i=0;i<n;i++) sum+=row[i];
for(int i=0;i<m;i++) sum1+=col[i];
if(sum+sum1==0)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
out.write("-1 ");
}
out.write("\n");
}
continue;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(s[i].charAt(j)=='1')
{
out.write(0+" ");
}
else{
if(row[i]==1 || col[j]==1)
{
out.write(1+" ");
}
else
{
out.write(2+" ");
}
}

}
out.write("\n");
}

}
out.flush();
}
public static void reverse(int[] array)
{
int n=array.length;
for(int i=0;i<n/2;i++)
{
int temp=array[i];
array[i]=array[n-i-1];
array[n-i-1]=temp;
}
}
public static String dectobin32(int x)
{
StringBuilder result = new StringBuilder();

for (int i = 31; i >= 0 ; i--)
{
int mask = 1 << i;
result.append((x & mask) != 0 ? 1 : 0);
}
//Integer.parseInt(result,2)
return result.toString();
}

}
class Pair implements Comparable<Pair> {
int first,second;

public Pair(int first,int second)
{
this.first =first;
this.second=second;
}
public int compareTo(Pair b)
{
//first element in descending order
if (this.first!=b.first)
return (this.first>b.first)?-1:1;
else
return this.second<b.second?-1:1;
//second element in incresing order
}
}``````

OR Matrix CodeChef Solution in PYPY 3

``````for _ in range(int(input())):
n,m = map(int,input().split())
arr=[]
for _ in range(n):
s = input()
arr.append(s)

res = [[-1]*(m) for i in range(n)]
r = [0]*n
col = [0]*m
for i in range(n):
for j in range(m):
if arr[i][j]=="1":
r[i]=1
col[j]=1

if sum(r) + sum(col)!=0:
for i in range(n):
for j in range(m):
if arr[i][j]=="1":
res[i][j] = 0
else:
if r[i]==1 or col[j]==1:
res[i][j] = 1
else:
res[i][j] = 2
for i in res:
print(*i)``````

OR Matrix CodeChef Solution in PYTH

``````t = int(raw_input())
for i in range(t):
st = raw_input().split()
N = int(st[0])
M = int(st[1])
A = [[0 for x in range(M+1)] for y in range(N+1)]
v = 0
for y in range(1,N+1):
st = raw_input().strip()
for x in range(1,M+1):
if st[x-1] == '1':
A[y][x] = 1
A[0][x] = 1
A[y][0] = 1
v = 1
# endif
# endfor x
# endfor y
if v == 0:
st = ''
for x in range(M):
st += '-1 '
# endfor x
for k in range(N):
print st
# endfor k
else:
for y in range(1,N+1):
st = ''
for x in range(1,M+1):
n = 0
if A[y][x] == 0:
if A[y][0]+A[0][x] > 0:
n = 1
else:
n = 2
# endif
# endif
st += str(n) + ' '
# endfor x
print st
# endfor y
# endif
# endfor i
``````

OR Matrix CodeChef Solution in C#

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;

namespace July18
{
class Program
{
static void Main(string[] args)
{
int T;

// Console.WriteLine("{0} s", (DateTime.Now - Start).TotalSeconds);

for (int i = 1; i <= T; i++)
{
int N, M;

N = Convert.ToInt32(inpargs[0]);

M = Convert.ToInt32(inpargs[1]);

int[,] A = new int[N,M];

int[] AccessOnRows = new int[M];

int[] AccessOnColumns = new int[N];

for (int j = 1; j <= N; j++)
{
for (int k = 1; k <= M; k++)
{
A[j - 1, k - 1] = Convert.ToInt32(rowstr[k - 1] - '0');
if (A[j - 1, k - 1] > 0)
AccessOnRows[k - 1] = AccessOnColumns[j - 1] = 1;
}
}

int isAnyOne = 0;

for (int j = 1; j <= N; j++)
if (AccessOnColumns[j - 1] > 0)
{
isAnyOne = 1;
break;
}

if (isAnyOne > 0)
{
// int[,] MinMovesToGetOnes = new int[N, M];

for (int j = 1; j <= N; j++)
{
string outline = "";
for (int k = 1; k <= M - 1; k++)
if (A[j - 1, k - 1] == 0)
{
if ((AccessOnRows[k - 1] > 0) || (AccessOnColumns[j - 1]) > 0)
outline += "1 ";
else
outline += "2 ";
}
else
outline += "0 ";

if (A[j - 1, M - 1] == 0)
{
if ((AccessOnRows[M - 1] > 0) || (AccessOnColumns[j - 1]) > 0)
outline += "1";
else
outline += "2";
}
else
outline += "0";

Console.WriteLine("{0}", outline);

}
}
else
{
for (int j = 1; j <= N; j++)
{
for (int k = 1; k <= M - 1; k++)
Console.Write("{0} ", -1);

Console.WriteLine("{0}", -1);
}
}
}

}
}
}``````
OR Matrix CodeChef Solution Review:

In our experience, we suggest you solve this OR Matrix CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the OR Matrix CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this OR Matrix CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!