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. 如何编写及运行JS

    JS也是一种脚本语言,他可以有两种方式在HTML页面进行引入,一种是外联,一种是内部.       外联JS的写法为: <script src="相对路径"></ ...

  2. redux和react-redux的使用详解

    我自己的理解redux就跟vue中的vuex差不多,都是数据管理器,话不多说,我们从经典的计数器案例开始讲解 使用redux实现计数器 创建如下的react项目,我习惯把每一个模块分块,才有这么多文件 ...

  3. c和c++单链表

    c++版 #include<iostream> #include<malloc.h> using namespace std; struct node{ int data; n ...

  4. MYSQL 8.0.11 安装过程及 Navicat 链接时遇到的问题

    参考博客:https://blog.csdn.net/WinstonLau/article/details/78666423 我的系统和软件版本是这样的: 系统环境:win7.64位 MySQL版本: ...

  5. Spring security学习笔记(二)

    对比两种承载认证信息的方式: session vs token token验证方案: session验证方案: session即会话是将用户信息保存在服务端,根据请求携带的session_id,从服务 ...

  6. Elasticsearch 映射操作

    一.创建 语法: PUT /索引库名称/_mapping/类型名称 { "properties": { "字段名": { "type": 类 ...

  7. centos7 杂记

    yum 源 https://www.cnblogs.com/renpingsheng/p/7845096.html 安装nginx php mysql https://www.cnblogs.com/ ...

  8. 《HTML与CSS知识》系列分享专栏

    收藏HTML和CSS方面的技术文章,作为一个WEB开发者,必须要知道HTML和CSS方面的知识,即使作为后台开发者也应该知道一些常用的HTML和CSS知识,甚至架构师也要了解,这样才会开发出实用的网站 ...

  9. java语言描述 猴子吃桃问题(递归和循环写法)

    //题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个//第二天早上又将剩下的桃子吃掉一半,又多吃了一个//以后每天早上都吃了前一天剩下 的一半零一个.到第10天早上想再 ...

  10. 慎用静态类static class

    偶然翻到一篇有趣的帖子: class - When to Use Static Classes in C# - Stack Overflowhttp://stackoverflow.com/quest ...