1. /*
  2. 题意:n行m列的矩阵,1表示可以放东西,0表示不可以。曼哈顿距离为2的两个位置最多只能有一个位置放东西。
  3. 问最多放多少个东西。
  4. */
  5. #include<stdio.h>
  6. #include<string.h>
  7. #include<stdlib.h>
  8. #include<algorithm>
  9. #include<iostream>
  10. #include<queue>
  11. #include<stack>
  12. #include<math.h>
  13. #include<map>
  14. using namespace std;
  15. const int maxn = ;
  16. const int maxm = ;
  17. const int N = ;
  18. int mat[ maxn ];
  19. int dp[ maxn ][ N ][ N ];
  20. int state[ N ];
  21. int ones_state[ N ];
  22. int Count_ones( int x ){
  23. int cnt = ;
  24. while( x ){
  25. if( x& )
  26. cnt++;
  27. x>>=;
  28. }
  29. return cnt;
  30. }
  31. int init( int n,int m ){
  32. memset( state,,sizeof( state ) );
  33. memset( ones_state,,sizeof( state ) );
  34. int M = <<m;
  35. int cnt = ;
  36. for( int i=;i<M;i++ ){
  37. if( (i&(i<<))==&&(i&(i>>))== ){
  38. state[ cnt ] = i;
  39. ones_state[ cnt ] = Count_ones( i );
  40. cnt++;
  41. }
  42. }
  43. //printf("cnt=%d\n",cnt);最多169种状态!!
  44. return cnt;
  45. }
  46. void DP( int cnt,int n,int m ){
  47. memset( dp,-,sizeof( dp ) );
  48. for( int i=;i<cnt;i++ ){
  49. if( (state[i]&mat[])== )
  50. dp[][i][] = ones_state[ i ];
  51. }//初始化
  52. for( int i=;i<n;i++ ){
  53. for( int j=;j<cnt;j++ ){
  54. if( (state[j]&mat[i])== ){
  55. for( int k=;k<cnt;k++ ){
  56. if( (state[j]&(state[k]<<))==&&(state[j]&(state[k]>>))== ){
  57. for( int k2=;k2<cnt;k2++ ){
  58. if( dp[i-][k][k2]==- ) continue;
  59. if( (state[j]&state[k2])==&&(state[k]&(state[k2]>>))==&&(state[k]&(state[k2]<<))== )
  60. dp[ i ][ j ][ k ] = max( dp[i][j][k],dp[i-][k][k2]+ones_state[j] );
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. int main(){
  69. int n,m;
  70. while( scanf("%d%d",&n,&m)== ){
  71. int cnt = init( n,m );
  72. memset( mat,,sizeof( mat ) );
  73. int tmp;
  74. for( int i=;i<n;i++ ){
  75. for( int j=;j<m;j++ ){
  76. scanf("%d",&tmp);
  77. if( tmp== ){
  78. mat[ i ] |= (<<j);
  79. }
  80. }
  81. }
  82. DP( cnt,n,m );
  83. int ans = ;
  84. for( int i=;i<cnt;i++ )
  85. for( int j=;j<cnt;j++ )
  86. ans = max( ans,dp[n-][i][j]);
  87. printf("%d\n",ans);
  88. }
  89. return ;
  90. }

HDU4539+状态压缩DP的更多相关文章

  1. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  2. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  3. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  4. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  5. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  6. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  7. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

  8. Marriage Ceremonies(状态压缩dp)

     Marriage Ceremonies Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  9. HDU 1074 (状态压缩DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...

随机推荐

  1. axure注册码

    ahjesus Axure RP 7.0注册码 用户名:axureuser 序列号:8wFfIX7a8hHq6yAy6T8zCz5R0NBKeVxo9IKu+kgKh79FL6IyPD6lK7G6+t ...

  2. host文件的作用和介绍

    在Window系统中有个Hosts文件(没有后缀名)在Windows98系统下该文件在Windows目录,在Windows2000/XP系统中位于C:\Winnt\System32\Drivers\E ...

  3. 反编译.o到.cpp

    反编译.o到.cpp 现在有很多反编译是对Java的,而.cpp的很少 因为不同的平台.cpp生成的.o是不一样的,但是Java因为有虚拟机的中间作用,生成的.o在不同平台是一样的 反编译的机理 (通 ...

  4. C语言 SDK编程之通用控件的使用--ListView

    一.ListView控件属于通用控件CONTROL中的一种,在SDK编程方式时要使用通用控件 必须包含comctl32.dll,所以代码中要有头文件: commctrl.h 导入库:comctl32. ...

  5. DataGridView显示时间格式

    默认显示时间不显示秒yyyy-MM-dd HH:mm dataGridView.Columns["日期时间字段"].DefaultCellStyle.Format = " ...

  6. C# 与C++的数据转换

    一.类型转化 下面重点罗列下常用的类型转化. C++类型 C#类型 备注说明 Int Int16.Int32 没有悬念,直接转化 Uint UInt16.Uint32.int 在程序中,不太清楚是,就 ...

  7. google map 点与点画线

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. php连接ftp

    PHP连接ftp,发现一个很好用的类库phpseclib.英文原文 Connecting to SFTP with PHP If you need to connect to SFTP using P ...

  9. TImage 的一些操作

    //给 image上写数字. Image1.Picture.Bitmap.Height:= Image1.Height; Image1.Picture.Bitmap.Width:= Image1.Wi ...

  10. python装饰器总结

    一.装饰器是什么 python的装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象.简单的说装饰器就是一个用来返回函数的函数 ...