codefoces 1393D Rarity and New Dress

https://codeforces.com/contest/1393/problem/D

矩阵dp,数有几个菱形,一般好像就是以某个位置为底,然后去dp

具体看代码吧,没啥可说的 ,都是玄学dp

#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn = 2020;
char map[maxn][maxn];
ll dp[maxn][maxn];
int cal(int i,int j){
	char ch = map[i][j];
	if(ch != map[i-1][j-1] || ch != map[i-1][j+1] || ch != map[i-1][j] || ch != map[i-2][j]) return 0;
	return 1;
}
int main() {
	int n,m;
	cin>>n>>m;
	for(int i=0; i<n; i++) {
		scanf("%s",map[i]);
	}

	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			if(i <= 1 || j == 0 || j == m-1 ) {
				dp[i][j] = 1;
			} 
			else {
				if(cal(i,j)){
					dp[i][j] = min(dp[i-1][j-1] ,min(dp[i-1][j+1],dp[i-2][j]))+1;
				}
				else{
					dp[i][j] = 1;
				}
			}
		}
	}
	ll ans = 0;
	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
		//	if(dp[i][j] == 0) dp[i][j] = 1;
			ans += dp[i][j];
		//	cout<<dp[i][j]<<" ";
		}
		//cout<<endl;
	}
	cout<<ans<<endl;
	return 0;
}

  

寻找真正的热爱
原文地址:https://www.cnblogs.com/lesning/p/13695215.html