codeforce—Seat Arrangements,补11-30 Training

Problem - C - Codeforces

Suppose that you are in a campus and have to go for classes day by day. As you may see, when you hurry to a classroom, you surprisingly find that many seats there are already occupied. Today you and your friends went for class, and found out that some of the seats were occupied.

The classroom contains \(n\) rows of seats and there are \(m\) seats in each row. Then the classroom can be represented as an \(n \times m\) matrix. The character '.' represents an empty seat, while '*' means that the seat is occupied. You need to find \(k\) consecutive empty seats in the same row or column and arrange those seats for you and your friends. Your task is to find the number of ways to arrange the seats. Two ways are considered different if sets of places that students occupy differs.

假设你在一所校园里,每天都要去上课。你可能会看到,当你匆匆赶到教室时,你会惊讶地发现那里有很多座位已经被占了。今天,你和朋友去上课,发现有些座位已经被占了。

教室里有 \(n\) 排座位,每排有 \(m\) 个座位。那么教室可以表示为一个 \(n \times m\) 矩阵。字符". "表示空座位,而"*"表示该座位有人。你需要在同一行或同一列中找到 \(k\) 个连续的空座位,并为你和你的朋友安排这些座位。您的任务是找出排列座位的方法数。**如果学生所占位置的集合不同,则认为两种方法不同。

Input

The first line contains three positive integers \(n,m,k\) (\(1 \leq n, m, k \leq 2\,000\)), where \(n,m\) represent the sizes of the classroom and \(k\) is the number of consecutive seats you need to find.

Each of the next \(n\) lines contains \(m\) characters '.' or '*'. They form a matrix representing the classroom, '.' denotes an empty seat, and '*' denotes an occupied seat.

输入

第一行包含三个正整数 \(n,m,k\) ( \(1 \leq n, m, k \leq 2\,000\) ),其中 \(n,m\) 代表教室的大小, \(k\) 是需要找到的连续座位数。

接下来的 \(n\) 行中,每一行都包含 \(m\) 个字符". "或"*"。它们组成了一个代表教室的矩阵,'.'表示一个空座位,'*'表示一个有人的座位。

Output

A single number, denoting the number of ways to find \(k\) empty seats in the same row or column.

Examples

Input

2 3 2**....

Output

3

Input

1 2 2..

Output

1

Input

3 3 4.*.*.*.*.

Output

0

Note

In the first sample, there are three ways to arrange those seats. You can take the following seats for your arrangement.

  • \((1,3)\), \((2,3)\)
  • \((2,2)\), \((2,3)\)
  • \((2,1)\), \((2,2)\)

在第一个示例中,有三种安排这些座位的方法。您可以选择以下座位进行安排。

  • \((1,3)\) , \((2,3)\)
  • \((2,2)\) , \((2,3)\)
  • \((2,1)\) , \((2,2)\)

题解

首先,这道题看一下数据量不大,我们需要找的就是连起来的座位,所以可以直接按照行扫描一遍,再按照列扫描一遍,最后二者相加,即可求出。
值得注意两点:

  1. 如果 k 为 1,也就是说要找的座位只有一个,那么我们从行扫一遍,再从列扫一遍,就会多加一次,结果就翻倍了我们把黑色看作*,绿色看作. ,那么4个绿色方块就会算出8种方案,明显是错误的
    一个事例
  2. 在处理连续的座位时,我们遇到满足>=k的连座数我们就进行方案添加,如果处理完这一行或者这一列后再添加,就会因为途中可能遇到的*归零

代码如下

#include<bits/stdc++.h>
using namespace std;
const int N = 2005;
char mp[N][N];
int main(){
int n,m,k;cin>>n>>m>>k;
for(int i = 0;i < n;i ++)
    for(int j = 0;j < m;j ++){
        cin>>mp[i][j];
    }
    int row = 0;
    int ans = 0;
    int col = 0;
    for(int i = 0;i < n;i ++){
        row = 0;
        for(int j = 0;j < m;j ++){
            if(mp[i][j] == '*')row = 0;
            else {row ++;if(row >= k) ans += 1;}
        }
       
    }
     for(int i = 0;i < m;i ++){
        col = 0;
        for(int j = 0;j < n;j ++){
            if(mp[j][i] == '*')col = 0;
            else{col ++; if(col >= k) ans += 1;}
               
        }
        
    }
    if(k == 1)ans /= 2;
    cout<<ans;
    return 0;
}
posted @ 2023-12-04 04:27  LongDz  阅读(45)  评论(0)    收藏  举报