zoukankan      html  css  js  c++  java
  • POJ 3254

     题目链接:http://poj.org/problem?id=3254

     Time Limit: 2000MS Memory Limit: 65536K

    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

    Hint

    Number the squares as follows:
    1 2 3
      4  

    There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9.

    题解:

    怎么说呢,类似的状压DP题解写多了真的挺无聊的,如果你已经撸完了:

    http://www.cnblogs.com/dilthey/p/7623028.html

    http://www.cnblogs.com/dilthey/p/7604432.html

    这两道题目的话,再来看本题,真的是很水的,所以就懒得写题解了,反正就是差不多的思路;

    AC代码:

     1 #include<cstdio>
     2 #include<cstring>
     3 int m,n,mp[15];
     4 int dp[15][1<<12];
     5 int main()
     6 {
     7     while(scanf("%d%d",&m,&n)!=EOF)//m行n列
     8     {
     9         for(int i=1,tmp;i<=m;i++)
    10         {
    11             mp[i]=0;
    12             for(int j=1;j<=n;j++)
    13             {
    14                 scanf("%d",&tmp);
    15                 tmp=!tmp;
    16                 mp[i]|=tmp;
    17                 if(j!=n) mp[i]<<=1;
    18             }
    19         }
    20 
    21         memset(dp,0,sizeof(dp));
    22         for(int i=0;i<(1<<n);i++)//初始化第一行
    23         {
    24             if( i&(i<<1) || i&mp[1] ) continue;
    25             dp[1][i]=1;
    26         }
    27         for(int r=2;r<=m;r++)//遍历第2~m行
    28         {
    29             for(int i=0;i<(1<<n);i++)//枚举第r行状态
    30             {
    31                 if( i&(i<<1) || i&mp[r] ) continue;
    32                 for(int j=0;j<(1<<n);j++)//枚举第r-1行状态
    33                 {
    34                     if( j&(j<<1) || j&mp[r-1] || i&j ) continue;
    35                     dp[r][i]+=dp[r-1][j];
    36                 }
    37             }
    38         }
    39 
    40         int ans=0;
    41         for(int i=0;i<(1<<n);i++)
    42         {
    43             if( i&(i<<1) || i&mp[m] ) continue;
    44             ans+=dp[m][i];
    45         }
    46         printf("%d
    ",ans%100000000);
    47     }
    48 }

    PS.哦对,有一点倒是值得提醒,需要对100000000取模,因为忘记了这个WA了一发,羞愧脸。

  • 相关阅读:
    PHP中使用Redis
    找回Xcode7的代码折叠功能
    使用 Kingfisher 处理网络图片的读取与缓存
    Swift的两个小窍门
    斯坦福公开课:Developing IOS 8 App with Swift(1-3)心得体会
    What is the relationship between Xcode, Swift and Cocoa?
    C++中static用法总结
    C++中const用法总结
    数据库概念要点
    虚拟内存地址和物理内存地址
  • 原文地址:https://www.cnblogs.com/dilthey/p/7623544.html
Copyright © 2011-2022 走看看