New Year and Domino(前缀和)

They say “years are like dominoes, tumbling one after the other”. But would a year fit into a grid? I don’t think so.

Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by ‘.’) or forbidden (denoted by ‘#’). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right.

Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid.

Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle?

Input
The first line of the input contains two integers h and w (1 ≤ h, w ≤ 500) – the number of rows and the number of columns, respectively.

The next h lines describe a grid. Each line contains a string of the length w. Each character is either ‘.’ or ‘#’ — denoting an empty or forbidden cell, respectively.

The next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of queries.

Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≤ r1i ≤ r2i ≤ h, 1 ≤ c1i ≤ c2i ≤ w) — the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle.

Output
Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle.

Examples
Input
5 8
…#…#
.#…
##.#…
##…#.##

4
1 1 2 3
4 1 4 1
1 2 4 5
2 5 5 8
Output
4
0
10
15
Note
A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways.

在这里插入图片描述
题目分析:
看到限时3s心想可以应该可以搜索,但一看测试数据最高100000,就放弃了。
既然不搜索肯定就dp啥的了,然后就想起来以前也有一个放多米诺骨牌的题,那道题的多米诺骨牌就两种方法,一种是横着,一种是竖着,当时的图是2n,那么把图的规模扩大一下,扩大到rc那么骨牌也无非只有横着放和竖着放两种区别,那么就可以确定状态了。设dp[i][j]表示从(1,1)到(i,j)所有骨牌可行方法的总和。那么就可以把这两种放法分别储存在一个二维数组总m[i][j]和n[i][j]表示从(1,1)到(i,j)竖着放与横着放的所有放法总和。可以根据加法原理得到递推关系式m[i][j]=m[i][j]+m[i-1][j]+m[i][j-1]-m[i-1][j-1]。
具体递推关系式的求法可根据这一篇文章
这里以题目样例为例,给出最后所得到的m,n以及dp这三个数组中的值。

m
0   0   0   0   0   0   0   0
1   1   2   3   3   4   5   5
1   1   3   4   5   7   9   10
1   1   4   5   6   9   11  12
1   1   5   7   8   12  14  15
n
0   1   2   3   3   3   4   4
0   1   2   4   5   6   8   9
0   1   2   4   5   7   10  12
0   1   2   5   6   8   11  13
0   2   4   8   10  13  17  20
dp
0   1   2   3   3   3   4   4
1   2   4   7   8   10  13  14
1   2   5   8   10  14  19  22
1   2   6   10  12  17  22  25
1   3   9   15  18  25  31  35

代码:

#include<iostream>
#include<iomanip>
using namespace std;
char s[501][501];
int m[501][501],n[501][501],dp[501][501];
int main()
{
    int r,c;
    cin>>r>>c;
    for(int i=1;i<=r;i++)
        for(int j=1;j<=c;j++)
        cin>>s[i][j];
    for(int i=1;i<=r;i++)
    {
        for(int j=1;j<=c;j++)
        {
            if(s[i][j]=='.'&&s[i-1][j]=='.')//竖着
                m[i][j]++;
            if(s[i][j]=='.'&&s[i][j-1]=='.')//横着
                n[i][j]++;
            m[i][j]+=m[i-1][j]+m[i][j-1]-m[i-1][j-1];
            n[i][j]+=n[i-1][j]+n[i][j-1]-n[i-1][j-1];
        }
    }
    for(int i=1;i<=r;i++)
        for(int j=1;j<=c;j++)
        dp[i][j]=m[i][j]+n[i][j];
    int q;
    cin>>q;
    while(q--)
    {
        int r1,r2,c1,c2;
        cin>>r1>>c1>>r2>>c2;
        int ans=0;
        for(int i=r1+1;i<=r2;i++)//因为最后处理之后是去掉了矩形边界的
            if(s[i][c1]=='.'&&s[i-1][c1]=='.') ans++;
        for(int i=c1+1;i<=c2;i++)
            if(s[r1][i]=='.'&&s[r1][i-1]=='.') ans++;
        cout<<dp[r2][c2]-dp[r1][c2]-dp[r2][c1]+dp[r1][c1]+ans<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值