HDU1510 White rectangles( 乱搞 O(n^3) )题解

思路:

友谊赛的时候一直想到了,但是没想出来怎么遍历才能找到所有矩阵,卡住了。

这里讲一下完整思路:我们用一个num[i][j]表示第i行第j列每一列连续的白色格子数量,然后我们定义一个MIN,并且每次都更新MIN的值(因为矩阵高度只和最小的那个有关)。

代码:

#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define ll long long
const int maxn = 100+5;
const int MOD = 1e7;
const int INF = 0x3f3f3f3f;
using namespace std;
int num[maxn][maxn];
char s[maxn][maxn];
int main(){
    int T,n,m;
    while(scanf("%d",&n) != EOF){
        memset(num[0],0,sizeof(num[0]));
       for(int i = 1;i <= n;i++){
            scanf("%s",s[i] + 1);
            for(int j = 1;j <= n;j++){
                if(s[i][j] == '.'){
                    num[i][j] = num[i - 1][j] + 1;
                }
                else{
                    num[i][j] = 0;
                }
            }
       }


       int ans = 0;
       for(int i = 1;i <= n;i++){
            for(int j = 1;j <= n;j++){
                int MIN =  num[i][j];   //卡在这一步没推出来,只要不断更新min一直往前推就好
                ans += MIN;
                for(int k = j - 1;k >= 1 && MIN;k--){
                    MIN = min(MIN,num[i][k]);
                    ans += MIN;
                }

            }
       }
       printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/KirinSB/p/9408754.html