2013-07-31 18:29:06Morris

[UVA][DP] 10259 - Hippity Hopscotch

Problem C - Hippity Hopscotch

The game of hopscotch involves chalk, sidewalks, jumping, and picking things up. Our variant of hopscotch involves money as well. The game is played on a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. Each grid location has on it a stack of between 0 and 100 pennies.

A contestant begins by standing at location (0,0). The contestant collects the money where he or she stands and then jumps either horizontally or vertically to another square. That square must be within the jumping capability of the contestant (say, k locations) and must have more pennies than those that were on the current square.

Given n, k, and the number of pennies at each grid location, compute the maximum number of pennies that the contestant can pick up before being unable to move.

Input Specification

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

  • 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 pennies at locations (0,0) (0,1) ... (0,n-1); the next line contains the number of pennies at locations (1,0), (1,1), ... (1,n-1), and so on.

Output Specification

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

  • a single integer giving the number of pennies collected

Sample Input

1

3 1
1 2 5
10 11 6
12 12 7

Output for Sample Input

37

由於有中文翻譯,就不多作說明。

題目解法:

很明顯地,由於只能往更高的數字爬,因此圖形是一個 DAG,那麼考慮將所有數字排序,
然後進行 DP 的更新,藉此得到最大值。

#include <stdio.h>
#include <algorithm>
using namespace std;
struct E {
int i, j, v;
bool operator<(const E &a) const {
return v < a.v;
}
};
int main() {
E D[10005];
int testcase;
int i, j, k, x, y;
int N, K;
int g[105][105], dp[105][105];
scanf("%d", &testcase);
while(testcase--) {
scanf("%d %d", &N, &K);
int n = 0;
for(i = 0; i < N; i++) {
for(j = 0; j < N; j++) {
scanf("%d", &g[i][j]);
D[n].i = i, D[n].j = j;
D[n].v = g[i][j];
n++;
dp[i][j] = 0;
}
}
sort(D, D+n);
dp[0][0] = g[0][0];
int can[105][105] = {};
for(i = 0; i < n; i++) {
if(D[i].i == 0 && D[i].j == 0)
break;
}
can[0][0] = 1;
for(; i < n; i++) {
if(can[D[i].i][D[i].j] == 0) continue;
j = 1, x = D[i].i+1, y = D[i].j;
while(x < N && j <= K) {
if(g[x][y] > D[i].v) {
dp[x][y] = max(dp[x][y], dp[D[i].i][D[i].j]+g[x][y]);
can[x][y] = 1;
}
x++, j++;
}
j = 1, x = D[i].i, y = D[i].j+1;
while(y < N && j <= K) {
if(g[x][y] > D[i].v) {
dp[x][y] = max(dp[x][y], dp[D[i].i][D[i].j]+g[x][y]);
can[x][y] = 1;
}
y++, j++;
}
j = 1, x = D[i].i-1, y = D[i].j;
while(x >= 0 && j <= K) {
if(g[x][y] > D[i].v) {
dp[x][y] = max(dp[x][y], dp[D[i].i][D[i].j]+g[x][y]);
can[x][y] = 1;
}
x--, j++;
}
j = 1, x = D[i].i, y = D[i].j-1;
while(y >= 0 && j <= K) {
if(g[x][y] > D[i].v) {
dp[x][y] = max(dp[x][y], dp[D[i].i][D[i].j]+g[x][y]);
can[x][y] = 1;
}
y--, j++;
}
}

int ret = 0;
for(i = 0; i < N; i++) {
for(j = 0; j < N; j++) {
ret = max(ret, dp[i][j]);
}
}
printf("%d\n", ret);
if(testcase) puts("");
}
return 0;
}