zoukankan      html  css  js  c++  java
  • 洛谷P1879 [USACO06NOV]玉米田Corn Fields

    题目描述

    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.

    农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地。John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用。

    遗憾的是,有些土地相当贫瘠,不能用来种草。并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边。

    John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案)

    输入输出格式

    输入格式:

    第一行:两个整数M和N,用空格隔开。

    第2到第M+1行:每行包含N个用空格隔开的整数,描述了每块土地的状态。第i+1行描述了第i行的土地,所有整数均为0或1,是1的话,表示这块土地足够肥沃,0则表示这块土地不适合种草。

    输出格式:

    一个整数,即牧场分配总方案数除以100,000,000的余数。

    输入输出样例

    输入样例#1: 
    2 3
    1 1 1
    0 1 0
    输出样例#1: 
    9

    题解:
    这就是一道标准的状态压缩题 (n,m<=12就是提示啊)
    二进制表示一行的状态,1为种草,0为不种草
    对输入的信息稍加处理,也用二进制表示,将不适宜种草的变为1,适宜的变为0
    之后,dp[i][st]表示到某一行i状态为st总的可行方案数
    首先需要判断st是否合法(st&(st>>1)判断是否有相邻的,若有,则值不为0;st&map[i]判断是否在不能种草的地方种草,若是,则不为0)
    dp[i][st]=sum(dp[i-1][st']),st'为第i行为st时上一行的合法状态, 注意还要判断上下有没有相邻的(就是st&st'是否为0)
    最后的答案为max(dp[m][st])

    注意:细节很多啊,而且不要把n与m弄混了

    代码:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #define P 1000000000
     5 using namespace std;
     6 
     7 typedef long long ll;
     8 int dp[15][1<<15],map[15];
     9 int m,n;
    10 
    11 bool check(int st,int num){
    12     if(st&(st>>1)) return false;
    13     if(st&map[num]) return false;
    14     return true;
    15 }
    16 bool check2(int st1,int st2){
    17     if(st1&st2) return false;
    18     return true;     
    19 }
    20 
    21 int main()
    22 {
    23     int i,j,w,x,k;
    24     scanf("%d%d",&m,&n);
    25     for(i=0;i<m;i++)
    26         for(j=0;j<n;j++) scanf("%d",&x),map[i]=map[i]*2+(1-x);
    27         
    28     w=1<<n;
    29     //dp 0
    30     for(i=0;i<w;i++)
    31         if(check(i,0)) dp[0][i]=1;else dp[0][i]=0;
    32     //dp rest
    33     for(i=1;i<m;i++)
    34         for(j=0;j<w;j++) if(check(j,i))
    35             for(k=0;k<w;k++) 
    36                 if(check(k,i-1) && check2(j,k)) 
    37                     dp[i][j]=((ll)dp[i][j]+dp[i-1][k])%P;
    38     
    39     int ans=0;
    40     for(i=0;i<w;i++) if(check(i,m-1)) ans=((ll)ans+dp[m-1][i])%P;
    41     printf("%d
    ",ans);
    42     
    43     return 0;
    44 }
    View Code
    既然选择了远方,便只顾风雨兼程
  • 相关阅读:
    【CF722E】Research Rover
    【CF1519E】Off by One
    【CF1519D】Maximum Sum of Products
    【CF702F】T-Shirts
    【CF1364E】X-OR
    RPA应用场景-自动轮询汇总报表
    RPA应用场景-财务报表统计整合
    RPA应用场景-公积金贷款业务数据整合和报送
    RPA应用场景-信用卡交易争议后续流程
    RPA应用场景-日终清算操作
  • 原文地址:https://www.cnblogs.com/lindalee/p/7718987.html
Copyright © 2011-2022 走看看