HZNU Training 3—H

H - Commandos


commandos.in / standard output
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.

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, H ≤ 10) means in the floor number F room Y-X there are H hostages.

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
【分析】

题意:有一个酒店最高10层,每层有10*10个房间,坐标为(x,y,z),每次移动可以选择下一层x-1,或者右边和前方的房间y+1或z+1
,问你从(10,1,1)开始到最底层,能救多少人。
显然我们可以知道,(1,10,10)会是终点(假设每个房间都有人,答案路线一定是以这个房间为结尾,在到达第一层后,尽量遍历更多房间答案肯定会越大)。
脑补一下地图,可以发现这道题其实就是三维的金字塔dp...金字塔dp是二维从顶上走到最下层,这道题多了一维罢了.....所以就算脑补不出地图也没有关系,很容易可以发现,对当前(i,j,k)房间,这个房间的最优方案数一定是a[i][j][k]+max(f[i+1][j][k],f[i][j-1][k],f[i][j][k-1])
然后因为题目给定的规则是从(10,1,1)开始走,所以我觉得为了不给自己增加思考难度,最好也跟着题目规则走...所以层数10~1递减,每层地图(1,1)~(10,10)遍历即可,答案就是f[1][10][10];

【代码】

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int f[20][20][20];
int a[20][20][20];
int x,y,z,q,n,pp,X,Y,Z;

int main()
{
	FILE *p1;
	p1=fopen("commandos.in","r");
	fscanf(p1,"%d",&pp);
	while (pp--)
	{
		memset(a,0,sizeof(a));
		memset(f,0,sizeof(f));
		fscanf(p1,"%d",&n);
		while (n--)
		{
			fscanf(p1,"%d%d%d%d",&x,&y,&z,&q);
			a[x][y][z]+=q;
		}
		for (int i=10;i>=1;i--)
			for (int j=1;j<=10;j++)
				for (int k=1;k<=10;k++)
					f[i][j][k]=a[i][j][k]+max(max(f[i][j][k-1],f[i][j-1][k]),f[i+1][j][k]);
		printf("%d\n",f[1][10][10]);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值