[codeforces]Goodbye_2015

C. New Year and Domino

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Sample test(s)
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
input
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
output
53
89
120
23
0
2
题意:给定一幅图,其中有空位和墙,给定n个矩形的左上方的坐标和右下角的坐标,多米诺骨牌需要占两个空位,求在这个矩形中有多少种放置多米诺骨牌的方法
思路:比赛的时候,一开始没考虑时间复杂度,直接模拟,导致time limits
之后就想到要先预置好所有的点,用动态规划来求。
dp[i][j]表示以(1,1)为矩形左上角起点,以(i, j)为矩形右下角总共放置多米诺骨牌的方法。dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1]+当前该点多米诺骨牌水平放置和垂直放置的个数
给定x1,y1,x2,y2则sum = dp[x2][y2] + dp[x1][y1] - dp[x1-1][y2]-dp[x2][y1-1].由于没想到要动态规划分开求垂直放置和水平放置导致最后计算有些复杂。
其题解的解法公式
sum = rowp[x2][y2] + rowp[x1][y1] - rowp[x1-1][y2]-rowp[x2][y1]
sum += colp[x2][y2] + colp[x1][y1] - colp[x1][y2]-colp[x2][y1-1]
看题解之后,才发现可以这么做,愈哭无泪。。附上比赛ac的代码

#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
#define rep(i, l, n) for (int i = l; i < n; i++)
int h, w, q, r1, c1, r2, c2;
char s[510][510];
int vis[510][510];
int dp[510][510];
int rowp[510][510];
int colp[510][510];
int disx[4] = {0, -1, 0, 1};
int disy[4] = {-1, 0, 1, 0};
int main() {
    scanf("%d%d", &h, &w);
    rep(i, 1, h+1) {
        scanf("%s", s[i]+1);
    }
    scanf("%d", &q);
    memset(dp, 0, sizeof(dp));
    for (int i = 2; i <= h; i++) {
        if (s[i][1] == '.') {
                int x = i-1;
                if (x >= 1 && x<= h && s[x][1] == '.') {
                    dp[i][1] = dp[i-1][1]+1;
                } else {
                    dp[i][1] = dp[i-1][1];
                }
        } else
            dp[i][1] = dp[i-1][1];
    }
    for (int i = 2; i <= w; i++) {
        if (s[1][i] == '.') {
                int x = i-1;
                if (x >= 1 && x<= w && s[1][x] == '.') {
                    dp[1][i] = dp[1][i-1]+1;
                } else {
                    dp[1][i] = dp[1][i-1];
                }
        }
        else
        dp[1][i] = dp[1][i-1];
    }
    for (int i = 1; i <= w; i++) {
        for (int j = 1; j <= h; j++) {
            if (i == 1 && j==1) {
                colp[j][i] = 0;
            }
            if (s[j][i] == '.') {
                int x = j-1;
                if (x >= 1 && x<= h && s[x][i] == '.') {
                    colp[j][i] = colp[j-1][i]+1;
                } else {
                    colp[j][i] = colp[j-1][i];
                }
            } else
                colp[j][i] = colp[j-1][i];
        }
    }
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            if (i == 1 && j==1) {
                rowp[i][j] = 0;
            }
            if (s[i][j] == '.') {
                int x = j-1;
                if (x >= 1 && x<= w && s[i][x] == '.') {
                    rowp[i][j] = rowp[i][j-1]+1;
                } else {
                    rowp[i][j] = rowp[i][j-1];
                }
            } else
                rowp[i][j] = rowp[i][j-1];
        }
    }

    for (int i = 2; i <= h; i++) {
        for (int j = 2; j <= w; j++) {
            if (s[i][j] == '.') {
                for (int k = 0; k < 2; k++) {
                    int x = i + disx[k];
                    int y = j + disy[k];
                    if (x >= 1 && y >= 1 && s[x][y] == '.') {
                        dp[i][j] += dp[x][y]+1;
                    } else {
                        dp[i][j] += dp[x][y];
                    }
                }
                dp[i][j] -= dp[i-1][j-1];
            } else {
                for (int k = 0; k < 2; k++) {
                    int x = i + disx[k];
                    int y = j + disy[k];
                    dp[i][j] += dp[x][y];
                }
                dp[i][j] -= dp[i-1][j-1];
            }
        }
    }
    while (q--) {
        scanf("%d%d%d%d", &r1, &c1, &r2, &c2);
        if (r1 == r2 && c1 == c2)
             printf("0\n");
        else
            printf("%d\n", dp[r2][c2]+dp[r1][c1] - dp[r2][c1] - dp[r1][c2] + rowp[r1][c2]- rowp[r1][c1] + colp[r2][c1] -colp[r1][c1]);
    }
    return 0;
}

你可能感兴趣的:(codeforces)