Codeforces 611C. New Year and Domino 动态规划
3 seconds
256 megabytes
standard input
standard 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?
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
A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways.
题意:输入h,w表示行和列的数目,接下来h行描述一个网格,每一行有一个长度为w的字符串,每个字符要么是'.',要么是'#',分别表示空的和禁止的。
输入一个q,接下来得q行有4个数,表示一个小网格的左上角的横纵坐标和右下角的横纵坐标。
输出每个小方格内有几个2个连在一起的空格。
思路:网格存在gg[MAXN][MAXN]中,用两个二位数组row[MAXN][MAXN]和column[MAXN][MAXN];row[i][j]表示第i行从开始到j这个位置有几个2个连在一起的空格,column[i][j]表示第j列从开始到i这个位置有几个2个连在一起的空格。
row举例来说,如果gg[i][j]这个位置是'.'并且它前面gg[i][j-1]这个位置也是'.'的话row[i][j]=row[i][j-1]+1;否则的话,row[i][j]=row[i][j-1]。
要注意越界的问题。
if(gg[i][j]=='.'&&j>1&&gg[i][j-1]=='.') row[i][j]=row[i][j-1]+1;
else row[i][j]=row[i][j-1];
if(gg[i][j]=='.'&&i>1&&gg[i-1][j]=='.') column[i][j]=column[i-1][j]+1;
else column[i][j]=column[i-1][j];
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char gg[][];
int row[][],column[][];
int x1[],y1[],x2[],y2[];
int main()
{
int h,w,q;
scanf("%d %d",&h,&w);
getchar();
memset(row,,sizeof(row));
memset(column,,sizeof(column));
int i,j;
for(i=; i<=h; i++)
{
for(j=; j<=w; j++)
{
scanf("%c",&gg[i][j]);
if(gg[i][j]=='.'&&j>&&gg[i][j-]=='.') row[i][j]=row[i][j-]+;
else row[i][j]=row[i][j-];
if(gg[i][j]=='.'&&i>&&gg[i-][j]=='.') column[i][j]=column[i-][j]+;
else column[i][j]=column[i-][j];
}
getchar();
}
/*
for(i=1; i<=h; i++)
{
for(j=1; j<=w; j++)
cout<<row[i][j]<<" ";
cout<<endl;
}
cout<<endl<<endl;
for(i=1; i<=h; i++)
{
for(j=1; j<=w; j++)
cout<<column[i][j]<<" ";
cout<<endl;
}
*/
scanf("%d",&q);
for(i=; i<q; i++)
scanf("%d%d%d%d",&x1[i],&y1[i],&x2[i],&y2[i]);
__int64 ans;
for(i=; i<q; i++)
{
ans=;
for(j=x1[i]; j<=x2[i]; j++)
{
//cout<<row[j][y2[i]]<<" "<<row[j][y1[i]]<<endl;
ans+=row[j][y2[i]]-row[j][y1[i]];
}
for(j=y1[i]; j<=y2[i]; j++)
{
//cout<<column[x2[i]][j]<<" "<<column[x1[i]][j]<<endl;
ans+=column[x2[i]][j]-column[x1[i]][j];
}
cout<<ans<<endl;
} return ;
}
Codeforces 611C. New Year and Domino 动态规划的更多相关文章
- CodeForces 611C New Year and Domino (动态规划,DP)
题意:给定一个h*w的网格,里面只有.和#,.表示空的,#表示禁止的,然后有q个询问,询问中给你两个坐标,分别是左上和右下,求在这两者中间的有多少种(竖着和横着)两个相邻的点. 析:一看到这个题目,肯 ...
- Codeforces 611C New Year and Domino(二维前缀和)
题目大概说给一个n*m个格子,格子'.'表示可以放东西,多次询问矩形区域(x1,y1)-(x2,y2)有几种放一张1*2的骨牌的方案数. 分别考虑横着竖着放,预处理出二维的前缀和,即sum[x][y] ...
- Codeforces 611C New Year and Domino DP+容斥
"#"代表不能放骨牌的地方,"."是可以放 500*500的矩阵,q次询问 开两个dp数组,a,b,a统计横着放的方案数,b表示竖着放,然后询问时O(1)的,容 ...
- 【CodeForces 611C】New Year and Domino
题 题意 h行w列的矩形格子,“." 代表空的,"#" 代表满的,多米诺是 1*2 的长方体,现在放进格子,给你子矩形的左上角和右上角,问在子矩形里共有多少种放一块多米诺 ...
- Codeforces 835F Roads in the Kingdom - 动态规划
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗基环树,要求删去其中一条边,使得剩下的图形是一棵树,并且最长路的长度最短,求最长路的最短长度. 路径可以分为两部分:跨过环 和 在树内 ...
- Codeforces 581F Zublicanes and Mumocrates - 树形动态规划
It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The ...
- 【Codeforces 949D】Shake It! 【动态规划】
参考: http://blog.csdn.net/gjghfd/article/details/77824901 所求的是满足条件的图中“不同构”的数量,意味着操作的顺序是可以忽略的.考虑若干次操作后 ...
- Codeforces 500 E. New Year Domino
\(>Codeforces \space 500 E. New Year Domino<\) 题目大意 : 数轴上有序排列着 \(n\) 块多米诺骨牌, 第 \(i\) 块骨牌坐标为 \( ...
- Codeforces Round #335 Sorting Railway Cars 动态规划
题目链接: http://www.codeforces.com/contest/606/problem/C 一道dp问题,我们可以考虑什么情况下移动,才能移动最少.很明显,除去需要移动的车,剩下的车, ...
随机推荐
- Python3 引入sqlite3时出现错误:ModuleNotFoundError: No module named '_sqlite3'
在Python3 中内置了SQLite3,但是在编译安装完之后执行: import sqlite3 出现错误: ModuleNotFoundError: No module named '_sqlit ...
- jQuery更新
jQuery jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行 ...
- python-pycharm中使用anaconda部署python环境
pycharm中使用anaconda部署python环境 今天来说一下python中一个管理包很好用的工具anaconda,可以轻松实现python中各种包的管理.相信大家都会有这种体验,在pycha ...
- [Python] 分段线性插值
利用线性函数做插值 每一段的线性函数: #Program 0.6 Linear Interploation import numpy as np import matplotlib.pyplot as ...
- 如何提取一个转录本的3'UTR区域的序列
庐州月光 如何提取一个转录本的3'UTR区域的序列 在做microRNA 和 mRNA 相互作用预测的时候,大家都知道microRNA 作用的靶点是位于mRNA 的3'UTR取,所以只需要提取mRNA ...
- 关于封装Dll为Web Service技术方案的讨论
关于web架构技术方案的讨论整理 Sonictl 2014年1月25日10:05:52 本着"三人行必有我师"的学习态度,我在近期跟x老师做了大量沟通,结合我们单位对于" ...
- yii 执行sql
sql $sql = "SELECT ".join(',', $this->search_fields_channel)." FROM {{chan ...
- Redis 通用操作1
01, 设置值 => set key value 01.1, 设置值并添加有效期 => set key value ex 秒数 或者 set key value px 毫秒数 01.2, ...
- EL&jsp
JSP 2.0(java server pages): EL 表达式 JSP九大内置对象及作用范围 JSP Directive JSP Action EL表达式: EL 算法(Arithmetic)表 ...
- No matter how hard it is or no matter how bad it gets, I am going to make it!
No matter how hard it is or no matter how bad it gets, I am going to make it! He always had a yearni ...