题面 :
题意:
很简单,直接看图。
思路:
开始以为一个dfs暴力莽过,看讨论利用前缀和思想,先预处理每一点向四个方向有最多多少个连续点,取最小值即答案。时间复杂性O(n^2)。具体见代码!
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 500+50;
int n,mat[maxn][maxn];
int Up[maxn][maxn],Down[maxn][maxn],Left[maxn][maxn],Right[maxn][maxn];
int main(){
while(~scanf("%d",&n)){
memset(Up,0,sizeof(Up));
memset(Down,0,sizeof(Down));
memset(Left,0,sizeof(Left));
memset(Right,0,sizeof(Right));
for(int i=1;i<=n;i++){
char s[n+1];
scanf("%s",s+1);
for(int j=1;j<=n;j++){
mat[i][j] = s[j] - '0';
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(mat[i][j]==1){
Left[i][j] = Left[i][j-1] + 1;
Up[i][j] = Up[i-1][j] + 1;
}
if(mat[i][n-j+1]==1){
Right[i][n-j+1] = Right[i][n-j+2] + 1;
}
if(mat[n-i+1][j] == 1){
Down[n-i+1][j] = Down[n-i+2][j] + 1;
}
}
}
int ans = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(mat[i][j]==1){
ans = max(ans,min(min(Up[i][j],Down[i][j]), min(Left[i][j],Right[i][j]))-1);
}
}
}
printf("%d\n",ans);
}
}