首页 > 其他分享 >[ABC233G] Strongest Takahashi

[ABC233G] Strongest Takahashi

时间:2023-01-03 21:03:00浏览次数:38  
标签:int top Sample Strongest ABC233G th left dp Takahashi

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]);
}

标签:int,top,Sample,Strongest,ABC233G,th,left,dp,Takahashi
From: https://www.cnblogs.com/mekoszc/p/17023361.html

相关文章

  • D - Takahashi's Solitaire -- ATCODER
    D-Takahashi'sSolitairehttps://atcoder.jp/contests/abc277/tasks/abc277_d 思路先计算所有的输入的和total,将输入列表首先进行排列找到所有连续段和中最大的......
  • C - Ladder Takahashi -- ATCODER
    C-LadderTakahashihttps://atcoder.jp/contests/abc277/tasks/abc277_c 思路把梯子可达楼层看成图的节点把梯子看成节点之间的连线所以整个问题变成图的遍历问题......
  • AtCoder Beginner Contest 277 D Takahashi's Solitaire
    Takahashi'sSolitaire双指针&&尺取先排个序,然后把数组扩展到\(2\timesn\),为了处理循环的情况然后双指针跑一下,限定\(r\)扩展的条件为:当前数字等于前一个或者......