您的位置:首页 > 其它

POJ 3254 - Corn Fields (状压DP)

2017-11-21 22:02 369 查看
Corn Fields

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17014 Accepted: 8987
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.
Source

USACO 2006 November Gold
题意:

给你一个地图,1可以放置。 让你求出放置的方案总数。注意放置不能上下左右相邻,放置数可以为0。

POINT:

nm才12,可以状压遍历状态来做。一层一层更新答案。

#include <string>
#include <string.h>
#include <iostream>
#include <queue>
#include <math.h>
#include <stdio.h>
using namespace std;
const int maxn = 2;
const int mod = 100000000;
int a[22][22];
int kind[1000];
int n,m;
int num=0;
void dfs()
{
for(int i=0;i<=((1<<m)-1);i++){
int x=i,flag=1;
while(x){
if(x&1&&flag==1) flag=0;
else if(x&1&&flag==0){
break;
}
else{
flag=1;
}
x>>=1;
}
if(x==0){
kind[++num]=i;
}
}
}
int dp[5000];
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&a[i][j]);
}
}
dfs();
for(int j=1;j<=num;j++){
int x=kind[j],flag=1;
for(int cnt=1;cnt<=m;cnt++){
if(a[1][cnt]==0&&((x>>(cnt-1))&1)==1){
flag=0;
break;
}
}
if(flag) dp[x]=1;
}
int dp1[5000];
for(int i=2;i<=n;i++){
memset(dp1,0,sizeof dp1);
for(int j=1;j<=num;j++){
for(int k=1;k<=num;k++){
int x=kind[j];
int y=kind[k];
int flag=1;
if((x&y)>0){
continue;
}
for(int cnt=1;cnt<=m;cnt++){
if(a[i][cnt]==0&&((y>>(cnt-1))&1)==1){
flag=0;
break;
}
}
if(flag){
(dp1[y]+=dp[x])%=mod;
}
}
}
for(int j=1;j<=num;j++){
dp[kind[j]]=dp1[kind[j]];
}
}
int ans=0;
for(int i=1;i<=num;i++){
(ans+=dp[kind[i]])%=mod;
}
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: