zoukankan      html  css  js  c++  java
  • POJ 3254 Corn Fields

    Corn Fields

    Time Limit: 2000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 3254
    64-bit integer IO format: %lld      Java class name: Main
     

    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

    Source

     
    解题:状压dp...dpi][j]表示到达i行状态为j的路径有多少条
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int mod = 100000000;
    18 int n,m,mp[18],s[1000],dp[18][1000],tot;
    19 int main() {
    20     int tmp;
    21     while(~scanf("%d %d",&n,&m)) {
    22         memset(mp,0,sizeof(mp));
    23         for(int i = 0; i < n; i++)
    24             for(int j = 0; j < m; j++) {
    25                 scanf("%d",&tmp);
    26                 mp[i] |= (!tmp)<<j;
    27             }
    28         for(int i = tot = 0; i < (1<<m); i++) {
    29             if(i&(i<<1)) continue;
    30             s[tot++] = i;
    31         }
    32         memset(dp,0,sizeof(dp));
    33         for(int i = 0; i < tot; i++)
    34             dp[0][i] = !(mp[0]&s[i]);
    35         int ans = 0;
    36         for(int i = 1; i < n; i++) {
    37             for(int j = 0; j < tot; j++) {
    38                 if(s[j]&mp[i]) continue;
    39                 for(int k = 0; k < tot; k++) {
    40                     if(s[j]&s[k]) continue;
    41                     dp[i][j] += dp[i-1][k];
    42                     dp[i][j] %= mod;
    43                 }
    44             }
    45         }
    46         for(int i = 0; i < tot; i++){
    47             ans += dp[n-1][i];
    48             ans %= mod;
    49         }
    50         printf("%d
    ",ans);
    51     }
    52     return 0;
    53 }
    View Code
     1 #include <cstdio>
     2 #include <cstring>
     3 using namespace std;
     4 const int maxn = 13;
     5 const int mod = 100000000;
     6 int mp[maxn],dp[maxn][1<<maxn],s[1<<maxn];
     7 int main() {
     8     int n,m,tmp,tot;
     9     while(~scanf("%d %d",&n,&m)) {
    10         memset(mp,0,sizeof mp);
    11         memset(dp,0,sizeof dp);
    12         for(int i = 0; i < n; ++i) {
    13             for(int j = 0; j < m; ++j) {
    14                 scanf("%d",&tmp);
    15                 mp[i] |= (!tmp)<<j;
    16                 //不能种的用1表示,
    17                 //因为只要不能种的种了就非法
    18             }
    19         }
    20         for(int i = tot = 0; i < (1<<m); ++i)
    21             if((i&(i<<1)) == 0) s[tot++] = i;
    22         for(int i = 0; i < tot; ++i)
    23             dp[0][i] = !(mp[0]&s[i]);//没有冲突,就一种方案
    24         for(int i = 1; i < n; ++i)
    25             for(int j = 0; j < tot; ++j) {
    26                 if(mp[i]&s[j]) continue;
    27                 for(int k = 0; k < tot; ++k) {
    28                     if(s[j]&s[k]) continue;
    29                     dp[i][j] += dp[i-1][k];
    30                     dp[i][j] %= mod;
    31                 }
    32             }
    33         int ret = 0;
    34         for(int i = 0; i < tot; ++i){
    35             ret += dp[n-1][i];
    36             ret %= mod;
    37         }
    38         printf("%d
    ",ret);
    39     }
    40     return 0;
    41 }
    View Code
  • 相关阅读:
    221. 最大正方形
    9. 回文数
    2. 两数相加
    1. 两数之和
    HDU 1864 最大报销额
    47 java包打成本地maven
    46 数组中的元素进行位置交换
    5 docker安装kibana
    45 vue图片放大预览
    4 docker中安装es
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4004771.html
Copyright © 2011-2022 走看看