poj3254 Corn Fields 状态压缩
来源:互联网 发布:哈尔滨淘宝代卖货 编辑:程序博客网 时间:2024/06/07 01:53
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 31 1 10 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<stdio.h>int n,m,a[13][13];int i,j,tem;struct len{int num;int s[6000];}g[13];void var(){int k,t=0;for(k=0;k<(1<<n);k++)if(!(tem&k))//说明没有选择贫瘠的土地{if(k&(k<<1))//排除同一行奶牛相邻的情况continue;g[i].s[t++]=k;//记录满足条件的状态}g[i].num=t;}int main(){int k,sum,d[13][1024];while(scanf("%d%d",&m,&n)!=EOF){for(i=0;i<m;i++){tem=0;for(j=0;j<n;j++){scanf("%d",&a[i][j]);tem=tem<<1;tem+=1-a[i][j];//将图用二进制存起来0表示可以放牛的土地1表示贫瘠的土地,图和是否有牛表示方法是相反的}var();}for(i=0;i<g[0].num;i++)d[0][i]=1;//第一行不受其他影响,满足题意即是一种方法,所以赋值为1for(i=1;i<m;i++){for(k=0;k<g[i].num;k++){d[i][k]=0;for(j=0;j<g[i-1].num;j++)if(!(g[i].s[k]&g[i-1].s[j]))//前后不相邻{d[i][k]+=d[i-1][j];//累加}}}sum=0;for(i=0;i<g[m-1].num;i++)sum+=d[m-1][i];printf("%d\n",sum%100000000);//题目要求啊,开始没注意WA了几次}return 0;}
- poj3254 Corn Fields 状态压缩
- POJ3254:Corn Fields(状态压缩)
- poj3254 Corn Fields (状态压缩)
- [状态压缩]poj3254 Corn Fields
- POJ3254-Corn Fields-状态压缩
- POJ3254 Corn Fields(状态压缩)
- poj3254 Corn Fields----dp状态压缩
- poj3254 - Corn Fields(状态压缩dp)
- poj3254 Corn Fields (状态压缩dp)
- POJ3254 Corn Fields 状态压缩DP
- POJ3254 Corn Fields 状态压缩DP
- POJ3254 Corn Fields (状态压缩DP)
- 状态压缩DP解POJ3254-Corn Fields
- Corn Fields(poj3254)状态压缩dp
- poj3254 状态压缩dp-Corn Fields
- 【POJ3254】Corn Fields(状态压缩DP)
- 【poj3254】Corn Fields 状态压缩dp
- 初识状态压缩dp poj3254 Corn Fields
- 【rzxt】Windows7 64位与32位两者之间到底有什么区别呢?
- poj2015-Permutation Code
- linux下安装netbeans, eclipse的IDE 的方法
- Java中的字符串分割函数
- hdu 2546(dp)
- poj3254 Corn Fields 状态压缩
- http_load测试工具
- 提取Google Play中的应用
- strlen和sizeof的区别
- python操作sqlite3
- android <初级篇> 开发环境的搭建——菜鸟的学习之路
- db2_表操作语句
- Android 开发之envsetup.sh配置lunch
- paypal