zoukankan      html  css  js  c++  java
  • poj3254 Corn Fields(状压dp)

    Corn Fields
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 17742   Accepted: 9340

    Description

    Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.

    Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.

    Input

    Line 1: Two space-separated integers: M and N
    Lines 2..M+1: Line i+1 describes row i of the pasture with N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)

    Output

    Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.

    Sample Input

    2 3
    1 1 1
    0 1 0

    Sample Output

    9

    分析:状态压缩DP。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 int dp[13][1<<12],cur[13];//cur[i]记录原矩阵的第i行 
     6 int can[1<<12];
     7 int main()
     8 {
     9     int M,N;//M行N列 
    10     //原矩阵1为可种植0为不可种植
    11     scanf("%d%d",&M,&N);
    12     int tot=0;
    13     for(int i=0;i<(1<<N);i++)//1与1不能相邻 的所有情况 
    14         if((i&(i<<1))==0) can[tot++]=i;//1为可种植0为不可种植
    15         
    16     for(int i=1;i<=M;i++)
    17         for(int j=0;j<N;j++)
    18         {
    19             int num;
    20             scanf("%d",&num);
    21             if(num==0) cur[i]=(cur[i]|(1<<j));//把第j位变为1 
    22             //把1变为0,0变为1,即0为可种植,1为不可种植
    23         }
    24 /*这样处理后第i行可行的情况变为(cur[i]&can[k])==0,0<=k<tot;
    25 例如第i行为1 1 1,0与1调换后为0 0 0,则种植方案为1 0 1,1 0 0,0 1 0,0 0 1,0 0 0; 
    26 */
    27     for(int i=0;i<tot;i++)
    28         if((cur[1]&can[i])==0) dp[1][can[i]]=1;//预处理第一行 
    29     //cur[1]&can[i]这部分要加括号,位运算符&优先级比==低 
    30     for(int i=1;i<M;i++)
    31     {
    32         for(int j=0;j<tot;j++)
    33         {
    34             if((can[j]&cur[i])==0)
    35             {
    36                 for(int k=0;k<tot;k++)//枚举第i+1行的情况 
    37                 if((can[k]&cur[i+1])==0&&(can[j]&can[k])==0)
    38                     dp[i+1][can[k]]+=dp[i][can[j]];
    39             }
    40         }
    41     }
    42     int ans=0;
    43     for(int i=0;i<tot;i++)
    44     {
    45         ans+=dp[M][can[i]];
    46         ans%=100000000;
    47     }
    48     printf("%d
    ",ans);
    49     return 0;
    50 }
    View Code



  • 相关阅读:
    转载-如何高效的学习技术
    Lc176-第二高的薪水
    Lc4-寻找两个有序数组的中位数
    Lc175-组合两个表
    Lc3-无重复字符的最长子串
    Lc2-俩数相加
    Lc1- 两数之和
    jpa-子查詢
    20191225页面样式
    leetcode二刷结束
  • 原文地址:https://www.cnblogs.com/ACRykl/p/8391674.html
Copyright © 2011-2022 走看看