topcoder-srm610-div2-1000(dp)

5 篇文章 0 订阅

Problem Statement

You are a goblin miner. Your job is to mine gold.

Picture yourself located in a mine. The mine can be seen as a rectangular grid of (N+1) times (M+1) cells. Rows are numbered 0 through N, columns 0 through M.

You will work in the mine for several days. You can choose the cell where you will work today (on day 0). On each of the next days, you can either stay in the cell where you were on the previous day, or you can move to any other cell in the same row or in the same column.

Whenever somebody discovers gold in the mine, each goblin profits! Your profit is N + M, minus your Manhattan distance from the cell where the gold was discovered. Formally, if the gold is discovered at (a, b) and you are located at (c, d), your profit is N + M - |a-c| - |b-d|, where || denotes absolute value.

You are given the ints N and M. You are also given two vector s: event_i and event_j. Both will contain the same number of elements. Assume that for each valid k, there will be exactly one gold discovery on day k: in the cell (event_i[k], event_j[k]).

Compute and return the maximum total profit you can get by correctly choosing the cells where you work on each day.

初看之下发现N和M很大不好下手,但是题目说每次只能移动到同一行或者同一列的位置,或者不移动,所以其实很多点是没有必要走的,直接暴力n^3解决

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int dp[55][55][55];

class MiningGoldEasy {
    public:
        int GetMaximumGold(int N, int M, vector <int> X, vector <int> Y) {

            memset(dp, 0, sizeof(dp));
            int n = X.size();

            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    dp[0][i][j] = N + M - abs(X[i] - X[0]) - abs(Y[j] - Y[0]);
                }
            }

            for (int k = 1; k < n; ++k) {
                for (int i = 0; i < n; ++i) {
                    for (int j = 0; j < n; ++j) {
                        int tmp = 0;
                        for (int l = 0; l < n; ++l) {
                            tmp = max(tmp, dp[k - 1][l][j] + N + M - abs(X[i] - X[k]) - abs(Y[j] - Y[k]));
                            tmp = max(tmp, dp[k - 1][i][l] + N + M - abs(X[i] - X[k]) - abs(Y[j] - Y[k]));
                        }
                        dp[k][i][j] = tmp;
                    }
                }
            }

            int ans = 0;

            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    ans = max(ans, dp[n - 1][i][j]);
                }
            }

            return ans;
        }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值