Problem Statement

There is a $N \times N$ grid, with blocks on some squares.

The grid is described by $N$ strings $S_1,S_2,\dots,S_N$, as follows.

  • If the $j$-th character of $S_i$ is #, there is a block on the square at the $i$-th row from the top and $j$-th column from the left.
  • If the $j$-th character of $S_i$ is ., there is not a block on the square at the $i$-th row from the top and $j$-th column from the left.

Takahashi can do the operation below zero or more times.

  • First, choose an integer $D$ between $1$ and $N$ (inclusive), and a $D \times D$ subsquare within the grid.
  • Then, consume $D$ stamina points to destroy all blocks within the subsquare.

Find the minimum number of stamina points needed to destroy all the blocks.

Constraints

  • $N$ is an integer.
  • $1 \le N \le 50$
  • $S_i$ consists of # and ..
  • $|S_i|=N$

Input

Input is given from Standard Input in the following format:

$N$
$S_1$
$S_2$
$\vdots$
$S_N$

Output

Print the answer as an integer.


Sample Input 1

5
##...
.##..
#.#..
.....
....#

Sample Output 1

4

By choosing the subsquares below, Takahashi will consume $4$ stamina points, which is optimal.

  • The $3 \times 3$ subsquare whose top-left square is at the $1$-st row from the top and $1$-st column from the left.
  • The $1 \times 1$ subsquare whose top-left square is at the $5$-th row from the top and $5$-th column from the left.

Sample Input 2

3
...
...
...

Sample Output 2

0

There may be no block on the grid.


Sample Input 3

21
.....................
.....................
...#.#...............
....#.............#..
...#.#...........#.#.
..................#..
.....................
.....................
.....................
..........#.....#....
......#..###.........
........#####..#.....
.......#######.......
.....#..#####........
.......#######.......
......#########......
.......#######..#....
......#########......
..#..###########.....
.........###.........
.........###.........

Sample Output 3

19

50的数据范围,基本上高次dp或者折半搜索的。这题不太像折半搜索,那就试一下高维dp。

定义 \(dp_{x1,y1,x2,y2}\) 为解决以 \((x1,y1)\) 为左上角,以 \((x2,y2)\) 为右下角矩形 所需的最小代价.

首先肯定有一种 \(\max(y2-y1+1,x2-x1+1)\) 的方案,那就是把他填满。

有一个引理,在我们填的矩阵中,一定不可能接壤,不然的话就可以选一个更大的矩阵,在代价一样的情况下框住的面积更大了。

所以如果不是全选的话,一定可以沿着某一行或者某一列把大矩阵割成两个矩阵解决。枚举对应的这一行或一列,递归下去就行了

这个dp的顺序应该按照区间dp的顺序。

#include<bits/stdc++.h>
using namespace std;
const int N=55;
int n,dp[N][N][N][N],c1[N][N],c2[N][N],r1,r2;
char s[N][N];
void tomax(int&a,int b)
{
a=min(a,b);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%s",s[i]+1);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
c1[i][j]=c1[i][j-1]+(s[i][j]=='#');
c2[i][j]=c2[i-1][j]+(s[i][j]=='#');
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int l1=1;l1+i-1<=n;l1++)
{
for(int l2=1;l2+j-1<=n;l2++)
{
r1=l1+i-1,r2=l2+j-1;
dp[l1][l2][r1][r2]=max(i,j);
for(int k=l1;k<=r1;k++)
if(c1[k][r2]==c1[k][l2-1])
tomax(dp[l1][l2][r1][r2],dp[l1][l2][k-1][r2]+dp[k+1][l2][r1][r2]);
for(int k=l2;k<=r2;k++)
if(c2[l1-1][k]==c2[r1][k])
tomax(dp[l1][l2][r1][r2],dp[l1][l2][r1][k-1]+dp[l1][k+1][r1][r2]);
// printf("%d %d %d %d %d\n",l1,l2,r1,r2,dp[l1][l2][r1][r2]);
}
}
}
}
printf("%d",dp[1][1][n][n]);
}

[ABC233G] Strongest Takahashi的更多相关文章

  1. 酷摄影:关于梦 - Miki takahashi

    这组摄影来自于日本东京摄影师 Miki takahashi 是一组双重曝光摄影,分开看也许很平常,但是结合在一起却非常有韵味. [gallery]

  2. 数据结构与算法实验题 4.2 Who is the strongest

    数据结构与算法实验题 4.2 Who is the strongest ★实验任务 在神奇的魔法世界,召唤师召唤了一群的魁偶.这些魁偶排成一排,每个魁偶都有一个 战斗值.现在该召唤师有一个技能,该技能 ...

  3. AtCoder Beginner Contest 088 C Takahashi's Information

    Problem Statement We have a 3×3 grid. A number ci,j is written in the square (i,j), where (i,j) deno ...

  4. AtCoder ABC 129F Takahashi's Basics in Education and Learning

    题目链接:https://atcoder.jp/contests/abc129/tasks/abc129_f 题目大意 给定一个长度为 L ,首项为 A,公差为 B 的等差数列 S,将这 L 个数拼起 ...

  5. CF1574D The Strongest Build

    考虑到有\(m\)个限制,可以考虑\(m+1\)个最大的答案,这样可以利用鸽笼原理得到答案. 这是一般做法. 但是这样的题目,我们发现\(n\)个操作都是独立的. 那么我们可以全部都选取最大,并以此调 ...

  6. 【LeetCode】1471. 数组中的 k 个最强值 The k Strongest Values in an Array (Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 自定义排序 日期 题目地址:https://leetc ...

  7. kafka源码分析之一server启动分析

    0. 关键概念 关键概念 Concepts Function Topic 用于划分Message的逻辑概念,一个Topic可以分布在多个Broker上. Partition 是Kafka中横向扩展和一 ...

  8. EasyPR--开发详解(8)文字定位

    今天我们来介绍车牌定位中的一种新方法--文字定位方法(MSER),包括其主要设计思想与实现.接着我们会介绍一下EasyPR v1.5-beta版本中带来的几项改动. 一. 文字定位法 在EasyPR前 ...

  9. Elixir - Hey, two great tastes that go great together!

    这是Elixir的作者 José Valim 参与的一次技术访谈,很有料,我们可以了解Elixir的一些设计初衷,目标等等. 原文在: http://rubyrogues.com/114-rr-eli ...

  10. JMM(java内存模型)

    What is a memory model, anyway? In multiprocessorsystems, processors generally have one or more laye ...

随机推荐

  1. 文心一言 VS 讯飞星火 VS chatgpt (89)-- 算法导论8.3 2题

    二.用go语言,下面的排序算法中哪些是稳定的:插入排序.归并排序.堆排序和快速排序?给出一个能使任何排序算法都稳定的方法.你所给出的方法带来的额外时间和空间开销是多少? 文心一言: 在Go语言中,以下 ...

  2. 浅谈Code Review

    1. 什么是Code Review Code Review(CR)即代码评审,又名代码走查,是指对软件开发过程中编写的代码进行检查和评估的一种实践.它通常由其他团队成员.同事或专门的质量保证团队成员来 ...

  3. 记一次 .NET 某餐饮小程序 内存暴涨分析

    一:背景 1. 讲故事 前些天有位朋友找到我,说他的程序内存异常高,用 vs诊断工具 加载时间又太久,让我帮忙看一下到底咋回事,截图如下: 确实,如果dump文件超过 10G 之后,市面上那些可视化工 ...

  4. Quantitative Relationship Induction

    数量关系是指事物之间的数值或数量之间的相互关系(+.-.*./). 数量关系描述各种量的变化和相互关系.数量关系可以包括数值的比较.增减.比例.百分比.平均值等方面. 在数学中,数量关系可以通过代数方 ...

  5. AcWing - 闫氏DP分析法

    核心思想:从集合角度来分析DP问题 在我们遇到的DP问题中,一般都是求在一个有限集内的最值,但是这些方案数量一般都是指数级别的,想要一个一个查找出来不太可能.所以DP方法是用来优化这种寻找最优方案的过 ...

  6. 分布式与微服务——Iaas,Paas和Saas、单体应用和缺点、微服务概念、传统 分布式 SOA 架构与微服务架构的区别、微服务实战、什么是RPC、CAP定理和BASE理论、唯一ID生成、实现分布式

    文章目录 1-什么是Iaas,Paas和Saas 一 IaaS基础设施服务 二 paas平台即服务 三saas软件即服务 四 总结 2-单体应用和缺点 一 单体应用 二 单体应用的缺陷 3-微服务概念 ...

  7. 如何查询4GL程序中创建的临时表中的数据

    前提:将dba_segments这个表的select权限授权给各个营运中心(即数据库用户) ①.用sys账号以dba的权限登录数据库 <topprod:/u1/topprod/tiptop> ...

  8. android的listview控件,加了行内按钮事件导致行点击失效的问题

    近日,修改一个app,原来的listview中只有行点击事件 ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() ...

  9. Iksevi 题解

    Iksevi 题目大意 \(n\) 次询问,每次给定一个点 \((x,y),x\ge 0, y\ge 0\),问有多少种对角线长为偶数的正方形使得在用该正方形正密铺第一象限的情况下该点位于正方形顶点上 ...

  10. 【Vue3响应式入门#01】Reactivity

    专栏分享:vue2源码专栏,vue3源码专栏,vue router源码专栏,玩具项目专栏,硬核推荐 欢迎各位ITer关注点赞收藏 背景 以下是柏成根据Vue3官方课程整理的响应式书面文档 - 第一节, ...