-
Notifications
You must be signed in to change notification settings - Fork 0
/
Largest subsquare surrounded by X
70 lines (55 loc) · 1.85 KB
/
Largest subsquare surrounded by X
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
package gfg;
//Largest subsquare surrounded by X
import java.io.*;
import java.util.*;
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0) {
int N = Integer.parseInt(read.readLine());
char A[][] = new char[N][N];
for (int i = 0; i < N; i++) {
String S[] = read.readLine().trim().split(" ");
for (int j = 0; j < N; j++) A[i][j] = S[j].charAt(0);
}
Solution ob = new Solution();
System.out.println(ob.largestSubsquare(N, A));
}
}
}
class Solution {
int largestSubsquare(int n, char a[][]) {
int[][] left = new int[n][n];
int[][] top = new int[n][n];
int ans =0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(a[i][j] == 'X') {
if(i != 0) {
top[i][j] = top[i - 1][j] + 1;
}
else {
top[i][j] = 1;
}
if(j != 0) {
left[i][j] = left[i][j - 1] + 1;
}
else {
left[i][j] = 1;
}
}
int minX = Math.min(top[i][j], left[i][j]);
while(minX != 0) {
int k = j - minX + 1;
int l = i - minX + 1;
if(top[i][k] >= minX && left[l][j] >= minX) {
ans = Math.max(ans, minX);
}
minX--;
}
}
}
return ans;
}
};