poj 3254(状态压缩)
2016-07-29 14:12
288 查看
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
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.
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int m, n;
int map[15];
int dp[15][1 << 13];
int st[1 << 13];
const int mod = 1e8;
int main()
{
while (scanf("%d%d", &m, &n) != EOF)
{
memset(map, 0, sizeof(map));
memset(dp, 0, sizeof(dp));
memset(st, 0, sizeof(st));
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
{
int a;
scanf("%d", &a);
map[i] = map[i] * 2 + (1 - a);<span style="white-space:pre"> </span>//将其取反,以便后面0(1)判断
}
int k = 0;
for (int i = 0; i < 1 << n; i++)
{
if (!(i&(i << 1)))
st[k++] = i;
}
for (int i = 0; i < k; i++)
if (!(map[0] & st[i]))
{
dp[0][i] = 1;
}
for (int i = 1; i < m; i++)
{
for (int j = 0; j < k; j++)
{
if ((map[i] & st[j]))
continue;
for (int f = 0; f < k; f++)
{
if ((st[f] & map[i - 1]))
continue;
if (!(st[f] & st[j]))
dp[i][j] += dp[i - 1][f];
}
}
}
int ans = 0;
for (int i = 0; i < k; i++)
ans = (ans + dp[m - 1][i]) % mod;
printf("%d\n", ans);
}
return 0;
}
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.
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int m, n;
int map[15];
int dp[15][1 << 13];
int st[1 << 13];
const int mod = 1e8;
int main()
{
while (scanf("%d%d", &m, &n) != EOF)
{
memset(map, 0, sizeof(map));
memset(dp, 0, sizeof(dp));
memset(st, 0, sizeof(st));
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
{
int a;
scanf("%d", &a);
map[i] = map[i] * 2 + (1 - a);<span style="white-space:pre"> </span>//将其取反,以便后面0(1)判断
}
int k = 0;
for (int i = 0; i < 1 << n; i++)
{
if (!(i&(i << 1)))
st[k++] = i;
}
for (int i = 0; i < k; i++)
if (!(map[0] & st[i]))
{
dp[0][i] = 1;
}
for (int i = 1; i < m; i++)
{
for (int j = 0; j < k; j++)
{
if ((map[i] & st[j]))
continue;
for (int f = 0; f < k; f++)
{
if ((st[f] & map[i - 1]))
continue;
if (!(st[f] & st[j]))
dp[i][j] += dp[i - 1][f];
}
}
}
int ans = 0;
for (int i = 0; i < k; i++)
ans = (ans + dp[m - 1][i]) % mod;
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- 什么情况下适合在knockout项目中应用KOMapper
- 决策树算法(六)——构建决策树
- 框架、架构摄取
- 面试题
- R语言 批量规划求解
- tomcat8 https ssl证书配置
- VC++获取不同Windows版本的方法
- eCryptfs - creat系统调用
- httpurlconnection
- 1095. Cars on Campus (30)
- u盘刻录光盘后空间缩小解决方法
- a20 nand更换emmc 版本sdk修改记录
- 使用TortoiseGit处理代码冲突
- curl 获取外网IP
- JSON 数据格式
- #开篇第一章
- (可再看)基于深度学习的目标检测研究进展
- solr5的基本操作
- 回味经典——uboot1.1.6 之 第二阶段 第三阶段
- android自定义控件之中间是斜线的占比条