题目类型 简单题
题目意思
给出一个 n*m 的只包含 '0' 或 '1'的字符矩阵 (1 <= n, m <= 25), 问只由 '0' 组成的矩阵中周长最长是多少
解题方法
直接暴力枚举最终矩阵的左上角 然后枚举矩阵的长和宽, 枚举长宽后再判断相应的矩形是否仅由 '0'组成, 如果是就更新结果
参考代码 - 有疑问的地方在下方留言 看到会尽快回复的
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue> using namespace std; typedef long long LL; const int MAXN = 1e2 + 10; const LL INF = 1LL<<62; char str[MAXN][MAXN]; int main() { int n, m; while(scanf("%d%d", &n, &m) != EOF) { for( int i=0; i<n; i++ ) scanf("%s", str[i]); int res = 0; for( int i=0; i<n; i++ ) { for( int j=0; j<m; j++ ) { if(str[i][j] == '1') continue; for( int k=1; k<=n-i; k++ ) { for( int h=1; h<=m-j; h++ ) { if((k + h)*2 <= res) continue; bool flag = true; for( int ti=0; ti<k && flag; ti++ ) { for( int tj=0; tj<h; tj++ ) { if(str[i+ti][j+tj] == '1') { flag = false; break; } } } if(flag) { res = max(res, (k + h)*2); } } } } } printf("%d\n", res); } return 0; }