Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17163 | Accepted: 6558 |
Description
Input
Output
Sample Input
5 4 PHPP PPHH PPPP PHPP PHHP
Sample Output
6
Source
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const long N = 110, MAX = 70; long n, m, t; long a[N]; long state[MAX]; long num[MAX]; long long f[N][MAX][MAX]; inline long max (long a, long b) { return a > b ? a : b; } long cala(long x) { long sum = 0; while (x > 0) { if ((x & 1) == 1) sum++; x >>= 1; } return sum; } int main() { freopen("poj1185.in", "r", stdin); scanf("%d%d\n", &n, &m); for (long i = 1; i <= n; i++) { char c; a[i] = 0; for (long j = 1; j <= m; j++) { scanf("%c", &c); if (c == 'H') a[i] += (1 << (j - 1)); } scanf("\n"); } long r = (1 << m) - 1; t = 0; for (long i = 0; i <= r; i++) { if (((i & (i >> 1)) == 0) && ((i & (i >> 2)) == 0)) { state[++t] = i; num[t] = cala(i); } } //cout << t<< endl; memset(f, 0 , sizeof(f)); for (long i = 1;i <= t; i++) { if ((state[i] & a[1]) == 0) f[1][i][0] = num[i]; // cout<< state[i]<< ' ' << f[1][i][0]<< endl; } for (long i = 1; i <= t; i++) for (long j = 1; j <= t; j++) { if ((state[j] & a[2]) != 0) continue; if ((state[i] & state[j]) != 0) continue; f[2][j][i] = max(f[2][j][i], num[j] + f[1][i][0]); } for (long i = 3; i <= n; i++) { for (long j = 1; j <= t; j++) for (long p = 1; p <= t; p++) for (long q = 1; q <= t; q++) { if ((state[j] & state[p]) != 0) continue; if ((state[p] & state[q]) != 0) continue; if ((state[j] & state[q]) != 0) continue; if ((state[j] & a[i]) != 0) continue; f[i][j][p] = max (f[i][j][p], f[i - 1][p][q] + num[j]); } } if (n == 1) { long re = 0; for (long i = 1; i <= t; i++) { re = max(re, f[1][i][0]); } cout << re <<endl; return 0; } long re = 0; for (long i = 1; i <= t; i++) for (long j = 1; j <= t; j++) { re = max(re, f[n][i][j]); } cout << re <<endl; return 0; }