SRM 610 D2L3:MiningGoldEasy,dp

题目: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 ************************/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值