Description

Facer is addicted to a game called "Tidy is learning to swim". But he finds it too easy. So he develops a new game called "Facer is learning to swim" which is more difficult and more interesting. 
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: 
  1. All speed changing machines in the surface cells can't work.
  2. 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.
  3. 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

Multiple test cases, ended by a line of "0 0 0". For each test case: 
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: 
  1. 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.
  2. 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

For each test case, output one line containing an integer representing the maximum amount of money Facer can get when he reaches cell (1, M).

题目大意:一个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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. hdu 4081 Qin Shi Huang's National Road System(最小生成树+dp)2011 Asia Beijing Regional Contest

    同样是看别人题解才明白的 题目大意—— 话说秦始皇统一六国之后,打算修路.他要用n-1条路,将n个城市连接起来,并且使这n-1条路的距离之和最短.最小生成树是不是?不对,还有呢.接着,一个自称徐福的游 ...

  7. HDU 3126 Nova [2009 Asia Wuhan Regional Contest Online]

    标题效果 有着n巫妖.m精灵.k木.他们都有自己的位置坐标表示.冷却时间,树有覆盖范围. 假设某个巫妖攻击精灵的路线(他俩之间的连线)经过树的覆盖范围,表示精灵被树挡住巫妖攻击不到.求巫妖杀死所有精灵 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 关于nodejs下载组件经常失败的问题

    由于最近在刚开始做一个前台element和mybatisplus的项目,但是在使用nodejs下载vue的脚手架和各种组件时,会经常出现下载失败的问题,进而导致前台无法启动. 在网上查询之后发现在下载 ...

  2. jquery--DOM操作基础

    元素的访问 元素属性操作 获取:attr(name):$("#my").attr("src"); 设置:attr(name,value):$("#my ...

  3. 如何在tornado中以异步的方式调用同步函数

    问题 如何在tornado的coroutine中调用同步阻塞的函数 解决方案 使用python内置标准库的concurrent.futures.ThreadPoolExecutor和tornado.c ...

  4. Django学习之mysql结果显示

    背景:向数据库添加相同名字的用户 上节可知,在查询某个用户信息时,只能查看到行数但不能查看结构性信息. fetchone()函数可以但仅可逐个查看结构性信息.查不到信息将以None替补. fetchA ...

  5. C语言基础——链表的相关操作

    1 #include <stdio.h> #include <malloc.h> #include <string.h> #include <math.h&g ...

  6. C语言学习记录

    思路: 工具书: <c程序设计语言> R&K <linux C 编程一站式学习>

  7. lambda方法的引用与构造方法引用

    方法的引用 /** * @auther hhh * @date 2018/12/29 22:37 * @description */ public class ObjectMethodUse { /* ...

  8. 成都Uber优步司机奖励政策(1月29日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. 使用AutoFac实现依赖注入(封装一个注册类)

    public class AutoFacBootStrapper { public static void CoreAutoFacInit() { var builder = new Containe ...

  10. java nio之Buffer

    一.JAVA NIO 是在和channel交互的时候使用的.Channel将数据读入缓冲区,然后我们又从缓冲区访问数据.写数据时,首先将要发送的数据按顺序填入缓冲区.基本上,缓冲区只是一个列表,它的所 ...