Codeforces Round 938 Div.3 H. The Most Reckless Defense 题解 状压dp

CodeForces - 1102F 状态压缩dp 状态转移 阅读详情

The Most Reckless Defense

题目描述

You are playing a very popular Tower Defense game called “Runnerfield 2”. In this game, the player sets up defensive towers that attack enemies moving from a certain starting point to the player’s base.

You are given a grid of size n × m n \times m n×m, on which k k k towers are already placed and a path is laid out through which enemies will move. The cell at the intersection of the x x x-th row and the y y y-th column is denoted as ( x , y ) (x, y) (x,y).

Each second, a tower deals p i p_i pi​ units of damage to all enemies within its range. For example, if an enemy is located at cell ( x , y ) (x, y) (x,y) and a tower is at ( x i , y i ) (x_i, y_i) (xi​,yi​) with a range of r r r, then the enemy will take damage of p i p_i pi​ if ( x − x i ) 2 + ( y − y i ) 2 ≤ r 2 (x - x_i) ^ 2 + (y - y_i) ^ 2 \le r ^ 2 (x−xi​)2+(y−yi​)2≤r2.

Enemies move from cell ( 1 , 1 ) (1, 1) (1,1) to cell ( n , m ) (n, m) (n,m), visiting each cell of the path exactly once. An enemy instantly moves to an adjacent cell horizontally or vertically, but before doing so, it spends one second in the current cell. If its health becomes zero or less during this second, the enemy can no longer move. The player loses if an enemy reaches cell ( n , m ) (n, m) (n,m) and can make one more move.

By default, all towers have a zero range, but the player can set a tower’s range to an integer r r r ( r > 0 r>0 r>0), in which case the health of all enemies will increase by 3 r 3^r 3r. However, each r r r can only be used for at most one tower.

Suppose an enemy has a base health of h h h units. If the tower ranges are 2 2 2, 4 4 4, and 5 5 5, then the enemy’s health at the start of the path will be h + 3 2 + 3 4 + 3 5 = h + 9 + 81 + 243 = h + 333 h + 3 ^ 2 + 3 ^ 4 + 3 ^ 5 = h + 9 + 81 + 243 = h + 333 h+32+34+35=h+9+81+243=h+333. The choice of ranges is made once before the appearance of enemies and cannot be changed after the game starts.

Find the maximum amount of base health h h h for which it is possible to set the ranges so that the player does not lose when an enemy with health h h h passes through (without considering the additions for tower ranges).

输入格式

The first line contains an integer t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1≤t≤100) — the number of test cases.

The first line of each test case contains three integers n n n, m m m, and k k k ( 2 ≤ n , m ≤ 50 , 1 ≤ k < n ∗ m 2 ≤ n, m ≤ 50, 1 ≤ k < n * m 2≤n,m≤50,1≤k<n∗m) — the dimensions of the field and the number of towers on it.

The next n n n lines each contain m m m characters — the description of each row of the field, where the character “.” denotes an empty cell, and the character “#” denotes a path cell that the enemies will pass through.

Then follow k k k lines — the description of the towers. Each line of description contains three integers x i x_i xi​, y i y_i yi​, and p i p_i pi​ ( 1 ≤ x i ≤ n , 1 ≤ y i ≤ m , 1 ≤ p i ≤ 500 1 \le x_i \le n, 1 \le y_i \le m, 1 \le p_i \le 500 1≤xi​≤n,1≤yi​≤m,1≤pi​≤500) — the coordinates of the tower and its attack parameter. All coordinates correspond to empty cells on the game field, and all pairs ( x i , y i ) (x_i, y_i) (xi​,yi​) are pairwise distinct.

It is guaranteed that the sum of n ⋅ m n \cdot m n⋅m does not exceed 2500 2500 2500 for all test cases.

输出格式

For each test case, output the maximum amount of base health h h h on a separate line, for which it is possible to set the ranges so that the player does not lose when an enemy with health h h h passes through (without considering the additions for tower ranges).

If it is impossible to choose ranges even for an enemy with 1 1 1 unit of base health, output “0”.

样例 #1

样例输入 #1

6
2 2 1
#.
##
1 2 1
2 2 1
#.
##
1 2 2
2 2 1
#.
##
1 2 500
3 3 2
#..
##.
.##
1 2 4
3 1 3
3 5 2
#.###
#.#.#
###.#
2 2 2
2 4 2
5 5 4
#....
#....
#....
#....
#####
3 2 142
4 5 9
2 5 79
1 3 50

样例输出 #1

0
1
1491
11
8
1797

提示

In the first example, there is no point in increasing the tower range, as it will not be able to deal enough damage to the monster even with 1 1 1 unit of health.

In the second example, the tower has a range of 1 1 1, and it deals damage to the monster in cells ( 1 , 1 ) (1, 1) (1,1) and ( 2 , 2 ) (2, 2) (2,2).

In the third example, the tower has a range of 2 2 2, and it deals damage to the monster in all path cells. If the enemy’s base health is 1491 1491 1491, then after the addition for the tower range, its health will be 1491 + 3 2 = 1500 1491 + 3 ^ 2 = 1500 1491+32=1500, which exactly equals the damage the tower will deal to it in three seconds.

In the fourth example, the tower at ( 1 , 2 ) (1, 2) (1,2) has a range of 1 1 1, and the tower at ( 3 , 1 ) (3, 1) (3,1) has a range of 2 2 2.

原题

CF——传送门
洛谷——传送门

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
// 若R大于12,500⋅π⋅r^2−3^r<0 那么塔楼对敌人的血量削减为负贡献,所以R最大即为12
// 由于R很小,所以采用状压dp枚举半径分布的子集
 
void solve()
{
    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<char>> ground(n + 1, vector<char>(m + 1)); // 场地
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> ground[i][j];
        }
    }
    vector<int> tower_x(k); // 塔楼的纵坐标
    vector<int> tower_y(k); // 塔楼的横坐标
    vector<int> p(k);       // 塔楼的攻击参数
    for (int i = 0; i < k; i++)
    {
        cin >> tower_x[i] >> tower_y[i] >> p[i];
    }
    vector<vector<int>> cover(k, vector<int>(13)); // 第i+1座塔楼半径为j时覆盖的路径点的数量
    for (int i = 0; i < k; i++)
    {
        for (int j = 1; j <= 12; j++)
        {
            int cnt = 0; // 覆盖的路径点的数量
            int x = tower_x[i];
            int y = tower_y[i];
            for (int nx = x - j; nx <= x + j; nx++) // 枚举以塔楼为中心点,2*j+1为边长的正方形
            {
                for (int ny = y - j; ny <= y + j; ny++)
                {
                    if (nx >= 1 && nx <= n && ny >= 1 && ny <= m)         // 不越界
                        if (pow(x - nx, 2) + pow(y - ny, 2) <= pow(j, 2)) // 在圆内
                            if (ground[nx][ny] == '#')                    // 是路径点
                                cnt++;
                }
            }
            cover[i][j] = cnt;
        }
    }
    // 由于每个r最多对应一个塔楼,所以最多为12个塔楼设置攻击范围
    int num = 12;
    vector<vector<int>> dp(k + 1, vector<int>((1 << num), -1e9)); // dp[i][j]表示设置前i个塔楼,半径状态压缩为j时塔楼可造成的最大伤害
    dp[0][0] = 0;
    for (int i = 1; i <= k; i++)
        for (int s = 0; s < (1 << num); s++) // s为上一行的dp状态
        {
            dp[i][s] = max(dp[i][s], dp[i - 1][s]);
            for (int j = 0; j < num; j++)
            {
                int s2 = s | (1 << j); // 状态压缩加上该塔楼该半径
                if (s2 != s)
                {
                    dp[i][s2] = max(dp[i][s2], dp[i - 1][s] + cover[i - 1][j + 1] * p[i - 1]);
                }
            }
        }
    int ans = 0;
    for (int s = 0; s < (1 << num); s++)
    {
        int ex = 0;   // 额外增加的敌人生命值
        int ppow = 3; // 3的r次方
        for (int j = 0; j < num; j++)
        {
            if ((s >> j) & 1)
            {
                ex += ppow;
            }
            ppow *= 3;
        }
        for (int j = 0; j <= k; j++)
        {
            ans = max(ans, dp[j][s] - ex);
        }
    }
    cout << ans << '\n';
 
    return;
}
 
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
 
    return 0;
}
深入浅出卡尔曼滤波器算法 深入浅出讲解卡尔曼滤波器的工作原理及其公式推导,讨论卡尔曼滤波器的核心公式所代表的意义及个人的理解。 阅读详情

相关推荐

ESP32锂电池电量检测实战:从引脚选择到低功耗优化(附完整电路图)

本文详细介绍了ESP32锂电池电量检测的实战方案,从ADC引脚选择到低功耗优化,提供完整的电路图和代码实现。通过MOS管智能开关和深度睡眠技术,显著降低静态功耗至0.5μA,提升设备续航能力。适合物联网开发者解决电量检测中的常见问题。

weixin_28731223的博客 461

Codeforces 1550 E. Stringforces —— 状压DP

This way 题意: 现在有一个字符串,只包含小写英文的前k位和’?’。定义fif_ifi​表示字符串中仅包含字母i的最长连续子串长度。 字符串的值是fi∣(i∈[1,k])f_i|(i∈[1,k])fi​∣(i∈[1,k])的最小值。问你字符串的值最大是多少。 题解: 那么首先看到最小值最大就大致猜测到是一个二分,而且这显然满足二分的性质。 我们二分出当前的答案之后怎么去检验,一开始我是想着dp[i]表示到了第i个位置的时候,已有的字母集合。但是敲到一半的时候突然反应过来到了相同的点,字母集合可能不

天翼之城的博客 465

从零搭建ROS机械臂仿真环境:Ubuntu 20.04+Noetic下配置睿尔曼RM65的完整流程

本文详细介绍了在Ubuntu 20.04系统中,基于ROS Noetic搭建睿尔曼RM65机械臂的完整仿真环境。从环境准备、工作空间创建到MoveIt!配置与Gazebo仿真环境集成,提供了完整的流程和优化建议,帮助开发者高效实现机械臂仿真与调试。

v6b7n8m9q0的博客 933

CodeForces-580D(状压dp)

【代码】CodeForces-580D(状压dp)

justhereforstudy的博客 295

OpenBmc开发11:添加传感器(I2C)

1 查看传感器信息 通过qemu以本地端口映射方式运行编译后的镜像,访问bmcweb页面的网址是:https://localhost:2443/,如果想用IP地址启动请参考:openbmc开发4:启动运行编译镜像。(这里以网桥方式启动,以ip地址方式访问BMC) 输入用户名和密码,密码是:0penBmc(第一个是数字)。 选择Health,然后点击Sensor,可以看到传感器信息。 2 查看传感器配置文件 进入meta-live/meta-test/recipes-phosphor/...

master 1万+

UBUNTU使用RTL8811CU网卡(包含树莓派)

下载驱动 从绿联中下载:链接 下载解压后,chmod Linux文件夹 sudo chmod -R 777 ./Linux/ 修改系统的BOOT Secure为disable,参考链接:链接 sudo apt install mokutil sudo mokutil --disable-validation 重启电脑后,按照提示选择change secure boot state,并输入对应的字母(并不是全部密码,而是第几个),选择disable即可 进入Linux文件夹,运行命令 ..

Hot_Ant的博客 6246

codeforces1185G1 2100分状压 + dp

题目传送门 题意: 现在有首歌可以选择,每首歌时长是,种类是。 每首歌最多被选择一次。 你现在想选择两两不同的若干首歌,使这些歌的时长和是。 你选择的歌是有顺序的,要求相邻的歌种类不同。 数据范围:。 题解: 我们状压选歌的集合。 一个集合合法的前提是歌曲播放时长的和是T。 然后看能形成多少种排列。 表示的人有个,的人有个,的人有个,...

Irving0323的博客 360

MIKE水动力笔记7_实测数据与模型输出结果的拟合对比

上一博文【MIKE水动力笔记6】主要讲解如何自己制作实测数据的时间序列文件dfs0,而本博文讲解如何进行实测数据与模型输出结果的拟合对比。

晏长街的博客 1万+

Codeforces53 E. Dead Ends(状压dp)

【代码】Codeforces53 E. Dead Ends(状压dp)

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 177

第11.4节 混合储能系统能量管理

混合储能系统(HESS)的能量管理策略采用分层架构实现多时间尺度优化:调度层(小时级)进行经济优化,协调层(秒级)基于频率分解分配功率,执行层(毫秒级)实现快速跟踪。深度强化学习(DRL)方法通过定义状态空间、动作空间和多目标奖励函数,能有效处理系统不确定性和多目标优化问题。HESS性能评估需综合技术指标(如调节性能Kp值、设备寿命指标)和经济指标(净现值NPV、平准化储能成本LCOS),并遵循"建模-仿真-实验"的闭环流程。

技术工作者 244

QML从入门到实战 - 完整学习路线

想入门 QML 开发?来 “从零开始学 QML” 专栏,海量源码 + 实战案例 + 大型项目拆解,手把手教你搞定跨平台 UI 设计,小白也能轻松掌握!

BenBierBa的博客 3478

Codeforces Beta Round 11 D. A Simple Task 【状压DP + 环计数】

个人练习记录

m0_73500785的博客 1024

Jetson Nano之CSI摄像头操作方法

jetson端csi摄像头的应用方法

ZONGXP的博客 1万+

Codeforces Round #531 F. Elongated Matrix (状压dp)

传送门 题意:给定了一个n*m的矩阵,(1<=n<=16,1<=m<=10000) 。我们可以任意交换行的顺序,交换完成后。我们定义k为:从第一列开始向下遍历元素,完成后从第二列开始向下遍历… 得到a11 a21…an1 a12 a22…an2…anm . k为相邻元素差值绝对值的最小值。 求k最大可以是多少? 分析:我们看到n的范围,大概可以猜到是状压dp 。 我们具体分析遍历方式可以发现,相邻元素的差值最小值其实就是i j行元素对应位置相差的最小值,还有首末行是错开一个位置

矮纸斜行闲作草 250

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

codeforces 111C(状压DP)

思路:数据范围很小,考虑用状压来做...这类网格放东西问题其实dp大概都可做。   把问题转化为蜘蛛移动后,每个点上下左右和本身至少有一个网格有蜘蛛。   首先取n,m的较小值作为列数,较大值作为行数,对每行进行状压(即用一个二进制数表示),0表示没有蜘蛛,1表示有蜘蛛。   用dp[i][j][k]表示当前处理第i行,且第i行状态为j,第i+1行状态为k时,前i行的空网格数。 ...

dingdi3021的博客 306

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

CodeForces - 1550E Stringforces(二分+状压dp)

题目链接:点击查看 题目大意:给出一个长度为 nnn 的字符串,只包含前 kkk 个小写字母以及通配符 ???,现在可以将通配符替换成任意的前 kkk 个字母中的一个。设 f[i]f[i]f[i] 为第 iii 个字母在字符串中,最长的连续子段的长度,现在要求所有 f[i]f[i]f[i] 中最小值的最大值 题目分析:读完题一下子根据复杂度把正解分析出来了,可是怎么也没办法往 dpdpdp 上去靠。因为 k≤17k\le 17k≤17,又因为 217≈2e52^{17}≈2e5217≈2e5,所以不难想到二

Falcon的博客 842

Codeforces 1242C Sum Balance(状压DP)

题目链接 题意 给你 nnn 行每行若干整数(所有整数互不相同),每行拿出任意一个整数,再给每行放回一个拿出的整数,求这样操作一次后所有行的整数和相同的操作方案 思路 第一次打DIV1,没想到代码能力太弱想到思路却没敲出来,题还是刷太少。 先验证所有整数和除以n是否存在余数判断 之后对每个点求其所在的环,每个环长度不超过n,所以这里复杂度是 O(n∗5000∗n)O(n*5000*n)O(n∗50...

←_← 377

CodeForces 544E - Remembering Strings(状压DP)

题目链接 https://cn.vjudge.net/problem/CodeForces-544E 【题意】 给定n个长度均为m的字符串,再给出一个n行m列的矩阵,矩阵和字符串对应的位置代表把该位置的字符改成其它字符所需要的代价,现在要求对于任意一个字符串,总存在某一列使得该字符串在该列的字符在整个列中是唯一的,求把所有字符串修改成满足上述要求的字符串的最小代价是多少?(n,m&lt;=20...

Change the world by program. 516

Codeforces1391 D. 505(状压DP)

A binary matrix is called good if every even length square sub-matrix has an odd number of ones. Given a binary matrix ???? consisting of ???? rows and ???? columns, determine the minimum number of cells you need to change to make it good, or report that t

tomjobs的博客 664

CodeForces743E. Vladik and cards 二分+状压dp

这个题我们可以想象成_---___-----__的一个水柱它具有一遍优一遍行的性质因此可以用来二分最小值len,而每次二分后我们都要验根,we可以把这个水柱想成我们在每个数段里取前一段的那个数后一段有也不选,而且最后一个区间的第一个数一定可以使这个数区间对应的数,那么我们只要在某个位置上不选或选就可以啦,这we很容易想到2n的验根但是这样还不如打暴力,这时我们发现这个dfs是低效的他会很多进入等效...

weixin_30640291的博客 118

【CodeForces - 1051D】Bicolorings (dp,类似状压dp)

题干: You are given a grid, consisting of 22 rows and nn columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and s...

xuanweiace的博客 489

别再只把FPGA当‘万能芯片’了:从LUT到硬核,聊聊它真正适合的5个应用场景

本文深入探讨FPGA技术的核心架构与适用场景,揭示其作为可编程芯片的真正潜力。从LUT结构到硬核系统,分析FPGA在实时流处理、原型验证、异构计算和边缘智能等5个关键领域的优势与局限,帮助工程师做出精准的技术选型决策。

weixin_30463341的博客 362

【CodeForces】【状压DP】1155F-Delivery Oligopoly

CodeForces 1155F Delivery Oligopoly 题目大意 给定一个已经是边双联通分量的图,要求删掉最多的边,使得最终得到的图是也是一个边双联通分量。输出保留的边。 分析 考虑我们如何构造出一个边双联通分量。 我们发现最终的答案一定是一个环带上一个环的样子(有点像糖葫芦)。 于是考虑强制以111为起点,每次向上面加上一条链。 考虑如何按照这样的方式来计算答案。 首先预处理一个...

IDDFS 396

codeforces401D Roman and Numbers 状压DP

题目链接:戳这里 题目大意:输入一个数,将该数的每个数位重新排列后(无前导零),有多少个数%m=0? 分析:因为最多有18位数,可以考虑用18位的2进制数表示每一位上的数是否使用,状压dp转移即可。 定义dp[state][k]表示当前状态为state时,%m余数为j的方案数。 转移方程为dp[state][(k*10+dight[j])%m]=dp[state^(1 注意重复问题,如果

Twin Automata的博客 407

codeforces 417D 状压DP

题意:多读英文题面有助于提高英语~~,要是你真忍不住就看代码下面吧。。一开始觉得n*2^k是不是会GG啊,后来:cf怎么可能连1e8都不能过1e9都能过不是开玩笑的。 然后开始推,明显的设f[i][j]表示前i个状态为j,有: f[i][j|a[i].s]=min(f[i][j]+a[i].cost)f[i][j|a[i].s]=min(f[i][j]+a[i].cost) 有个问题就是他要求

老臣 625

Codeforces 743E(二分+状压dp)

Codeforces 743E Codeforces Round #384 (Div. 2) E 题解

ME495的博客 1397

深挖C++:类和对象(中)

(先进后出)

xys_678的博客 222
上一篇: 洛谷 P1494 [国家集训队] 小 Z 的袜子 题解 莫队
下一篇: 洛谷 P3834 可持久化线段树 2 题解
bbc_ouo
博客等级 码龄3年 2096粉丝 132原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值