Description
Input
Output
Sample Input
5 4 PHPP PPHH PPPP PHPP PHHP
Sample Output
6
与HDU4539基本上一样的题目,只是在判断情况上有些不同而已
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int n,m; char map[105][15]; int mat[105][15]; int now[1<<10],pre[1<<10],prepre[1<<10]; int l_now,l_pre,l_prepre; int dp[1<<10][1<<10],tem[1<<10][1<<10],tt[1<<10]; void dfs(int id,int k,int p,int sum) { if(k>=m) { now[++l_now] = p; tt[l_now] = sum; return; } if(k>=2 && mat[id][k] && !(p&(1<<(k-2))) && !(p&(1<<(k-1)))) dfs(id,k+1,p|(1<<k),sum+1); else if(k==1 && mat[id][k] && !(p&(1<<(k-1)))) dfs(id,k+1,p|(1<<k),sum+1); else if(k == 0 && mat[id][k]) dfs(id,k+1,p|(1<<k),sum+1); dfs(id,k+1,p,sum); } void solve() { int i,j,k,t; tem[1][1] = pre[1] = prepre[1] = 0; l_pre = l_prepre = 1; for(k = 0; k<n; k++) { l_now = 0; dfs(k,0,0,0); for(i = 1; i<=l_now; i++) for(j = 1; j<=l_pre; j++) dp[i][j] = 0; for(i = 1; i<=l_now; i++) for(j = 1; j<=l_pre; j++) for(t = 1; t<=l_prepre; t++) { if(now[i] & prepre[t]) continue; if(now[i] & pre[j]) continue; if(pre[j] & prepre[t]) continue; dp[i][j] = max(dp[i][j],tem[j][t]+tt[i]); } for(i = 1; i<=l_now; i++) for(j = 1; j<=l_pre; j++) tem[i][j] = dp[i][j]; for(i = 1; i<=l_pre; i++) prepre[i] = pre[i]; for(i = 1; i<=l_now; i++) pre[i] = now[i]; l_prepre = l_pre; l_pre = l_now; } } int main() { int i,j,ans; while(~scanf("%d%d",&n,&m)) { for(i = 0; i<n; i++) { scanf("%s",map[i]); for(j = 0; j<m; j++) { if(map[i][j] == 'P') mat[i][j] = 1; else mat[i][j] = 0; } } solve(); ans = 0; for(i = 1; i<=l_pre; i++) for(j = 1; j<=l_prepre; j++) ans = max(ans,tem[i][j]); printf("%d\n",ans); } return 0; }