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 ...
随机推荐
- Codeforces Round #487 (Div. 2)
A. A Blend of Springtime(暴力/模拟) 题目大意 给出$n$个花,每个点都有自己的颜色,问是否存在连续大于等于三个花颜色均不相同 sol 直接模拟判断即可 #include&l ...
- 竞赛题解 - [CF 1080D]Olya and magical square
Olya and magical square - 竞赛题解 借鉴了一下神犇tly的博客QwQ(还是打一下广告) 终于弄懂了 Codeforces 传送门 『题目』(直接上翻译了) 给一个边长为 \( ...
- JavaBen 中 如何将字段设置为 "text" 文本类型
@Lob @Column(name="FEEDBACK_MESSAGE",columnDefinition="TEXT", nullable=true) pub ...
- jQuery获取Select option 选择的Text和 Value
获取一组radio被选中项的值:var item = $('input[name=items][checked]').val();获取select被选中项的文本var item = $("s ...
- JS 匿名函数或自执行函数总结
JS引擎在遇到function关键字时做如下两种处理: 1.当语句是以function关键字开头:此时的JS语句解释为函数声明,因此function关键字后面必须要跟函数名字,如果写成匿名函数,则会报 ...
- button onclick实现跳转的常用方法
1.onclick="javascript:window.location.href='aa.htm' " 2.onclick="location='URL' " ...
- windows 10 安装node.js
第一步:下载软件 nodejs的中文官网http://nodejs.cn/download/ 选择 windows 系统 msi 安装版本. 下载完成之后,直接打开下一步安装就可以. 安装完成 打开 ...
- QP之QK原理
QK是一个很小的抢占式微内核调度程序,它专用用QP中. QK的思想源于SST,Miro Samek重写了自己前期编的SST(Super Simple Task)代码. QK循环查询AO队列的状态表QK ...
- core dump文件分析和调试
core介绍 当程序运行的过程中异常终止或崩溃,操作系统会将程序当时的内存状态记录下来,保存在一个文件中,这种行为就叫做Core Dump(中文有的翻译成"核心转储").我们可以认 ...
- SSH Secure :Algorithm negotiation failed,反复提示输入password对话框
在嵌入式开发中,SSH Secure File Transfer Client 软件使用,方便了windows和linux之间文件拷贝,尤其是多台主机状况下. 最近装了Ubuntu 16.0.4,在V ...