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)