-
Notifications
You must be signed in to change notification settings - Fork 6
/
Maximum borders.cpp
62 lines (57 loc) · 1.35 KB
/
Maximum borders.cpp
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
/*
You are given a table with n rows and m columns. Each cell is colored with white or black. Considering the shapes created by black cells, what is the maximum border of these shapes?
A shape is a set of connected cells. Two cells are connected if they share an edge. Note that no shape has a hole in it.
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 14;
char c[maxn][maxn];
int main(){
int t;
cin >> t;
while(t--){
int n, m;
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>(c[i]+1);
int ans =0;
for(int i=1;i<=n;i++)
for(int j=1,ptr=1;j<=m;j=ptr)
if(c[i][j]!='#'||c[i-1][j]=='#')
ptr++;
else{
while(c[i][ptr]=='#'&&c[i-1][ptr]!='#')
ptr++;
ans=max(ans,ptr-j);
}
for(int i=1;i<=n;i++)
for(int j=1,ptr=1;j<=m;j=ptr)
if(c[i][j]!='#'||c[i+1][ptr]=='#')
ptr++;
else{
while(c[i][ptr]=='#'&&c[i+1][ptr]!='#')
ptr++;
ans=max(ans,ptr-j);
}
for(int i=1;i<=m;i++)
for(int j=1,ptr=1;j<=n;j=ptr)
if(c[j][i]!='#'||c[j][i-1]=='#')
ptr++;
else{
while(c[ptr][i]=='#'&&c[ptr][i-1]!='#')
ptr++;
ans=max(ans,ptr-j);
}
for(int i=1;i<=m;i++)
for(int j=1,ptr=1;j<=n;j=ptr)
if(c[j][i]!='#'||c[j][i+1]=='#')
ptr++;
else{
while(c[ptr][i]=='#'&&c[ptr][i+1]!='#')
ptr++;
ans=max(ans,ptr-j);
}
cout<<ans<<'\n';
}
}