题目如下:
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?
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.
Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle.
5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8
4 0 10 15
7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8
53 89 120 23 0 2
思路:很明显,这道题100000的查询量决定了这题需要预处理,用DP解决从(1,1)到(i,j)方块中总共符合条件的位置数量。
DP方程:f[i][j] = f[i][j-1]+f[i-1][j]-f[i-1][j-1]+新增(i>0,j>0); i=0/j=0时特殊处理。
然后需要用到部分和的思想(因为题目要求从r1,c1到r2,c2),我觉得也很明显,但有一些边界值处理上的问题。
最简单的一个公式与DP公式差不多:ans = f[r2][c2] - f[r2][c1-1] - f[r1-1][c2] + f[r1-1][c1-1]
但要注意到r1和c1为0的情况,还要考虑两块拼接在一起时跨越两块的个数。
即 完整的公式应该是 ans = f[r2][c2] - f[r2][c1-1] - f[r1-1][c2] + f[r1-1][c1-1] - 跨越两块的个数。
代码如下:
#include
using namespace std;
const int maxn = 508;
int h,w,q;
string s[maxn];
int f[maxn][maxn];
void do_it(){
f[0][0] = 0;
for (int i=1; i<h; i++) {
if(s[i][0] == '.' && s[i-1][0] == '.') f[i][0] = f[i-1][0]+1;
else f[i][0] = f[i-1][0];
}
for (int i=1; i<w; i++) {
if(s[0][i] == '.' && s[0][i-1] == '.') f[0][i] = f[0][i-1]+1;
else f[0][i] = f[0][i-1];
}
for (int i=1; i<h; i++) {//以上是边界值的确定
for (int j=1; j<w; j++) {
f[i][j] = f[i-1][j]+f[i][j-1]-f[i-1][j-1];
if(s[i][j] == '.' && s[i-1][j] == '.') f[i][j]++;//新增的
if(s[i][j] == '.' && s[i][j-1] == '.') f[i][j]++;
}
}
}
int main(int argc, const char * argv[]) {
cin >> h >> w;
for (int i=0; i<h; i++) {
cin >> s[i];
}
do_it();
cin >> q;
for (int i=0; i<q; i++) {
int r1,c1,r2,c2;
cin >> r1 >> c1 >> r2 >> c2;
int ans = f[r2-1][c2-1];
if(r1>1){//考虑 r1 = 0 特殊情况
ans -= f[r1-2][c2-1];
for (int j = c1-1; j if(s[r1-1][j]=='.' && s[r1-2][j] == '.') ans--; } } if(c1>1){ ans -= f[r2-1][c1-2]; for (int j = r1-1; j if(s[j][c1-1]=='.' && s[j][c1-2] == '.') ans--; } } if(r1>1&&c1>1) ans += f[r1-2][c1-2]; cout << ans << endl; } return 0; }