记忆化

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b)) //#define ON_DEBUGG #ifdef ON_DEBUGG #define D_e_Line printf("\n----------\n")
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin) #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif
using namespace std;
struct ios{
template<typename ATP>inline ios& operator >> (ATP &x){
x = 0; int f = 1; char ch;
for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
x *= f;
return *this;
}
}io; template<typename ATP>inline ATP max(ATP &a, ATP &b){
return a > b ? a : b;
} const int N = 207; int n, m, timeLine;
int dx[] = {0, -1, 1, 0, 0};
int dy[] = {0, 0, 0, -1, 1}; char mp[N][N];
int l[N], r[N], dir[N];
int f[N][N][N]; inline int DFS(int tim, int x, int y){
if(tim > timeLine) return 0;
if(f[tim][x][y] != -1) return f[tim][x][y];
int len = r[tim] - l[tim] + 1;
int fx = x, fy = y;
int sum = DFS(tim + 1, x, y);
R(i,1,len){
fx += dx[dir[tim]], fy += dy[dir[tim]];
if(fx < 1 || fx > n || fy < 1 || fy > m || mp[fx][fy] == 'x') break;
sum = max(sum, i + DFS(tim + 1, fx, fy));
}
return f[tim][x][y] = sum;
} int main(){
FileOpen();
Fill(f, -1);
int X, Y;
io >> n >> m >> X >> Y >> timeLine;
R(i,1,n){
R(j,1,m){
char ch;
for(ch = getchar(); ch != '.' && ch != 'x'; ch = getchar());
mp[i][j] = ch;
}
}
R(i,1,timeLine){
io >> l[i] >> r[i] >> dir[i];
} printf("%d", DFS(1, X, Y)); return 0;
}

WA的暴力

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b)) #define ON_DEBUGG #ifdef ON_DEBUGG #define D_e_Line printf("\n----------\n")
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin) #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif
using namespace std;
struct ios{
template<typename ATP>inline ios& operator >> (ATP &x){
x = 0; int f = 1; char ch;
for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
x *= f;
return *this;
}
}io; const int N = 207; int f[N][N][2];
char mp[N][N]; int Dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
int main(){
//FileOpen(); int n, m, X, Y, K;
io >> n >> m >> X >> Y >> K;
R(i,1,n){
R(j,1,m){
char ch;
for(ch = getchar(); ch != '.' && ch != 'x'; ch = getchar());
mp[i][j] = ch;
}
} int l, r, dir;
int tot = 0;
R(i,1,n){
R(j,1,m){
f[i][j][0] = -2147483647;
}
}
f[X][Y][0] = 0;
R(timeLine,1,K){
io >> l >> r >> dir;
R(t, l, r){
++tot;
R(i,1,n){
R(j,1,m){
if(mp[i][j] == 'x') continue;
int fx = i + Dir[dir - 1][0], fy = j + Dir[dir - 1][1];
if(fx < 1 || fx > n || fy < 1 || fy > m || mp[fx][fy] == 'x') continue;
f[fx][fy][tot & 1] = Max(f[fx][fy][tot & 1], f[i][j][!(tot & 1)] + 1);
}
}
}
} int ans = 0;
R(i,1,n){
R(j,1,m){
ans = Max(ans, f[i][j][tot & 1]);
}
} printf("%d", ans); return 0;
}
/*
10 10 5 8 5
..........
......xxxx
.....xxxxx
.....xxxxx
..........
xxxx......
..........
..........
..........
..x.......
1 5 3
6 7 1
8 11 2
12 15 3
16 17 2 the right ans is : 15
but mine is : 17
*/

LuoguP2254 [NOI2005]瑰丽华尔兹 (单调队列优化DP)(用记忆化过了。。。)的更多相关文章

  1. BZOJ 1499 [NOI2005] 瑰丽华尔兹 | 单调队列优化DP

    BZOJ 1499 瑰丽华尔兹 | 单调队列优化DP 题意 有一块\(n \times m\)的矩形地面,上面有一些障碍(用'#'表示),其余的是空地(用'.'表示).每时每刻,地面都会向某个方向倾斜 ...

  2. bzoj 1499 [NOI2005]瑰丽华尔兹——单调队列优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1499 简单的单调队列优化dp.(然而当时却WA得不行.今天总算填了坑) 注意滚动数组赋初值应 ...

  3. bzoj1499[NOI2005]瑰丽华尔兹 单调队列优化dp

    1499: [NOI2005]瑰丽华尔兹 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 1802  Solved: 1097[Submit][Status ...

  4. bzoj1499 [NOI2005]瑰丽华尔兹——单调队列优化DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1499 朴素DP方程很好想,以右移为例,就是 f[i][x][y]=max(f[i][x][y ...

  5. 【BZOJ1499】[NOI2005]瑰丽华尔兹 单调队列+DP

    [BZOJ1499][NOI2005]瑰丽华尔兹 Description 你跳过华尔兹吗?当音乐响起,当你随着旋律滑动舞步,是不是有一种漫步仙境的惬意?众所周知,跳华尔兹时,最重要的是有好的音乐.但是 ...

  6. BZOJ 1499 NOI2005 瑰丽华尔兹 单调队列

    题目大意:给定一个m*n的地图,一些点有障碍物,钢琴初始在一个点,每一个时间段能够选择向给定的方向移动一段距离,求最长路径长 朴素DP的话,我们有T个时间段,每一个时间段有m*n个点,n个时间,一定会 ...

  7. 单调队列优化DP || [NOI2005]瑰丽华尔兹 || BZOJ 1499 || Luogu P2254

    题外话:题目极好,做题体验极差 题面:[NOI2005]瑰丽华尔兹 题解: F[t][i][j]表示第t时刻钢琴位于(i,j)时的最大路程F[t][i][j]=max(F[t-1][i][j],F[t ...

  8. 2018.09.10 bzoj1499: [NOI2005]瑰丽华尔兹(单调队列优化dp)

    传送门 单调队列优化dp好题. 这题其实很简单. 我们很容易想到一个O(T∗n∗m)" role="presentation" style="position: ...

  9. bzoj1499: [NOI2005]瑰丽华尔兹&&codevs1748 单调队列优化dp

    这道题 网上题解还是很多很好的 强烈推荐黄学长 码风真的好看 神犇传送门 学习学习 算是道单调队列优化dp的裸题吧 #include<cstdio> #include<cstring ...

  10. CF939F Cutlet (单调队列优化DP)

    题目大意:要煎一块有两个面的肉,只能在一段k不相交的时间段$[l_{i},r_{i}]$内翻转,求$2*n$秒后,保证两个面煎的时间一样长时,需要最少的翻转次数,$n<=100000$,$k&l ...

随机推荐

  1. K8S 使用Minikube搭建Kubernetes(K8S)~单机运行Kubernetes~适用于快速学习

    在一台主机上运行起来的Kubernetes,仅适用于学习!~~~ 系统版本:CentOS Linux release 7.6.1810 (Core) 软件版本:Docker-ce-18.06.0.Ku ...

  2. vue 的个人学习小笔记

    一.vite2.0+vue3.0+ts 创建.配置 个人公众号文章地址 个人github仓库地址 1.Vite 创建 vue3 项目: 1.1.npm 常用命令 1.npm 查看版本号 npm vie ...

  3. 背包,子集和以及 (max, +) 卷积在特殊情形下的求法

    背包,子集和以及 (max, +) 卷积在特殊情形下的求法 子集和 1:总重量不太大 有 \(n\) 个物品,每个物品重量为 \(w_i\),且 \(\sum\limits_{i} w_i=C\).你 ...

  4. STM32启动文件

    一.复位电路 在了解启动文件之前需要明白STM32的复位中断流程,STM32的复位分为上电复位和手动复位,复位的电路图如下所示: 注意: 图中的复位电路是低电平复位,有的MCU是高电平复位. 上电复位 ...

  5. Redis中的原子操作(2)-redis中使用Lua脚本保证命令原子性

    Redis 如何应对并发访问 使用 Lua 脚本 Redis 中如何使用 Lua 脚本 EVAL EVALSHA SCRIPT 命令 SCRIPT LOAD SCRIPT EXISTS SCRIPT ...

  6. 『忘了再学』Shell基础 — 26、cut列提取命令

    目录 1.cut命令说明 2.cut命令练习 (1)cut命令基本用法 (2)cut命令选取多列 (3)按字符来进行提取 (4)按指定分隔符进行截取数据 3.cut命令分隔符说明 1.cut命令说明 ...

  7. 前端2CSS2

    内容概要 伪元素选择器 选择器优先级 字体样式 文字属性 背景属性 display属性 边框属性 盒子模型 浮动(重要) 解决浮动造成的影响 内容详情 伪元素选择器 """ ...

  8. 《SQL Server基础——SQL语句》

    SQL Server基础--SQL语句       一.创建和删除数据库: 1.创建数据库(默认化初始值) 格式: CREATE DATABASE 数据库名称 例如: CREATE DATABASE ...

  9. Vue数据双向绑定原理(vue2向vue3的过渡)

    众所周知,Vue的两大重要概念: 数据驱动 组件系统 1 2 接下来我们浅析数据双向绑定的原理 一.vue2 1.认识defineProperty vue2中的双向绑定是基于definePropert ...

  10. SAP - 拆包,组件入库

    场景: 一个成品商品,例如汽车,有很多零部件:车轮,框架,发动机等.以整体形式发货过账,在遇到质量问题客户退货情况,需要把汽车拆开,然后零部件退回到库(按照BOM结构拆卸). MB1A/MIGO:发货 ...