CodeForces 611C New Year and Domino (动态规划,DP)
题意:给定一个h*w的网格,里面只有.和#,.表示空的,#表示禁止的,然后有q个询问,询问中给你两个坐标,分别是左上和右下,求在这两者中间的有多少种(竖着和横着)两个相邻的点。
析:一看到这个题目,肯定是DP,一想,不会做,想了好久都不会,这怎么分析呢,横着和竖着明明就是混合的,怎么考虑呢,想了好久都没想出来。后来偷偷问一下别人,哦,原来就是分开考虑的。
分开考虑就是行的考虑行的,列的考虑列的,最后再相加就好了,我们用dr[i][j]来表示第i行到第j个位置的种数,同样的列dc[i][j]第i列到第j个位置的种数。
因为行和列类似,我们就分析行,如果第j-1个是.,那么总数就会多一个,否则总数和和j-1时一样,再考虑一下边界,即:if(G[i][j] == '.' && G[i][j-1] == '.') dr[i][j] = dr[i][j-1] + 1;
else dr[i][j] = dr[i][j-1];
同理列也是如此,
if(G[i][j] == '.' && G[i-1][j] == '.') dc[i][j] = dc[i-1][j] + 1;
else dc[i][j] = dc[i-1][j];剩下的就是如何去从左上角到右下角的满足条件种数了,就是每一行(列)用最后那个坐标减掉最前面那个坐标,这就是这一行(列)的总数,再分别加起来就是最后答案了。
代码如下:
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <iomanip>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <map>
#include <list> using namespace std;
typedef long long LL;
const int maxn = 500 + 10;
char G[maxn][maxn];
int dr[maxn][maxn], dc[maxn][maxn]; int main(){
int r, c;
cin >> r >> c;
for(int i = 1; i <= r; ++i)
scanf("%s", G[i]+1); memset(dr, 0, sizeof(dr));
memset(dc, 0, sizeof(dc));
for(int i = 1; i <= r; ++i){
for(int j = 1; j <= c; ++j){
if(G[i][j] == '.' && G[i][j-1] == '.') dr[i][j] = dr[i][j-1] + 1;
else dr[i][j] = dr[i][j-1];
if(G[i][j] == '.' && G[i-1][j] == '.') dc[i][j] = dc[i-1][j] + 1;
else dc[i][j] = dc[i-1][j];
}
} int q; cin >> q;
while(q--){
int x1, y1, x2, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
int cnt = 0;
for(int i = x1; i <= x2; ++i){
cnt += dr[i][y2] - dr[i][y1];
}
for(int i = y1; i <= y2; ++i){
cnt += dc[x2][i] - dc[x1][i];
}
printf("%d\n", cnt); }
return 0;
}
CodeForces 611C New Year and Domino (动态规划,DP)的更多相关文章
- Codeforces 611C. New Year and Domino 动态规划
C. New Year and Domino time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- Codeforces 611C New Year and Domino DP+容斥
"#"代表不能放骨牌的地方,"."是可以放 500*500的矩阵,q次询问 开两个dp数组,a,b,a统计横着放的方案数,b表示竖着放,然后询问时O(1)的,容 ...
- Codeforces 611C New Year and Domino(二维前缀和)
题目大概说给一个n*m个格子,格子'.'表示可以放东西,多次询问矩形区域(x1,y1)-(x2,y2)有几种放一张1*2的骨牌的方案数. 分别考虑横着竖着放,预处理出二维的前缀和,即sum[x][y] ...
- Codeforces Round #335 Sorting Railway Cars 动态规划
题目链接: http://www.codeforces.com/contest/606/problem/C 一道dp问题,我们可以考虑什么情况下移动,才能移动最少.很明显,除去需要移动的车,剩下的车, ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- 动态规划dp
一.概念:动态规划dp:是一种分阶段求解决策问题的数学思想. 总结起来就一句话:大事化小,小事化了 二.例子 1.走台阶问题 F(10):10级台阶的走法数量 所以:F(10)=F(9)+F(8) F ...
- 算法-动态规划DP小记
算法-动态规划DP小记 动态规划算法是一种比较灵活的算法,针对具体的问题要具体分析,其宗旨就是要找出要解决问题的状态,然后逆向转化为求解子问题,最终回到已知的初始态,然后再顺序累计各个子问题的解从而得 ...
- [CodeForces - 1272D] Remove One Element 【线性dp】
[CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...
- 【CodeForces 611C】New Year and Domino
题 题意 h行w列的矩形格子,“." 代表空的,"#" 代表满的,多米诺是 1*2 的长方体,现在放进格子,给你子矩形的左上角和右上角,问在子矩形里共有多少种放一块多米诺 ...
随机推荐
- Annoying “Remote System Explorer Operation” causing freeze for couple of seconds
Eclipse -> Preferences -> General -> Startup and Shutdown. -Uncheck RSE UI. Eclipse -> P ...
- position_css
position: 定位,元素的定位与这五个属性相关.left,top,bottom,right,z-index 1. static (默认值).没有定位,五个属性都不起作用. 2. inherit ...
- C#--抽象工厂设计模式原理
C#--抽象工厂设计模式原理 C#--抽象工厂设计模式--三层框架 C#--使用反射改进简单工厂
- Ubuntu下启动 Redis时, 提示 "Can't open the log file: Permission denied failed"
问题来源:在删除var目录下的log文件时,将redis文件夹删除了.然后在重启时:/etc/init.d/redis-server start,提示: Starting redis-server: ...
- 吴裕雄 实战python编程(2)
from urllib.parse import urlparse url = 'http://www.pm25x.com/city/beijing.htm'o = urlparse(url)prin ...
- 吴裕雄 数据挖掘与分析案例实战(14)——Kmeans聚类分析
# 导入第三方包import pandas as pdimport numpy as np import matplotlib.pyplot as pltfrom sklearn.cluster im ...
- mysql 常用option
[mysql 常用option] --host=host_name, -h host_name Connect to the MySQL server on the given host. --por ...
- ping,telnet,tracert分别用的是什么协议
Telnet使用的是tcp协议使用示例:telnet 192.168.1.20 80 ping命令使用的是icmp协议示例:ping www.sina.com.cn或ping 192.168.1.10 ...
- Web标准:九、CSS表单设计
Web标准:九.CSS表单设计 知识点: 1.改变文本框和文本域样式 2.用图片美化按钮 3.改变下拉列表样式 4.用label标签提升用户体验 1)改变文本框和文本域样式 文本框标签:<i ...
- LuoguP1126 机器人搬重物(BFS)
题目链接:https://www.luogu.org/problemnew/show/P1126 思路:很不错的搜索题,用BFS,虐了我1天多才A掉 QAQ,细节很多. 1.每个状态包含行.列.方向. ...