實機體驗,再天天抽M8金豪禮★拿現金最實在!吉隆坡最便宜行程一覽表療傷止痛,王郁琦先下台!
2013-07-31 18:29:06 人氣(23) | 回應(0) | 推薦(0) | 收藏(0) 上一篇 | 下一篇

[UVA][DP] 10259 - Hippity Hopscotch

0
收藏
0
推薦

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;
}
 

10259Hippity Hopscotchdp
台長:Morris
人氣(23) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][dp] 10350 - Liftless EME
此分類上一篇:[UVA] 11047 - The Scrooge Co Problem

我要回應
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入以下數字 (ex:123)

(有*為必填)
詳全文