FatMouse and Cheese
Problem Description
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Now he's going to enjoy his favorite food.
FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse -- after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.
Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move.
Input
There are several test cases. Each test case consists of
a line containing two integers between 1 and 100: n and k
n lines, each with n numbers: the first line contains the number of blocks of cheese at locations (0,0) (0,1) ... (0,n-1); the next line contains the number of blocks of cheese at locations (1,0), (1,1), ... (1,n-1), and so on.
The input ends with a pair of -1's.
Output
For each test case output in a line the single integer giving the number of blocks of cheese collected.
Sample Input
3 1
1 2 5
10 11 6
12 12 7
-1 -1
Sample Output
37
Source
Zhejiang University Training Contest 2001
题目类型:线性DP(记忆化搜索)
算法分析:dp[i][j]表示(i, j)位置所最终能够得到的最大的奶酪数,则状态转移方程为dp[i][j] = max (dp[k][q]) + aa[i][j],其中aa[k][q] < aa[i][j],而(k, q)合理且由(i,j)经过不超过k步转移过来,最后直接找dp[i][j]的最大值即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
/************************************************* Author :supermaker Created Time :2015/12/2 21:58:21 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; LL dp[166][166], aa[166][166], n, k; const int dx[] = {0, 0, -1, 1}; const int dy[] = {-1, 1, 0, 0}; bool In (int x, int y) { if (x >= 1 && x <= n && y >= 1 && y <= n) return true; return false; } LL dfs (int x, int y) { LL res = 0; if (dp[x][y]) return dp[x][y]; for (int i = 1; i <= k; i++) { for (int j = 0; j < 4; j++) { int tx = x + i * dx[j], ty = y + i * dy[j]; if (In (tx, ty) && aa[tx][ty] > aa[x][y]) res = max (res, dfs (tx, ty)); } } return dp[x][y] = res + aa[x][y]; } int main() { //CFF; //CPPFF; while (scanf ("%lld%lld", &n, &k) != EOF) { if (n == -1 && k == -1) break; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) scanf ("%lld", &aa[i][j]); memset (dp, 0, sizeof (dp)); dfs (1, 1); LL maxval = -INF; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) maxval = max (maxval, dp[i][j]); printf ("%lld\n", maxval); } return 0; } |
- « 上一篇:poj3616
- hdu5115:下一篇 »