本文主要是介绍【POJ3254】Corn Fields,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 8670 | Accepted: 4622 |
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 34
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
1.判断A < B 使用 A 并(~B) = 0.
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
const int mod = 100000000;
int N,M;
int state[2 << 13];
int dp[13][2<<13];
int cur[13];
int stanums;
int upp;
void init()
{
stanums = 0;
for(int i=0;i<upp;i++)
{
if((i & (i<< 1)))
{
}
else
{
state[stanums ++] = i;
}
}
}
bool fit(int idx,int i)
{
if((state[idx] & cur[i]) == 0) return true;
return false;
}
int main()
{
freopen("1.txt","r",stdin);
while(scanf("%d%d",&N,&M) != EOF)
{
memset(dp,0,sizeof(dp));
upp = (1 << M);
init();
memset(cur,0,sizeof(cur));
int tmp;
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
{
scanf("%d",&tmp);
if(tmp == 0)
{
//scanf("")
cur[i] += (1 << (M-j));
}
}
for(int i=0;i<stanums;i++)
{
if(fit(i,1))
dp[1][i] = 1;
}
for(int i=2;i<=N;i++)
{
for(int j=0;j<stanums;j++)
{
if(!fit(j,i)) continue;
for(int k=0;k<stanums;k++)
{
if(!fit(k,i-1)) continue;
if(state[j] & state[k]) continue;
dp[i][j] = (dp[i][j] + dp[i-1][k]) % mod;
}
}
}
int ans = 0;
for(int i=0;i<stanums;i++)
{
ans = (ans + dp[N][i]) % mod;
}
printf("%d\n",ans);
}
while(1);
return 0;
}
这篇关于【POJ3254】Corn Fields的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!