I used DP instead of Greedy. But got WA on PoJ, though it passed all web-searched cases. Maybe I have to use Greedy.

BTW: a careless modification introduced an error, and it took me 1+ hours to debug. DETAILS

Sth. about this DP solution: dp[iLake][nCurrTotalTime + currentLakeTime] = dp[iLake - 1][nCurrTotalTime] + getFish(iLake, nCurrTotalTime, currentLakeTime);

Anyway, this is the first 2D DP problem I worked out :)

 #include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <vector>
using namespace std; #define Max(a, b) (a) > (b) ? (a) : (b)
#define MAX_LAKE 25
#define MAX_TIME (16*12) // We are at lake i now, with total time of tTtlLake spent on previous lakes,
// and we'd like to spend myt time on lake i -> how many fishes we can get from lake i?
int getFish(int i, int tTtlLake, int myt, int *accti, int *fi, int *di)
{
if (myt == ) return ; // If any left, get fishes by time myt
int ret = , left = fi[i];;
while (left > && myt > )
{
ret += left;
left -= di[i];
myt--;
}
return ret;
} void backtrack(int path[MAX_LAKE + ][MAX_TIME], int si, int sk, int n, int h)
{
bool bHasSolution = false;
int opath[MAX_LAKE + ] = { };
int actTtl = ;
while (si > && path[si][sk] != -)
{
opath[si] = path[si][sk];
actTtl += opath[si];
sk -= path[si][sk];
si--;
bHasSolution = true;
}
// Actual results less than h?
if (actTtl < h)
{
bool allAfterZero = true;
for (int i = ; i <= n; i++)
{
if (opath[i] != )
{
allAfterZero = false;
}
}
if (allAfterZero)
{
opath[] += h - actTtl;
}
}
if (!bHasSolution)
{
opath[] = h;
}
for (int i = ; i <= n; i++)
{
printf("%d", opath[i] * );
if (i < n) printf(", ");
}
printf("\n");
} void calc(int n, int h, int *fi, int *di, int *ti, int *accti)
{
// Reset
int dp[MAX_LAKE + ][MAX_TIME];
int path[MAX_LAKE + ][MAX_TIME];
memset( dp, , sizeof(int) * MAX_TIME * (MAX_LAKE + ));
memset(path, -, sizeof(int)* MAX_TIME * (MAX_LAKE + )); // dp[currentLakeIndex, currentTtlTimeOnLake] = currTtlFishCnt. Note: no time on the way included
// dp[i + 1, tTtl + t] = dp[i, tTtl] + f(fi(i + 1), t) int maxFish = , maxI, maxT;
for (int i = ; i <= n; i++)
{
// time on the way so far
int tWay = accti[i-]; for (int t = h; t >=; t--) // descending is necessary: it required more miniutes to spend as earlier as possible
{
if (i == && t > ) continue; int leftTime = h - t - tWay;
leftTime = leftTime < ? : leftTime; for (int k = ; k <= leftTime; k++) // k is how much time left in total
{
int newV = dp[i - ][t] + getFish(i, t, k, ti, fi, di);
if (newV > dp[i][t + k])
{
dp[i][t + k] = newV;
path[i][t + k] = k;
//printf("-putting %d to [%d][%d]\n", k, i, t + k); if (newV > maxFish)
{
maxFish = newV;
maxI = i; maxT = t + k;
}
}
}
}
} backtrack(path, maxI, maxT, n, h);
printf("Number of fish expected: %d\n\n", maxFish);
} int main()
{
int n, h;
while (scanf("%d", &n), (n != ))
{
scanf("%d", &h); // all in 5min-interval
h *= ; int fi[MAX_LAKE + ] = { }; // initial fish cnt
int di[MAX_LAKE + ] = { }; // decrease rate
int ti[MAX_LAKE] = { }; // travel time
int accti[MAX_LAKE] = { }; // travel time // Get Input
for (int i = ; i <= n; i ++) scanf("%d", fi + i);
for (int i = ; i <= n; i ++) scanf("%d", di + i);
for (int i = ; i <= n - ; i++)
{
scanf("%d", ti + i);
accti[i] = ti[i] + accti[i - ];
} calc(n, h, fi, di, ti, accti);
} return ;
}

POJ #1042 Gone Fishing - WA by a DP solution. TODO的更多相关文章

  1. POJ 1042 Gone Fishing( DP )

    题意:小明打算做一个h((1 <= h <= 16))个小时钓鱼旅行.发现这里有n(2 <= n <= 25)个湖,而且所有的湖都在一条路的旁边.小明打算从第1个湖开始钓起,每 ...

  2. POJ 1042 Gone Fishing

    题意:一个人要在n个湖中钓鱼,湖之间的路径是单向的,只能走1->2->3->...->n这一条线路,告诉你每个湖中一开始能钓到鱼的初始值,和每钓5分钟就减少的数量,以及湖之间的 ...

  3. POJ 1042 Gone Fishing (贪心)(刘汝佳黑书)

    Gone Fishing Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 30281   Accepted: 9124 Des ...

  4. poj -- 1042 Gone Fishing(枚举+贪心)

    题意: John现有h个小时的空闲时间,他打算去钓鱼.钓鱼的地方共有n个湖,所有的湖沿着一条单向路顺序排列(John每在一个湖钓完鱼后,他只能走到下一个湖继续钓),John必须从1号湖开始钓起,但是他 ...

  5. POJ 1042 Gone Fishing#贪心

    (- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cstring> using namespace std ...

  6. POJ 3249 Test for Job (拓扑排序+DP)

    POJ 3249 Test for Job (拓扑排序+DP) <题目链接> 题目大意: 给定一个有向图(图不一定连通),每个点都有点权(可能为负),让你求出从源点走向汇点的路径上的最大点 ...

  7. leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)

    leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...

  8. Poj/OpenJudge 1042 Gone Fishing

    1.链接地址: http://bailian.openjudge.cn/practice/1042/ http://poj.org/problem?id=1042 2.题目: Gone Fishing ...

  9. Gone Fishing POJ 1042

    #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> us ...

随机推荐

  1. Redis的初步安装

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 下载 官网下载:http://redis.io/downlo ...

  2. 2分钟读懂Hadoop和Spark的异同

    谈到大数据框架,现在最火的就是Hadoop和Spark,但我们往往对它们的理解只是提留在字面上,并没有对它们进行深入的思考,倒底现在业界都在使用哪种技术?二者间究竟有哪些异同?它们各自解决了哪些问题? ...

  3. spingmvc 返回json数据日期格式化方法

    第一种: json 用的是这个依赖 <!-- JSON lib 开发包 以及它的依赖包 --> <dependency> <groupId>com.fasterxm ...

  4. Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  5. GridView用法大全(转)

    http://www.cnblogs.com/sufei/archive/2010/03/27/1698590.html

  6. makefile--模式规则(七)

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 上一节讲到目录创建成功,目标文件没有生产到对应目录下,这里我们先给目标文件加上对应目录,这样的话 ...

  7. leetcode 154. Find Minimum in Rotated Sorted Array II --------- java

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  8. URAL 1072 Routing(最短路)

    Routing Time limit: 1.0 secondMemory limit: 64 MB There is a TCP/IP net of several computers. It mea ...

  9. IE 下加载jQuery

    转:http://www.iitshare.com/ie8-not-use-native-json.html 解决在IE8中无法使用原生JSON的问题   起因 在项目中要将页面上的js对象传给后台, ...

  10. android开源项目---blog篇

    本文转载于:http://blog.csdn.net/likebamboo/article/details/19081241 主要介绍那些乐于分享并且有一些很不错的开源项目的个人和组织.Follow大 ...