poj 3254 Corn Fields (状态压缩dp)
2016-01-26 11:58
351 查看
Corn Fields
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
Sample Output
状态压缩,将一行的可能出现的可行状态打表
判断每一行与上一行的关系
dp[i][j] += dp[i-1][k]; 0 <k< 1<<m
/*************************************************
Author :SunMing
Created Time :2016年01月26日 星期二 08时18分21秒
File Name :A.cpp
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int mod = 1e8;
int n,m;
int dp[15][1<<12], state[1<<12], Map[15][1<<12];
void Init()
{
int top = 0;
for(int i = 0; i < (1<<12); i++)
if(!(i & (i<<1)))
state[top++] = i;
}
bool Judge(int x,int y)
{
for(int i=1;i<=m;i++)
if((y & 1<<(i-1)) && !Map[x][i])
return false;
return true;
}
int main()
{
Init();
while(~scanf("%d%d",&n,&m))
{
memset(dp, 0, sizeof(dp));
memset(Map, 0, sizeof(Map));
for(int i=1;i<=n;i++)
for(int j=1; j<=m;j++)
scanf("%d",&Map[i][j]);
for(int i=1; i<=n;i++)
{
for(int j=0; state[j] <(1<<m); j++)
{
if(Judge(i, state[j]))
{
if(i == 1)
{
dp[i][j] = 1;
continue;
}
for(int k=0; state[k] < (1<<m); k++)
{
if((state[j] & state[k]) == 0 )
dp[i][j] += dp[i-1][k];
}
}
}
}
long long ans = 0;
for(int i=0; state[i] < (1<<m); i++)
ans = (ans + dp
[i]) % mod;
cout<<ans<<endl;
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10923 | Accepted: 5723 |
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
状态压缩,将一行的可能出现的可行状态打表
判断每一行与上一行的关系
dp[i][j] += dp[i-1][k]; 0 <k< 1<<m
/*************************************************
Author :SunMing
Created Time :2016年01月26日 星期二 08时18分21秒
File Name :A.cpp
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int mod = 1e8;
int n,m;
int dp[15][1<<12], state[1<<12], Map[15][1<<12];
void Init()
{
int top = 0;
for(int i = 0; i < (1<<12); i++)
if(!(i & (i<<1)))
state[top++] = i;
}
bool Judge(int x,int y)
{
for(int i=1;i<=m;i++)
if((y & 1<<(i-1)) && !Map[x][i])
return false;
return true;
}
int main()
{
Init();
while(~scanf("%d%d",&n,&m))
{
memset(dp, 0, sizeof(dp));
memset(Map, 0, sizeof(Map));
for(int i=1;i<=n;i++)
for(int j=1; j<=m;j++)
scanf("%d",&Map[i][j]);
for(int i=1; i<=n;i++)
{
for(int j=0; state[j] <(1<<m); j++)
{
if(Judge(i, state[j]))
{
if(i == 1)
{
dp[i][j] = 1;
continue;
}
for(int k=0; state[k] < (1<<m); k++)
{
if((state[j] & state[k]) == 0 )
dp[i][j] += dp[i-1][k];
}
}
}
}
long long ans = 0;
for(int i=0; state[i] < (1<<m); i++)
ans = (ans + dp
[i]) % mod;
cout<<ans<<endl;
}
return 0;
}
相关文章推荐
- Hadoop集群中SSH免密码登录
- 设置 tabBar 下面文字和图片颜色
- hdu 5282 Senior's String 两次dp
- 60行JavaScript俄罗斯方块
- Python生成器表达式
- Json数据的序列化与反序列化的三种经常用法介绍
- Android自定义组件ListPopWindow
- 【数据表格】-005-增删改查
- amfe手机淘宝前端团队 https://github.com/amfe
- URL短地址---实现方案收集
- 仿新浪微博手机客户端呼出菜单
- 关于竞品分析,这应该是最实用的分析流程
- 【HTML5】DOMContentLoaded事件
- 浏览器缓存相关HEADER头
- 三步走--低功耗蓝牙BLE开发实战
- jdk&jre FAQ
- 2016,开始用博客记录工作!
- head first设计模式(一)——策略模式
- swift 单例实现
- linux 问答