码迷,mamicode.com
首页 > 其他好文 > 详细

topcoder-srm610-div2-1000(dp)

时间:2015-05-07 16:41:22      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:dp   topcoder   

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

topcoder-srm610-div2-1000(dp)

标签:dp   topcoder   

原文地址:http://blog.csdn.net/guard_mine/article/details/45562777

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!