老二次元题面了
直接统计
#include<iostream>
using namespace std;
const int N = 110;
char g[N][N];
int r, c, k;
int p[N], q[N];
int res;
int main(){
cin >> r >> c >> k;
for(int i = 1; i <= r; i ++) cin >> g[i] + 1;
for(int i = 1; i <= r; i ++){
int cnt = 0;
for(int j = 1; j <= c; j ++){
while(j <= c && g[i][j] == '.') cnt ++, j ++;
if(cnt >= k) res += cnt - k + 1;
cnt = 0;
}
}
for(int i = 1; i <= c; i ++){
int cnt = 0;
for(int j = 1; j <= r; j ++){
while(j <= r && g[j][i] == '.') cnt ++, j ++;
if(cnt >= k) res += cnt - k + 1;
cnt = 0;
}
}
if(k == 1) res /= 2;
cout << res << endl;
return 0;
}
搜索版
#include<iostream>
using namespace std;
const int N = 110;
char g[N][N];
int r, c, k;
int st[N][N];
int dx[] = {1, 0}, dy[] = {0, 1};
int dfs(int x, int y, int u){
int a = x + dx[u], b = y + dy[u], cnt = 1;
if(a <= r && b <= c && g[a][b] == '.') cnt += dfs(a, b, u);
return cnt;
}
int main(){
cin >> r >> c >> k;
for(int i = 1; i <= r; i ++) cin >> g[i] + 1;
int res = 0;
for(int i = 1; i <= r; i ++)
for(int j = 1; j <= c; j ++)
if(g[i][j] == '.')
for(int u = 0; u < 2; u ++)
if(dfs(i, j, u) >= k) res ++;
if(k == 1) res /= 2;
cout << res;
return 0;
}