[ACM] POJ 3254 Corn Fields(状态压缩)
2016-01-13 20:20
246 查看
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
Hint
Number the squares as follows:
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
解题思路:
题意为有n行m列的长方形,分成n*m个格子,每一个格子标记为0或1,在这些格子里面放牛,当中1代表该格子能够放牛,0代表不能放牛,且相邻的两个格子不能同一时候有牛。问总的方案数。
思想为状态压缩。把每一行放牛的状态看作一个二进制数,dp[i][j]表示第i行状态为j时前i行共同拥有的方案数。
代码:
for(int i=1;i<=n;i++)//枚举每一行
{
for(int j=0;j<Max;j++)
{
if(!check(i,j))
continue;
for(int k=0;k<Max;k++)
if((j&k)==0)
{
dp[i][j]+=dp[i-1][k];
if(dp[i][j]>=mod)
dp[i][j]%=mod;
}
}
}
int ans=0;
for(int i=0;i<Max;i++)
{
ans+=dp
[i];
if(ans>=mod)
ans%=mod;
}
cout<<ans;
return 0;
}
[/code]
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 8062 | Accepted: 4295 |
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
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
解题思路:
题意为有n行m列的长方形,分成n*m个格子,每一个格子标记为0或1,在这些格子里面放牛,当中1代表该格子能够放牛,0代表不能放牛,且相邻的两个格子不能同一时候有牛。问总的方案数。
思想为状态压缩。把每一行放牛的状态看作一个二进制数,dp[i][j]表示第i行状态为j时前i行共同拥有的方案数。
代码:
for(int i=1;i<=n;i++)//枚举每一行
{
for(int j=0;j<Max;j++)
{
if(!check(i,j))
continue;
for(int k=0;k<Max;k++)
if((j&k)==0)
{
dp[i][j]+=dp[i-1][k];
if(dp[i][j]>=mod)
dp[i][j]%=mod;
}
}
}
int ans=0;
for(int i=0;i<Max;i++)
{
ans+=dp
[i];
if(ans>=mod)
ans%=mod;
}
cout<<ans;
return 0;
}
[/code]
相关文章推荐
- 与独立、自由说再见!微信是怎样扼杀了它们
- jquery——滚动条插件jscroll.js
- 无法解析的外部符号 "public: __thiscall BasePlugin::BasePlugin(class QObject *)"
- CentOS6.5下编译spark1.6.0源码
- Android RecyclerView 使用完全解析 体验艺术般的控件
- MATLABtextscan函数中参数delimiter的使用
- 阿岳之_mariadb安装篇(一)
- 创建ROS消息发布器(publisher)、订阅器(subscriber)
- java工程师面试题积累中...
- 给你90天,成为不一样的自己(转)
- android面试所涉及的范围及学习方向
- HotSpot GC机制初步理解
- 005_Eclipse配置Tomcat
- Erlang 学习(二) 文件夹拷贝
- 如何在Chrome下使用Postman进行rest请求测试
- 《一课经济学》书摘笔记IV
- OpenCV记录鼠标左键点击位置
- VB调用API函数
- CSS设置img属性让图片水平居中/居左/居右的写法
- 4W家庭理财 V2.8已经发布