Topcoder-srm610-div2-1000 (DP)

Source: Internet
Author: User

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.

At first glance, I found that N and m are very difficult to start with, but the question says that each time we can only move to the same row or column, or not move, there is no need to move many points, direct brute-force solution 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.