UVa10259 - Hippity Hopscotch(动态规划)

本文介绍了一个基于网格的跳跃游戏,玩家从起点出发,在限定条件下选择路径收集硬币,目标是获得尽可能多的硬币。文章提供了游戏的具体规则说明,并附带了实现这一策略的算法代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

Given n, k, and the number of pennies at each grid location, compute themaximum number of pennies that the contestant can pick up before beingunable 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
题意:从(0,0)位置开始,可以水平或者竖直跳转不大于k的步数,跳的下一步中的值要大小当前步的值,而结果是经过的方格的数的和,求其最大值

思路:f(x,y) = grid[x][y] + max{f[x][y - 1], ...,f[x][y - k], f[x][y + 1],...,f[x][y + k], f[x - 1][y],......,f[x - k][y], f[x + 1][y], ...., f[x + k][y]})

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 110;

int grid[MAXN][MAXN];
int f[MAXN][MAXN];
int n, k;

void input()
{
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			scanf("%d", &grid[i][j]);
		}
	}
}

int dp(int x, int y)
{
	if (f[x][y] != -1) return f[x][y];

	int tmp = grid[x][y];
	for (int i = 1; i <= k; i++) {
		if (x + i < n && grid[x + i][y] > grid[x][y]) {
			tmp = max(tmp, grid[x][y] + dp(x + i, y));
		}

		if (x - i >= 0 && grid[x - i][y] > grid[x][y]) {
			tmp = max(tmp, grid[x][y] + dp(x - i, y));
		}

		if (y + i < n && grid[x][y + i] > grid[x][y]) {
			tmp = max(tmp, grid[x][y] + dp(x, y + i));
		}

		if (y - i >= 0 && grid[x][y - i] > grid[x][y]) {
			tmp = max(tmp, grid[x][y] + dp(x, y - i));
		}
	}

	return f[x][y] = tmp;

}

void solve()
{
	memset(f, -1, sizeof(f));
	dp(0, 0);
	printf("%d\n", f[0][0]);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif

	int cas;
	scanf("%d", &cas);
	while (cas--) {
		input();
		solve();
		if (cas) printf("\n");
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值