-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBandwidthOfAMatrix.java
More file actions
96 lines (85 loc) · 2.15 KB
/
BandwidthOfAMatrix.java
File metadata and controls
96 lines (85 loc) · 2.15 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by hardCode on 3/5/2017.
*/
public class BandwidthOfAMatrix {
static boolean[][]a=new boolean[500][500];
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t,n,zeros,ones;
String []s;
t=Integer.parseInt(br.readLine());
while (t-->0){
zeros=ones=0;
n=Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
s=br.readLine().split("\\s");
for (int j = 0; j <n; j++) {
if (s[j].equals("1"))
a[i][j]=true;
else{
a[i][j]=false;
zeros++;
}
}
}
System.out.println(solve(a,n,zeros,ones));
}
}
private static int solve(boolean[][] a, int n,int zeros,int ones) {
int available,required,zerosTemp;
for (int bw = 0; bw < n; bw++) {
required=0;
available=0;
zerosTemp=0;
for (int i = 0; i < n - bw - 1; i++) {
for (int j = bw + i + 1; j < n; j++) {
if (a[i][j]) {
required++;
}
else {
zerosTemp++;
}
if (a[j][i]) {
required++;
}
else {
zerosTemp++;
}
}
}
if (bw==0){
for (int i = 0; i < n; i++) {
if (!a[i][i]){
available++;
}
}
}
else
available = zeros - zerosTemp;
if (required<=available)
return bw;
}
return 0;
}
}
/*
1
7
1111111
1111111
1111111
1111111
1111111
1111111
1111111
1
4
1111
1111
1111
1111
*/