CFGym - 101147H. Commandos 动态规划

在一个10x10x10的建筑中,特种部队需要在限定时间内从顶层开始,通过移动到相邻房间来解救人质。任务是计算能解救的最大人质数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.题目描述:

H. Commandos
time limit per test
2 seconds
memory limit per test
64 megabytes
input
commandos.in
output
standard output

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
2.题意概述:

在一个10x10x10的楼里面有人质在不同楼层不同方位,特种队员在 (F, Y, X)单位时

间可以向(F, Y, X + 1)或者(F, Y + 1, X)或者(F - 1, Y, X)方向走,同时解救该方向人质,

问你最多可以解救多少人质

3.解题思路:

既可以搜索,也可以dp,我当时做法是dp,转移方程

dp[i][j][k] = max(dp[i][j][k], max(dp[i - 1][j][k], max(dp[i][j - 1][k], dp[i][j][k - 1]))) + a[i][j][k]; }

注意的是自顶部向下走,则要对楼层进行颠倒

4.Ac代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <ctime>
#define N 11
using namespace std;
int a[N][N][N];
int dp[N][N][N];

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        long _begin_time = clock();
    #endif
    freopen("commandos.in", "r", stdin);
	int t, n;
	scanf("%d", &t);
	while (t--)
	{
        memset(a, 0, sizeof(a));
		memset(dp, 0, sizeof(dp));
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
		{
            int f, y, x, h;
            scanf("%d%d%d%d", &f, &y, &x, &h);
            a[11 - f][y][x] = h;
		}
        for (int i = 1; i <= 10; i++)
        {
            for (int j = 1; j <= 10; j++)
            {
                for (int k = 1; k <= 10; k++)
                {
                    dp[i][j][k] = max(dp[i][j][k], max(dp[i - 1][j][k], max(dp[i][j - 1][k], dp[i][j][k - 1]))) + a[i][j][k];
                }
            }
        }
        printf("%d\n", dp[10][10][10]);
	}
    #ifndef ONLINE_JUDGE
        long _end_time = clock();
        printf("time = %ld ms\n", _end_time - _begin_time);
    #endif
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值