题目:http://community.topcoder.com/stat?c=problem_statement&pm=12931
关键是要看出,每天所在的位置只能在特定的位置。
代码:
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
#define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
typedef pair<int, int> pii;
typedef long long llong;
typedef pair<llong, llong> pll;
#define mkp make_pair
/*************** Program Begin **********************/
int dp[55][55][55];
class MiningGoldEasy {
public:
int N, M, endays;
vector <int> event_i, event_j;
int rec(int cur, int x, int y) // 已过天数cur,此时所在位置为 ( event_i[x], event_j[y] ),
// 返回最小距离。
{
if (cur == endays) {
return 0;
}
int & res = dp[cur][x][y];
if (res != -1) {
return res;
}
res = INT_MAX;
int dis = abs(event_i[cur] - event_i[x]) + abs(event_j[cur] - event_j[y]);
for (int i = 0; i < endays; i++) {
// 竖直方向
res = min(res, dis + rec(cur + 1, x, i));
// 水平方向
res = min(res, dis + rec(cur + 1, i, y));
}
return res;
}
int GetMaximumGold(int N, int M, vector <int> event_i, vector <int> event_j) {
int res = 0;
this->N = N;
this->M = M;
this->event_i = event_i;
this->event_j = event_j;
this->endays = event_i.size();
res = INT_MAX;
memset(dp, -1, sizeof(dp));
for (int i = 0; i < endays; i++) {
for (int j = 0; j < endays; j++) {
res = min(res, rec(0, i, j));
}
}
res = (N + M) * endays - res;
return res;
}
};
/************** Program End ************************/