Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21381 | Accepted: 8290 |
Description
Input
Output
Sample Input
5 4
PHPP
PPHH
PPPP
PHPP
PHHP
Sample Output
6
Source
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int const MAX = 105; int sta[MAX], stnum[MAX], f[MAX]; int dp[MAX][MAX][MAX]; char s[MAX][15]; int main() { int n, m, cnt = 0; scanf("%d %d", &n, &m); for(int i = 0; i < n; i++) scanf("%s", s[i]); memset(f, 0, sizeof(f)); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(s[i][j] == 'H') f[i] += (1 << j); for(int j = 0; j < (1 << m); j++) { int tmp = j; if((j & (j << 1)) || (j & (j << 2))) continue; while(tmp) { stnum[cnt] += (tmp & 1); tmp >>= 1; } sta[cnt ++] = j; } for(int i = 0; i < cnt; i++) { if(f[0] & sta[i]) continue; dp[0][i][0] = stnum[i]; } for(int i = 0; i < cnt; i++) { if(f[1] & sta[i]) continue; for(int j = 0; j < cnt; j++) { if(f[0] & sta[j]) continue; if(sta[i] & sta[j]) continue; dp[1][i][j] = max(dp[1][i][j], dp[0][j][0] + stnum[i]); } } for(int r = 2; r < n; r++) { for(int i = 0; i < cnt; i++) { if(f[r] & sta[i]) continue; for(int j = 0; j < cnt; j++) { if(f[r - 1] & sta[j]) continue; if(sta[i] & sta[j]) continue; for(int k = 0; k < cnt; k++) { if(f[r - 2] & sta[k]) continue; if((sta[i] & sta[k]) || (sta[j] & sta[k])) continue; dp[r][i][j] = max(dp[r][i][j], dp[r - 1][j][k] + stnum[i]); } } } } int ans = 0; for(int i = 0; i < cnt; i++) for(int j = 0; j < cnt; j++) ans = max(ans, dp[n - 1][i][j]); if(n == 0 || m == 0) printf("0\n"); else printf("%d\n", ans); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/tc_to_top/article/details/47003491