USACO 3.3 Home on the Range(最大子矩阵问题)

以前做过类似的。

 1 /*

 2      ID: cuizhe

 3      LANG: C++

 4      TASK: range

 5 */

 6 #include <iostream>

 7 #include <cstdio>

 8 #include <cstring>

 9 #include <cmath>

10 using namespace std;

11 #define N 501

12 char str[N][N];

13 int key[N][N],x[N][N],y[N][N];

14 int o[N],map[N][N];

15 int Min(int a,int b,int c)

16 {

17     int t = a;

18     if(t > b)

19     t = b;

20     if(t > c)

21     t = c;

22     return t;

23 }

24 int main()

25 {

26     int i,j,k,n;

27     freopen("range.in","r",stdin);

28     freopen("range.out","w",stdout);

29     scanf("%d",&n);

30     for(i = 1;i <= n;i ++)

31     scanf("%s",str[i]);

32     for(i = 1;i <= n;i ++)

33     {

34         for(j = 1;j <= n;j ++)

35         {

36             if(str[i][j-1] == '1')

37             map[i][j] = 1;

38         }

39     }

40     for(i = 1;i <= n;i ++)

41     {

42         for(j = 1;j <= n;j ++)

43         {

44             if(map[i][j] == 1)

45             {

46                 x[i][j] = x[i-1][j]+1;

47                 y[i][j] = y[i][j-1]+1;

48                 if(map[i-1][j-1] == 1)

49                 key[i][j] = Min(x[i][j],y[i][j],key[i-1][j-1]+1);

50                 else

51                 key[i][j] = 1;

52             }

53             else

54             {

55                 x[i][j] = 0;

56                 y[i][j] = 0;

57                 key[i][j] = 0;

58             }

59         }

60     }

61     for(i = 1;i <= n;i ++)

62     {

63         for(j = 1;j <= n;j ++)

64         {

65             for(k = 2;k <= key[i][j];k ++)

66             {

67                 o[k] ++;

68             }

69         }

70     }

71     for(i = 2;i <= n;i ++)

72     {

73         if(o[i])

74         printf("%d %d\n",i,o[i]);

75     }

76     return 0;

77 }

你可能感兴趣的:(USACO)