思路:
友誼賽的時候一直想到了,但是沒想出來怎麼遍歷才能找到所有矩陣,卡住了。
這裏講一下完整思路:我們用一個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\n",ans);
}
return 0;
}