HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)
Description
In the new game, a robot named "Facer" is swimming in a pool. The pool can be considered as a vertical N x M grid. The coordinates of the top-left cell is (1,1), and the coordinates of the bottom-right cell is (N, M). Cells in the top rows are called "surface cells", and all other cells are called "under water cells".
Figure 1. The swimming pool. Shadowed cells are poisonous.Facer starts swimming from the top-left cell (1,1) and must arrive at the top-right cell (1, M). He has a constant horizontal speed of 1 per second, and a vertical speed of v per second. v is always integer and can be changed. If v is positive, it means Facer is moving down; and if v is negative, it means Facer is moving up. So if Facer is at cell (x, y) now, he will arrive at cell (x + v, y + 1) after a second. Be careful that if x + v > N, Facer will get to cell (N, y + 1) because he cannot move down anymore and of course if x + v < 1, Facer will get to cell (1, y + 1) because he cannot fly. Please remember that all the ``under water cells" in the right-most column are poisonous. If Facer goes into those cells, he will die. The capacity of Facer's oxygen bottle is limited, and when Facer gets into a surface cell, the oxygen bottle is refilled automatically. Facer has to refill his oxygen bottle every K seconds, which means that during the time between two refillings, Facer can only pass at most K - 1 under water cells in the horizontal direction.
In every cell, there is either a "speed changing machine" or a "money box" (can't be both). Every speed changing machine has a value T and every money box has a value P.
You have got enough number of devices called "speedo". Every speedo has a value Q which is 1 or -1. You can put your speedos in any cells, but you can put at most one speedo in a cell.
When Facer reaches a cell, the speed changing machine of value T in that cell (if there is one) will change his vertical speed by T, and the speedo of value Q in that cell (if there is one) will change his vertical speed by Q. For example, if Facer's vertical speed is v when he arrives at a cell with a speed changing machine of value T and a speedo of value Q, his vertical speed will be changed to v + T + Q immediately. There are three more things you need to know about speed changing:
- All speed changing machines in the surface cells can't work.
- When reaching a surface cell without a speedo, Facer's vertical speed becomes zero immediately; and when reaching a surface cell with a speedo of value Q, Facer's vertical speed becomes Q immediately.
- When reaching a bottom cell, though Facer can't go down any more, his vertical speed will NOT be changed unless there is a speedo or a speed changing machine in that cell. It's maybe sound a little bit weird but Facer really is such a weird guy.
When arriving at a cell with a money box of value P, Facer gets P dollars (In fact, if P is negative, Facer loses |P| dollars). Facer's total amount of money can be negative -- that means Facer owes some money to the pool's owner.
You task is deploying your speedos in a smart way before Facer starts swimming, so Facer can get money as much as possible when he arrives at cell (1, M). Facer's initial vertical speed is zero and if you put a speedo in the cell (1,1), it will change Facer's initial vertical speed.
Input
The first line contains three integers N,M and K ( 1 <= N <= 100, 1 <= M <= 1000, 1 <= K <= 10), tells the size of the pool and Facer must refill the oxygen bottle every K seconds.
Following are N lines describing the cells by top to bottom order. Each line contains M elements, separated by blanks, telling the information about the cells of a row, by left to right order. There are only two kinds of elements:
- a char "v" followed by an integer T, meaning that there is a "speed changing machine" of value T (-20 <= T <= 20) in the correspondent cell.
- a char "$" followed by an integer P, meaning that there is a "money box" of value P (-1000000 <= P <= 1000000) in the correspondent cell.
Output
题目大意:一个N*M的游泳池,要从(1, 1)游到(1, M),每个格子有变速器和钱(都可能为负),到了钱的地方可以拿到那些钱,到了变速器的地方速度就会加上那个变速器的速度。横向速度恒定为1,纵向速度可以改变,每次可以随意+1、-1或不变,变速器影响的也是纵向的速度。每K秒要到水面呼吸一次,求游到(1, M)的最大钱数。其他细节见题目。
思路:首先朴素的DP(如dp[N, M, V, K]的那种)是没法过的,时间复杂度太大了。另辟蹊径,注意到K很小,只有10,而且每次到水面速度会变为0,那么可以利用这一点,dp[i]表示到(1, i)时取得的最大金钱,然后对(1, i)爆搜K步,每次时间复杂度为O(3^K),总复杂度为O(M * 3^K),也不过6 * 10^7。
PS:这题坑爹的地方比较多,来一一细说。
1、每个格子的P (-1000000 <= P <= 1000000),乘个1000绝对爆32位整数,但是人人都用int我也用了int,不知道大家有没看到答案在32位以内反正我是没看到。
2、题目原句:and when reaching a surface cell with a speedo of value Q, Facer's vertical speed becomes Q immediately.
3、翻译一下:当到达水平而且那个格子有一个变速器Q,那么速度变为Q(不知道有没翻译错)
4、按这个说法,如果我所有格子都是V20,就不一定有答案了,题目是没说没答案怎么处理的……
5、重点是我按上面的说法做AC不了,改成到达水面之后速度为0不管那个格子有木有变速器就能AC,WA了几次之后对拍别人的AC代码发现的。
6、然后我看到了这句:All speed changing machines in the surface cells can't work。我承认我没认真看题但也不带这样坑我的啊……
代码(POJ 1219MS):
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL; const int MAXN = ;
const int MAXM = ;
const int INF = 0x3fff3fff; int dp[MAXM];
int mat[MAXN][MAXM];
bool money[MAXN][MAXM];
int n, m, k; void dfs(int from, int ti, int tj, int spd, int sum) {
if(ti < ) ti = ;
if(ti > n) ti = n;
if(ti == && from != tj) {
if(dp[tj] < sum) dp[tj] = sum;
return ;
}
if(tj - from == k || tj == m) return ;
if(money[ti][tj]) sum += mat[ti][tj];
else if(ti != ) spd += mat[ti][tj];
dfs(from, ti + spd - , tj + , spd - , sum);
dfs(from, ti + spd , tj + , spd , sum);
dfs(from, ti + spd + , tj + , spd + , sum);
} void solve() {
dp[] = ;
for(int i = ; i <= m; ++i) dp[i] = -INF;
for(int i = ; i < m; ++i) {
if(dp[i] == -INF) continue;
dfs(i, , i, , dp[i]);
}
//for(int i = 1; i <= m; ++i) printf("%d\n", dp[i] + money[1][i] * mat[1][i]);
if(money[][m]) dp[m] += mat[][m];
printf("%d\n", dp[m]);
} char lastch = ' '; inline char readchar() {
while(lastch != 'v' && lastch != '$')
lastch = getchar();
return lastch;
} inline int readint() {
int g = , l;
while(!isdigit(lastch) && lastch != '-') lastch = getchar();
if(lastch == '-') {
l = -;
lastch = getchar();
}
else l = ;
while(isdigit(lastch)) {
g = g * + lastch - '';
lastch = getchar();
}
return g * l;
} int main() {
while(true) {
n = readint(); m = readint(); k = readint();
if(n + m + k == ) break;
char c;
for(int i = ; i <= n; ++i) {
for(int j = ; j <= m; ++j) {
//scanf(" %c%d", &c, &mat[i][j]);
c = readchar();
mat[i][j] = readint();
money[i][j] = (c == '$');
}
}
solve();
}
}
代码(POJ 1922MS):
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL; const int MAXN = ;
const int MAXM = ;
const int INF = 0x3fff3fff; int dp[MAXM];
int mat[MAXN][MAXM];
bool money[MAXN][MAXM];
int n, m, k; void dfs(int from, int ti, int tj, int spd, int sum) {
if(ti < ) ti = ;
if(ti > n) ti = n;
if(ti == && from != tj) {
if(dp[tj] < sum) dp[tj] = sum;
return ;
}
if(tj - from == k || tj == m) return ;
if(money[ti][tj]) sum += mat[ti][tj];
else if(ti != ) spd += mat[ti][tj];
dfs(from, ti + spd - , tj + , spd - , sum);
dfs(from, ti + spd , tj + , spd , sum);
dfs(from, ti + spd + , tj + , spd + , sum);
} void solve() {
dp[] = ;
for(int i = ; i <= m; ++i) dp[i] = -INF;
for(int i = ; i < m; ++i) {
if(dp[i] == -INF) continue;
dfs(i, , i, , dp[i]);
}
if(money[][m]) dp[m] += mat[][m];
printf("%d\n", dp[m]);
} int main() {
while(scanf("%d%d%d", &n, &m, &k) != EOF) {
if(n + m + k == ) break;
char c;
for(int i = ; i <= n; ++i) {
for(int j = ; j <= m; ++j) {
scanf(" %c%d", &c, &mat[i][j]);
money[i][j] = (c == '$');
}
}
solve();
}
}
HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)的更多相关文章
- HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)
Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...
- HDU 3264/POJ 3831 Open-air shopping malls(计算几何+二分)(2009 Asia Ningbo Regional)
Description The city of M is a famous shopping city and its open-air shopping malls are extremely at ...
- HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)
Description Students often have problems taking up seats. When two students want the same seat, a qu ...
- HDU 3268/POJ 3835 Columbus’s bargain(最短路径+暴力枚举)(2009 Asia Ningbo Regional)
Description On the evening of 3 August 1492, Christopher Columbus departed from Palos de la Frontera ...
- HDU 3269 P2P File Sharing System(模拟)(2009 Asia Ningbo Regional Contest)
Problem Description Peer-to-peer(P2P) computing technology has been widely used on the Internet to e ...
- hdu 4081 Qin Shi Huang's National Road System(最小生成树+dp)2011 Asia Beijing Regional Contest
同样是看别人题解才明白的 题目大意—— 话说秦始皇统一六国之后,打算修路.他要用n-1条路,将n个城市连接起来,并且使这n-1条路的距离之和最短.最小生成树是不是?不对,还有呢.接着,一个自称徐福的游 ...
- HDU 3126 Nova [2009 Asia Wuhan Regional Contest Online]
标题效果 有着n巫妖.m精灵.k木.他们都有自己的位置坐标表示.冷却时间,树有覆盖范围. 假设某个巫妖攻击精灵的路线(他俩之间的连线)经过树的覆盖范围,表示精灵被树挡住巫妖攻击不到.求巫妖杀死所有精灵 ...
- hdu 3123 GCC (2009 Asia Wuhan Regional Contest Online)
GCC Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...
- HDU 3699 A hard Aoshu Problem(暴力枚举)(2010 Asia Fuzhou Regional Contest)
Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. N ...
随机推荐
- 个人免签即时到账收款接口 bufpay.com 支持多账号收款
有很多 bufpay 的用户反馈,单个手机收款有些时候不太方便,切换手机太麻烦:或者是营业额比较多,希望分摊到多个账号上面. 基于以上的问题,bufpay 开发了多手机收款的功能:每个收款的手机安装 ...
- 剑指Offer_编程题之替换空格
题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy.
- 02.将python3作为centos7的默认python命令
博客为日常工作学习积累总结: 由于个人兴趣爱好对python有了解: 1.安装Python3: 参考博客:https://zhuanlan.zhihu.com/p/47868341 安装依赖包: yu ...
- markdown常用命令(持续整理更新...)
编写使用的工具 VS Code 拥有丰富插件支持的代码编辑器,当然也支持markdown MdEditor一款在线编辑markdown网站 1.标题 示例: # 一级标题 ## 二级标题 ### 三级 ...
- md5的理解
md5之所以很难破解,是因为它是不可逆的(下面会解释),它是一种散列函数(哈希函数),并且是单向密码体制,即:从明文到密文的不可逆映射,只有加密过程没有解密过程. 为何说是不可逆映射呢?这是因为,md ...
- 我的Tmux学习笔记
0. 修改指令前缀 // ~/.tmux.conf ubind C-b set -g prefix C-a 1. 新建会话 tmux tmux new -s session-name // 可以设置会 ...
- Appointment Helper
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk.Que ...
- django创建第一个子应用-3
在Web应用中,通常有一些业务功能模块是在不同的项目中都可以复用的,故在开发中通常将工程项目拆分为不同的子功能模块,各功能模块间可以保持相对的独立,在其他工程项目中需要用到某个特定功能模块时,可以将该 ...
- golang区块链开发的视频教程推荐
目前网上关于golang区块链开发的资源很少,以太坊智能合约相关的课程倒是很多,可能是由于前者的难度比后者难度大,课程开发需要投入更多精力.搜了一圈之后没结果,我就直接去之前没覆盖的视频网站找资源,包 ...
- java的编码格式
几种常见的编码格式 为什么要编码 不知道大家有没有想过一个问题,那就是为什么要编码?我们能不能不编码?要回答这个问题必须要回到计算机是如何表示我们人类能够理解的符号的,这些符号也就是我们人类使用的语言 ...