栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3772 Baseball of Planet P...

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3772 Baseball of Planet P...

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int dp[(1<<16)+2][10][4];int map[17][17];int bit[(1<<16)+2];int main(){    int i,j,n,cnt,k,l,m,p,a,ans;    for (i=0;i<(1<<16);i++)    {        cnt=0;        for (j=0;j<16;j++)        { if ((i & (1<<j))!=0) cnt++;        }        bit[i]=cnt;    }    while(1)    {        scanf("%d",&n);        if (n==0) break;        for (i=0;i<n;i++)        { for (j=0;j<n;j++) {     scanf("%d",&map[i][j]); }        }        memset(dp,-1,sizeof(dp));        ans=0;        dp[0][0][0]=0;        for (i=0;i<(1<<n);i++)        { for (j=0;j<(1<<3);j++) {     for (k=0;k<3;k++)     {         if (dp[i][j][k]==-1) continue;         ans=max(ans,dp[i][j][k]);         if (k==3) continue;         for (l=0;l<n;l++)         {  if ((i & (1<<l))!=0) continue;  p=(i | (1<<l));  cnt=bit[p]-1;  if (map[l][cnt]==0)  {      dp[p][j][k+1]=max(dp[p][j][k+1],dp[i][j][k]);  }  if (map[l][cnt]==1)  {      if (k==2) continue;      if ((j & 4)!=0) dp[p][(j<<1) & 7][k+1]=max(dp[p][(j<<1) & 7][k+1],dp[i][j][k]+1);      else dp[p][(j<<1) & 7][k+1]=max(dp[p][(j<<1) & 7][k+1],dp[i][j][k]);  }  if (map[l][cnt]==2)  {      if ((j & 4)!=0) dp[p][((j<<1) & 7)+1][k]=max(dp[p][((j<<1) & 7)+1][k],dp[i][j][k]+1);      else dp[p][((j<<1) & 7)+1][k]=max(dp[p][((j<<1) & 7)+1][k],dp[i][j][k]);  }  if (map[l][cnt]==3)  {      dp[p][0][k]=max(dp[p][0][k],dp[i][j][k]+((j & 1)!=0)+((j & 2)!=0)+((j & 4)!=0)+1);  }         }     } }        }        printf("%dn",ans);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373913.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号