开始没看到要求对角线以外的地方不能是0,以为是个xx题。。。照着题解思路写的,很妙啊
题意:给定01矩阵
求矩阵中最长的只有对角线是1的正方形的对角线长度
x[i][j]从(i,j)向左/右(不包括(i,j))的连续的0的数量
y[i][j]从(i,j)向上(不包括(i,j))的连续的0的数量
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int eps = 1e-8; 5 typedef long double LD; 6 typedef pair<int, int> pii; 7 #define lop(i,a,b) for(register int i = (a); i <= (b); ++i) 8 #define dlop(i,a,b) for(register int i = (a); i >= (b); --i) 9 #define bl(i) for(int i = head[u]; i; i = G[i].next) 10 //char buf[50<<20], *p1=buf; 11 //#define getchar() (*p1++) 12 #define make_pair MP 13 inline int read(){ 14 register int c = getchar(), x = 0, f = 1; 15 while(!isdigit(c)){if (c == ‘-‘) f = -1; c = getchar();} 16 while(isdigit(c)) x = x*10+(c&15), c = getchar(); 17 return x * f; 18 } 19 int n, m, s, dp[2503][2505], x[2503][2505], y[2503][2505]; 20 bool matrix[2503][2505]; 21 int main(void){ 22 //freopen("data.in", "r", stdin); 23 //fread(buf,1,50<<20,stdin); 24 int ans = 0; 25 n = read(), m = read(); 26 lop(i,1,n) lop(j,1,m) { 27 matrix[i][j] = read(); 28 if (matrix[i][j]) dp[i][j] = min(dp[i-1][j-1], min(x[i][j-1], y[i-1][j]))+1, ans = max(ans, dp[i][j]); 29 else x[i][j] = x[i][j-1] + 1, y[i][j] = y[i-1][j] + 1; 30 } 31 memset(x,0,sizeof(x)); 32 memset(dp,0,sizeof(dp)); 33 lop(i,1,n) dlop(j,m,1) { 34 if (matrix[i][j]) dp[i][j] = min(dp[i-1][j+1], min(x[i][j+1], y[i-1][j]))+1, ans = max(ans, dp[i][j]); 35 else x[i][j] = x[i][j+1] + 1; 36 } 37 cout << ans; 38 return 0; 39 }