80分求助
查看原帖
80分求助
1404938
SnowZ楼主2024/10/19 14:36
#include<stdio.h>
#define MAX_SIZE 100
int main()
{
  int R, C, K;
  scanf("%d %d %d",&R, &C, &K);
  int a[MAX_SIZE+1][MAX_SIZE+1];
  char ch;
  for(int i = 0; i < R; i++){
    for(int j = 0; j < C; j++){
      scanf(" %c",&ch);
      a[i][j] = (ch == '.') ? 1 : 0;
    }
  }

  int ans = 0;
  // 单行双指针
  for (int i = 0; i < R; i++) { // 水平方向
      int cnt = 0;
      for (int j = 0; j < C; j++) {
          if (a[i][j]) {
              cnt++;
          } else {
              cnt = 0;
          }
          if (cnt == K) {
              ans++;
              cnt--;
          }
      }
  }
  
  // 单列双指针
  for (int j = 0; j < C; j++) { // 垂直方向
      int cnt = 0;
      for (int i = 0; i < R; i++) {
          if (a[i][j]) {
              cnt++;
          } else {
              cnt = 0;
          }
          if (cnt == K) {
              ans++;
              cnt--;
          }
      }
  }

  printf("%d",ans);
  return 0;
}
2024/10/19 14:36
加载中...