题目链接:
本题数据范围仅为 \(100\),因此可以暴力枚举 \(O(n^3)\),唯一需要注意的一点就是当 \(k=1\) 时,横着站和竖着站是一样的,答案被计算了两次,因此最终的 \(\rm ans\) 需要再除以 \(2\)。
#include <bits/stdc++.h>
const int N = 110;
char w[N][N];
int R, C, K, ans;
bool flag = true;
int main()
{
std::cin >> R >> C >> K;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
std::cin >> w[i][j];
}
}
for (int i = 0; i < R; i++) {
for (int j = 0; j <= C - K; j++) {
int z = j + K - 1;
flag = true;
for (int k = j; k <= z; k++) {
if (w[i][k] == '#') flag = false;
}
if (flag) ans++;
}
}
for (int j = 0; j < C; j++) {
for (int i = 0; i <= R - K; i++) {
int z = i + K - 1;
flag = true;
for (int k = i; k <= z; k++) {
if (w[k][j] == '#') flag = false;
}
if (flag) ans++;
}
}
if (K == 1) ans /= 2;
std::cout << ans;
return 0;
}
标签:std,int,P3654,++,Step,ans,First
From: https://www.cnblogs.com/pangyou3s/p/18114081