-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva108_CompleteSearch.java
More file actions
60 lines (56 loc) · 1.7 KB
/
Uva108_CompleteSearch.java
File metadata and controls
60 lines (56 loc) · 1.7 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
import java.util.*;
import java.io.*;
public class Uva108_CompleteSearch {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr = new PrintWriter(System.out);
int N = Integer.parseInt(br.readLine());
int [][] a = new int[N][N];
int i = 0 , j = 0 ;
while (br.ready()){
StringTokenizer st = new StringTokenizer(br.readLine());
while (st.hasMoreTokens()){
if(j==N){
i++ ;
j = 0 ;
}
a[i][j++] = Integer.parseInt(st.nextToken());
}
}
int max = Integer.MIN_VALUE ;
for(i = 1 ; i <= N ; i++)
{
for(j = 1 ; j <= N ; j++)
{
int m = maxSubRectangleSum(a,N,i,j);
max = Integer.max(max,m);
}
}
pr.println(max);
pr.close();
br.close();
}
static int maxSubRectangleSum(final int [][] rectangle , int N , int n , int m)
{
int max = Integer.MIN_VALUE ;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if( i + n <= N && j + m <= N )
{
int sum = 0 ;
for(int y = i ; y < i+n ; y++)
{
for(int x = j ; x < j+m ; x++)
{
sum += rectangle[y][x];
}
}
max = Integer.max(max,sum);
}
}
}
return max ;
}
}