程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ - 1185 跑兵陣地 狀態壓縮DP

POJ - 1185 跑兵陣地 狀態壓縮DP

編輯:C++入門知識

    狀態壓縮DP...很簡單了...一行最多10個...用1代表放炮兵..0代表不放...每行的狀態最多1024種..並且還要合法..這樣一來..一行的狀態最多60種了...

    能影響到當前行的只有上兩行..所以用三維的DP...dp[t][x1][x2]....t代表哪一層了...x1代表上一層的狀態..x2代表當前層的狀態...

    dp[t][x1][x2]= max ( dp[t-1][y][x1] + cnt[x2]) ...y是t-2層的狀態...cnt是當前層狀態的炮兵數,也就是1的數量...這裡看出約束條件..就是狀態x1,x2,y不能沖突...

 


Program:


[cpp]
#include<iostream>  
#include<cmath>  
#include<stack>  
#include<queue>  
#include<set>  
#include<algorithm>  
#include<stdio.h>  
#include<string.h>  
#define ll long long  
#define oo 1000000007  
using namespace std;    
int n,m,a[105],anum; 
char arc[105][12]; 
int cnt[65],dp[105][65][65]; 
bool ok1(int x)   // 判斷當前狀態是否合法  

       int p,i; 
       p=-10;  
       while (x) 
       { 
             i++; 
             if (x%2) 
             { 
                   if (i-p<=2) return false; 
                   p=i; 
             } 
             x/=2; 
       }        
       return true; 

bool ok2(int t,int x)  //判斷當前狀態能否放在當前的圖上  

       int i;  
       for (i=m;i>=1 && x;i--) 
       { 
             if (x%2 && arc[t][i]=='H') return false; 
             x/=2; 
       } 
       return true; 

bool ok3(int y,int x) //判斷狀態y在上,狀態x在下,能否不沖突  

       while (y && x) 
       { 
             if (y%2 && x%2) return false; 
             y/=2;  x/=2; 
       } 
       return true; 

int main() 
{        
       int i,t,j,x,ans; 
       while (~scanf("%d%d",&n,&m)) 
       {  
               anum=0; 
               a[0]=0; 
               for (i=0;i<(1<<m);i++) 
                  if (ok1(i))  
                  { 
                          a[++anum]=i; 
                          cnt[anum]=0; 
                          j=i; 
                          while (j) 
                          { 
                                 j=j&(j-1); 
                                 cnt[anum]++; 
                          } 
                  } 
               for (i=0;i<=n;i++) gets(arc[i]+1); 
               memset(dp,0,sizeof(dp)); 
               for (i=1;i<=anum;i++) 
                 if (ok2(1,a[i]))  dp[1][0][i]=cnt[i];   
               for (t=2;t<=n;t++) 
                  for (i=0;i<=anum;i++) 
                    if (ok2(t,a[i])) 
                      for (j=0;j<=anum;j++) 
                        if (ok2(t-1,a[j]) && ok3(a[j],a[i])) 
                          for (x=0;x<=anum;x++) 
                            if (dp[t][j][i]<dp[t-1][x][j]+cnt[i] && ok3(a[x],a[i])) 
                                dp[t][j][i]=dp[t-1][x][j]+cnt[i]; 
                          
               ans=0; 
               for (i=0;i<=anum;i++) 
                 for (j=0;j<=anum;j++) 
                   if (ans<dp[n][i][j])  
                       ans=dp[n][i][j]; 
               printf("%d\n",ans); 
       }  
       return 0; 

#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#define ll long long
#define oo 1000000007
using namespace std;  
int n,m,a[105],anum;
char arc[105][12];
int cnt[65],dp[105][65][65];
bool ok1(int x)   // 判斷當前狀態是否合法
{
       int p,i;
       p=-10;
       while (x)
       {
             i++;
             if (x%2)
             {
                   if (i-p<=2) return false;
                   p=i;
             }
             x/=2;
       }      
       return true;
}
bool ok2(int t,int x)  //判斷當前狀態能否放在當前的圖上
{
       int i;
       for (i=m;i>=1 && x;i--)
       {
             if (x%2 && arc[t][i]=='H') return false;
             x/=2;
       }
       return true;
}
bool ok3(int y,int x) //判斷狀態y在上,狀態x在下,能否不沖突
{
       while (y && x)
       {
             if (y%2 && x%2) return false;
             y/=2;  x/=2;
       }
       return true;
}
int main()
{      
       int i,t,j,x,ans;
       while (~scanf("%d%d",&n,&m))
       {
               anum=0;
               a[0]=0;
               for (i=0;i<(1<<m);i++)
                  if (ok1(i))
                  {
                          a[++anum]=i;
                          cnt[anum]=0;
                          j=i;
                          while (j)
                          {
                                 j=j&(j-1);
                                 cnt[anum]++;
                          }
                  }
               for (i=0;i<=n;i++) gets(arc[i]+1);
               memset(dp,0,sizeof(dp));
               for (i=1;i<=anum;i++)
                 if (ok2(1,a[i]))  dp[1][0][i]=cnt[i]; 
               for (t=2;t<=n;t++)
                  for (i=0;i<=anum;i++)
                    if (ok2(t,a[i]))
                      for (j=0;j<=anum;j++)
                        if (ok2(t-1,a[j]) && ok3(a[j],a[i]))
                          for (x=0;x<=anum;x++)
                            if (dp[t][j][i]<dp[t-1][x][j]+cnt[i] && ok3(a[x],a[i]))
                                dp[t][j][i]=dp[t-1][x][j]+cnt[i];
                        
               ans=0;
               for (i=0;i<=anum;i++)
                 for (j=0;j<=anum;j++)
                   if (ans<dp[n][i][j])
                       ans=dp[n][i][j];
               printf("%d\n",ans);
       }
       return 0;
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved