FstAutoMaton @ 2022-11-09 20:22:00
萌新初学DP,求dalao帮助 代码如下
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m, dp[1 << 10][1 << 10][3], a[105], Sum[1 << 10];
string s;
int change()
{
int sum = 0;
for( int i = 0; i < s.size(); i ++ )
{
sum = sum * 10;
if( s[i] == 'H' ) sum ++;
}
return sum;
}
int count( int x )
{
int sum = 0;
while( x > 0 )
{
if( x & 1 ) sum ++;
x >> 1;
}
return sum;
}
bool check_To( int x, int y )
{
if( x & y ) return 1;
return 0;
}
bool check_Now( int x )
{
if( x & ( x >> 1 ) || x & ( x >> 2 ) ) return 0;
if( x & ( x << 1 ) || x & ( x << 2 ) ) return 0;
return 1;
}
signed main()
{
cin >> n >> m;
int N = ( 1 << m ) - 1;
for( int i = 0; i <= N; i ++ )
{
Sum[i] = count( i );
}
for( int i = 1; i <= n; i ++ )
{
cin >> s;
a[i] = change();
}
for( int i = 0; i <= N; i ++ )
{
dp[0][i][0] = Sum[i];
}
for( int i = 0; i <= N; i ++ )
{
for( int j = 0; j <= N; j ++ )
{
if( check_Now( i ) || check_Now( j ) || check_To( i, j ) ) continue;
dp[i][j][1] = Sum[i] + Sum[j];
}
}
for( int i = 3; i <= n; i ++ )
{
for( int j = 0; j <= N; j ++ )//当前行
{
if( check_Now( j ) ) continue;
for( int k = 0; k <= N; k ++ )//上一行
{
if( check_Now( k ) ) continue;
if( check_To( j, k ) ) continue;
for( int l = 0; l <= N; l ++ )
{
if( check_Now( l ) ) continue;
if( check_To( j, l ) ) continue;
if( check_To( k, l ) ) continue;
dp[k][j][i % 3] = max( dp[k][j][i % 3], dp[l][k][( i - 1 ) % 3] + Sum[j] );
}
}
}
}
int ans = 0;
for( int i = 0; i <= N; i ++ )
for( int j = 0; j <= N; j ++ )
ans = max( ans, dp[i][j][(n - 1) % 3] );
cout << ans;
}
by FstAutoMaton @ 2022-12-14 18:56:22
考自己的古,AC于2022-11-10