Commandos (空間上的dp,目前很少接觸。藉著這個機會學習一下,和平面上的dp是一樣的。)
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
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.
Input
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
Output
For each test case, print on a single line, a single number representing the maximum possible hostages that they could save.
Example
Input
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
Output
10 8
AC程式碼:
#include <iostream> #include <bits/stdc++.h> using namespace std; int mp[15][15][15]; int dp[15][15][15]; int main() { // freopen("commandos.in","r",stdin); int T; int n; int f,x,y,h; scanf("%d",&T); while(T--) { memset(mp,0,sizeof(mp)); memset(dp,0,sizeof(dp)); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d %d %d %d",&f,&x,&y,&h); mp[f][x][y] = h; } for(int k=10;k>=1;k--) { for(int i=1;i<=10;i++) { for(int j=1;j<=10;j++) { dp[k][i][j] = max(dp[k+1][i][j],max(dp[k][i-1][j],dp[k][i][j-1]))+mp[k][i][j]; } } } printf("%d\n",dp[1][10][10]); } return 0; }