D——Determine the Photo Position
题目大意
给你一个 n ∗ n n*n n∗n的矩阵,0表示这个位置没有人,1表示这个位置有人,问你把一个 1 ∗ m 1*m 1∗m的矩阵安排在没有人的位置上有多少种安排方法
解题思路
遍历每一行,找连续的0,连续0的个数为num,每一块连续的0对答案的贡献是 m a x ( 0 , n u m − m + 1 ) max(0, num-m + 1) max(0,num−m+1)
Code
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 2e5 + 7;
string s;
int main(){
int n, m;
cin >> n >> m;
int ans = 0;
for(int i = 0; i < n; i++){
cin >> s;
int cnt = 0;
s += '1';
for(int j = 0; j <= n; j++){
if(s[j] == '0')
cnt++;
else{
if(cnt >= m)
ans += cnt - (m-1);
cnt = 0;
}
}
}
cin >> s;
cout << ans << endl;
return 0;
}
// zk