USACO 5.3 Big Barn(DP)
最大子矩阵问题,USACO上貌似好几个把。。
1 /* 2 ID: cuizhe 3 LANG: C++ 4 TASK: bigbrn 5 */ 6 #include <iostream> 7 #include <cstring> 8 #include <cstdio> 9 #include <cstdlib> 10 using namespace std; 11 int dp[1001][1001],r[1001][1001],c[1001][1001]; 12 bool o[1001][1001]; 13 int main() 14 { 15 int n,m,x,y,maxz,i,j; 16 freopen("bigbrn.in","r",stdin); 17 freopen("bigbrn.out","w",stdout); 18 scanf("%d%d",&n,&m); 19 for(i = 1;i <= m;i ++) 20 { 21 scanf("%d%d",&x,&y); 22 o[x][y] = 1; 23 } 24 for(x = 1;x <= n;x ++) 25 { 26 for(y = 1;y <= n;y ++) 27 { 28 if(o[x][y]) 29 { 30 r[x][y] = 0; 31 c[x][y] = 0; 32 dp[x][y] = 0; 33 } 34 else 35 { 36 r[x][y] = r[x-1][y] + 1; 37 c[x][y] = c[x][y-1] + 1; 38 dp[x][y] = min(min(r[x][y],c[x][y]),dp[x-1][y-1]+1); 39 } 40 } 41 } 42 maxz = 0; 43 for(i = 1;i <= n;i ++) 44 { 45 for(j = 1;j <= n;j ++) 46 { 47 maxz =max(dp[i][j],maxz); 48 } 49 } 50 printf("%d\n",maxz); 51 return 0; 52 }