| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 18317 | Accepted: 9645 |
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
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
Sample Input
2 3 1 1 1 0 1 0
Sample Output
9
Hint
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.
Source
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
#define mod 100000000
int M,N,top = 0;
///top表示每行最多的状态数
int state[600];
///state存放每行所有的可行状态(即没有相邻的状态)
int dp[20][600];
///dp[i][j]:对于前i行数据,每行有前j种可能状态时的解
int cur[20];
///cur[i]表示的是第i行整行的情况,就是能种与不能种
bool ok(int x) ///判断状态x是否可行
{
if(x&x<<1) return false;///若存在相邻两个格子都为1,则该状态不可行
return true;
}
void init() ///遍历所有可能的状态
{
top = 0;
int total = 1 << N; ///遍历状态的上界,也就是情况的最多种数
// printf("total==%d\n",total);
for(int i = 0; i < total; ++i)
{
if(ok(i))state[++top] = i;
}
// printf("top==%d\n",top);
// for(int i= 1; i<=top; i++)
// {
// printf("state[%d]==%d\n",i,state[i]);
// }
}
bool fit(int x,int k) ///判断状态x 与第k行的实际状态的逆是否有‘重合’
{
if(x&cur[k])return false; ///若有重合,(即x不符合要求)
printf("x==%d cur[%d]==%d\n",x,k,cur[k]);
return true; ///若没有,则可行
}
int main()
{
while(scanf("%d%d",&M,&N)!= EOF)
{
init();
memset(dp,0,sizeof(dp));
for(int i = 1; i <= M; ++i)
{
cur[i] = 0;
int num;
for(int j = 1; j <= N; ++j) ///输入时就要按位来存储,cur[i]表示的是第i行整行的情况,每次改变该数字的二进制表示的一位
{
scanf("%d",&num); ///表示第i行第j列的情况(0或1)
if(num == 0) ///若该格为0
{
cur[i] +=(1<<(N-j));///则将该位置为1(注意要以相反方式存储,即1表示不可放牧
}
}
}
// for(int i=1;i<=M;i++)
// {
// printf("cur[%d]==%d\n",i,cur[i]);
// }
for(int i = 1; i <= top; i++)
{
if(fit(state[i],1)) ///判断所有可能状态与第一行的实际状态的逆是否有重合
{
dp[1][i] = 1; ///若第1行的状态与第i种可行状态吻合,则dp[1][i]记为1
}
}
// printf("----------------\n");
/*
状态转移过程中,dp[i][k] =Sigma dp[i-1][j] (j为符合条件的所有状态)
*/
for(int i = 2; i <= M; ++i) ///i索引第2行到第M行
{
for(int k = 1; k <= top; ++k) ///该循环针对所有可能的状态,找出一组与第i行相符的state[k]
{
if(!fit(state[k],i))continue; ///判断是否符合第i行实际情况
printf("state[%d]==%d cur[%d]==%d\n",k,state[k],i,cur[i]);
for(int j = 1; j <= top ; ++j) ///找到state[k]后,再找一组与第i-1行符合,且与第i行(state[])不冲突的状态state[j]
{
if(!fit(state[j],i-1))continue; ///判断是否符合第i-1行实际情况
if(state[k]&state[j])continue; ///判断是否与第i行冲突
dp[i][k] = (dp[i][k] +dp[i-1][j])%mod; ///若以上皆可通过,则将'j'累加到‘k'上
}
}
}
int ans = 0;
for(int i = 1; i <= top; ++i) ///累加最后一行所有可能状态的值,即得最终结果
{
ans = (ans + dp[M][i])%mod;
}
printf("%d\n",ans);
}
}
本文介绍了一个经典的组合计数问题——如何在限定条件下选择种植玉米的位置。Farmer John 的牧场由 M x N 的网格组成,部分地块无法种植。文章探讨了避免选择相邻地块的方法,并提供了完整的算法实现。
816

被折叠的 条评论
为什么被折叠?



