H - 记忆里在微甜
Gym - 101147H
Statements
A commando is a soldier of an elite light infantry often specializing in amphibious landings, abseiling or parachuting. This time our Commando unit wants to free as many hostages as it could from a hotel in Byteland, This hotel contains 10 identical floors numbered from 1 to 10 each one is a suite of 10 by 10 symmetric square rooms, our unit can move from a room (F, Y, X) to the room right next to it (F, Y, X + 1) or front next to it (F, Y + 1, X) and it can also use the cooling system to move to the room underneath it (F - 1, Y, X).
Knowing that our unit parachuted successfully in room 1-1 in floor 10 with a map of hostages locations try to calculate the maximum possible hostages they could save.
Your program will be tested on one or more test cases. The first line of the input will be a single integer T. Followed by the test cases, each test case contains a number N (1 ≤ N ≤ 1, 000) representing the number of lines that follows. Each line contains 4 space separated integers (1 ≤ F, Y, X, H ≤ 10) means in the floor number F room Y-X there are H hostages.
For each test case, print on a single line, a single number representing the maximum possible hostages that they could save.
2 3 10 5 5 1 10 5 9 5 10 9 5 9 3 1 5 5 1 5 5 9 5 5 9 5 8
10 8
My Solution 题意:即普通的DAG加了一维。 DAG上的dp 三维的有向无环图上dp, dp[k][i][j] = max(dp[k][i][j], dp[k][i+1][j] + mp[k][i][j]);
dp[k][i][j] = max(dp[k][i][j], dp[k][i][j+1] + mp[k][i][j]);
dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j] + mp[k][i][j]);
复杂度 O(n^3)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 8;
int dp[12][12][12], mp[12][12][12];
int main()
{
freopen("commandos.in", "r", stdin);
#ifdef LOCAL
freopen("h.txt", "r", stdin);
//freopen("h.out", "w", stdout);
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);
int T, n, f, y, x, i, j, k;
cin >> T;
while(T--){
memset(dp, 0, sizeof dp);
memset(mp, 0, sizeof mp);
cin >> n;
for(i = 1; i <= n; i++){
cin >> f >> y >> x;
cin >> mp[f][y][x];
}
for(k = 1; k <= 10; k++){
for(i = 10; i >= 1; i--){
for(j = 10; j >= 1; j--){
dp[k][i][j] = max(dp[k][i][j], dp[k][i+1][j] + mp[k][i][j]);
dp[k][i][j] = max(dp[k][i][j], dp[k][i][j+1] + mp[k][i][j]);
dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j] + mp[k][i][j]);
}
}
}
cout << dp[10][1][1] << "\n";
}
return 0;
}
Thank you! ------from ProLights