【POJ 1185】炮兵阵地(状压DP)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 23107 | Accepted: 8957 |
Description
Input
Output
Sample Input
5 4 PHPP PPHH PPPP PHPP PHHP
Sample Output
6
Source
#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm> #include <map> #include <set> #define LL long long #define Pr pair<int,int> #define fread() freopen("in.in","r",stdin) #define fwrite() freopen("out.out","w",stdout) using namespace std; const int INF = 0x3f3f3f3f; const int msz = 10000; const int mod = 1e9+7; const double eps = 1e-8; char stmp[11]; int mp[233]; int pre[66][66]; int now[66][66]; int mode[66]; int add[66]; int tp = 0; int n,m; bool can(int x) { int cnt = 2; int ans = 0; while(x) { if(x&1) { ans++; if(cnt < 2) return false; cnt = 0; }else cnt++; x >>= 1; } add[tp] = ans; return true; } int main() { //fread(); //fwrite(); int tot = 1<<10; for(int i = 0; i < tot; ++i) if(can(i)) mode[tp++] = i; while(~scanf("%d%d",&n,&m)) { for(int i = 0; i < n; ++i) { scanf("%s",stmp); int tmp = 0; for(int j = 0; j < m; ++j) tmp = (tmp<<1)+(stmp[j] == 'H'); mp[i] = tmp; } tot = 1<<m; memset(now,0,sizeof(now)); for(int i = 0; i < tp && mode[i] < tot; ++i) if(mode[i]&mp[0]) continue; else now[0][i] = add[i]; for(int i = 1; i < n; ++i) { memcpy(pre,now,sizeof(now)); memset(now,0,sizeof(now)); for(int j = 0; j < tp && mode[j] < tot; ++j) { if(i >= 2 && (mode[j]&mp[i-2])) continue; for(int k = 0; k < tp && mode[k] < tot; ++k) { if(mode[k]&(mp[i-1]|mode[j]))continue; for(int l = 0; l < tp && mode[l] < tot; ++l) if(mode[l]&(mp[i]|mode[k]|mode[j])) continue; else { now[k][l] = max(now[k][l],pre[j][k]+add[l]); } } } } int ans = 0; for(int i = 0; i < tp && mode[i] < tot; ++i) for(int j = 0; j < tp && mode[j] < tot; ++j) ans = max(ans,now[i][j]); printf("%d\n",ans); } return 0; }