CF 22B. Bargaining Table

水题。好久没有写过优化搜索题了。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <vector>
 5 using namespace std;
 6 char str[31][31];
 7 int r[31][31];
 8 int c[31][31];
 9 int judge(int rx,int ry,int lx,int ly)
10 {
11     int i,j;
12     for(i = lx;i <= rx;i ++)
13     {
14         for(j = ly;j <= ry;j ++)
15         {
16             if(str[i-1][j-1] == '1')
17             return 0;
18         }
19     }
20     return 1;
21 }
22 int main()
23 {
24     int n,m,i,j,k,u,ans;
25     scanf("%d%d",&n,&m);
26     for(i = 0;i < n;i ++)
27     scanf("%s",str[i]);
28     ans = 0;
29     for(i = 1;i <= n;i ++)
30     {
31         for(j = 1;j <= m;j ++)
32         {
33            if(str[i-1][j-1] == '0')
34            {
35                r[i][j] = r[i-1][j] + 1;
36                c[i][j] = c[i][j-1] + 1;
37            }
38            else
39            {
40                r[i][j] = 0;
41                c[i][j] = 0;
42            }
43         }
44     }
45     for(i = n;i >= 1;i --)
46     {
47         for(j = m;j >= 1;j --)
48         {
49             for(k = 1;k <= r[i][j];k ++)
50             {
51                 for(u = 1;u <= c[i][j];u ++)
52                 {
53                     if(ans >= (k+u)*2) continue;
54                     if(judge(i,j,i-k+1,j-u+1))
55                     {
56                         ans = max(ans,(k+u)*2);
57                     }
58                 }
59             }
60         }
61     }
62     printf("%d
",ans);
63     return 0;
64 }
原文地址:https://www.cnblogs.com/naix-x/p/3397309.html