POJ3254(入门状态压缩dp)
Corn Fields
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 13203 | Accepted: 6921 |
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 3
1 1 1
0 1 0
Sample Output
9 第一道状态压缩dp,水一水
题目大意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的(用1标记),农夫可以在这些格子里放牛,其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。
现在输入数据给出这块地的大小及可否放牧的情况,求该农夫有多少种放牧方案可以选择(注意:任何格子都不放也是一种选择,不要忘记考虑!
//2016.8.8
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int mod = ;
int arr[][];
int dp[][<<];//dp[i][j]表示第i行第j种状态时的方法数。
int state[<<];//记录可行的状态,即两两不相邻,以减少枚举次数。
int cur[];//根据输入记录该行的状态。
int len;//可行状态总数 bool ok(int sta)//判断状态两两不相邻
{
return (sta&(sta<<))==?true:false;
} void init(int m)//初始化可行状态
{
len = ;
for(int i = ; i < (<<m); i++)
if(ok(i))state[len++] = i;
} bool fit(int sta, int k)//判断状态sta是否满足第k行的状态要求
{
return (sta&cur[k])==?true:false;
} int main()
{
int n, m;
while(cin>>n>>m)
{
init(m);
for(int i = ; i < n; i++)
{
int feifa = ;
for(int j = ; j < m; j++)
{
scanf("%d", &arr[i][j]);
if(arr[i][j]==)feifa += (<<(m-j-));//0为不可以放牛,反向存为1
}
cur[i] = feifa;
}
memset(dp, , sizeof(dp));
for(int i = ; i < len; i++)//初始化第一行dp值
if(fit(state[i], ))
dp[][i] = ;
for(int i = ; i < n; i++)
{
for(int j = ; j < len; j++)
{
if(fit(state[j], i))//如果状态state[j]满足该行i的要求,把上一行可行状态的方法数加起来
{
for(int k = ; k < len; k++)//枚举上一行(第i-1行)的状态
{
if(!fit(state[k], i-))continue;//排除不满足i-1行要求的状态
if(state[k]&state[j])continue;//排除不满足状态state[j]的状态
dp[i][j] = (dp[i][j]+dp[i-][k])%mod;
}
}
}
}
int ans = ;
for(int i = ; i < len; i++)//答案是最后一行填各个状态的方法数之和
ans = (ans+dp[n-][i])%mod;
cout<<ans<<endl;
} return ;
}
POJ3254(入门状态压缩dp)的更多相关文章
- poj3254(状态压缩DP)
poj3254 题意 给出一个01矩阵,1表示当前这个位置可以放牛,要求放牛的方案保证牛不能左右或上下相邻,求方案数. 分析 dp[S][i]: 表示到 i 行时的状态S(用二进制数表示),那么状态转 ...
- POJ3254 - Corn Fields(状态压缩DP)
题目大意 给定一个N*M大小的土地,土地有肥沃和贫瘠之分(每个单位土地用0,1来表示贫瘠和肥沃),要求你在肥沃的单位土地上种玉米,如果在某个单位土地上种了玉米,那么与它相邻的四个单位土地是不允许种玉米 ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- POJ1185 - 炮兵阵地(状态压缩DP)
题目大意 中文的..直接搬过来... 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平 ...
- 『最短Hamilton路径 状态压缩DP』
状压DP入门 最短Hamilton路径 Description 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamilton路径. Hamil ...
- poj 3311 floyd+dfs或状态压缩dp 两种方法
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6436 Accepted: 3470 ...
- 状态压缩dp poj 3254 hdu5045
近来感觉状态压缩dp的强大性(灵活利用了二进制运算非常关键). . . 于是做了俩提来看看..毕竟队友是专业的dp.我仅仅是管中窥豹下而已.. 日后有机会再与之玩耍玩耍...ps:假设上天再给我一次机 ...
- 状态压缩DP(大佬写的很好,转来看)
奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
随机推荐
- DataTable的名称要后设置
string sqldatabase = string.Format(dr["sql"].ToString(), drpat["PATIENT_ID"].ToS ...
- PAT (Advanced Level) 1002. A+B for Polynomials (25)
为0的不要输出. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
- Windows Azure Table Storage 解决 Guid 查询问题
在使用 Windows Azure Table Storage 的 CloudTableClient 对Azure 进行数据查询时,会发现在自定义类的Guid类型始终无法去成功查询出数据,对比发现 G ...
- CodeForces 617D Polyline
无脑暴力判断. #include<cstdio> #include<cstring> #include<vector> #include<cmath> ...
- Codeforces#364Div2
A题: 题意:给定一些数,然后每两个组成一对,要求每对的和的大小相同,输出这样的组合 分析:直接模拟就行 #include <iostream> #include <cstdio&g ...
- iOS开发——绘图电池按百分比显示
1.在DrawLine.h文件中提供了一个改变电池电量数值的接口 // // DrawLine.h // Demo-draw2 // // Created by yyt on 16/5/11. ...
- JNI介绍(转)
源:JNI介绍 JNI是在学习Android HAL时必须要面临一个知识点,如果你不了解它的机制,不了解它的使用方式,你会被本地代码绕的晕头转向,JNI作为一个中间语言的翻译官在运行Java代码的An ...
- MongoDB和MySQL的区别
http://www.cnblogs.com/caihuafeng/p/5494336.html MongoDB(文档型数据库):提供可扩展的高性能数据存储 一. 1.基于分布式文件存储 2.高负载情 ...
- css强制折行和隐藏超出部分
一.强制换行1 word-break: break-all; 只对英文起作用,以字母作为换行依据. 2 word-wrap: break-word; 只对英文起作用,以单词作为换行依据. 3 whit ...
- ECSHOP中ajax的调用原理
ECSHOP中ajax的调用原理 ecshop中ajax的调用原理. 1.首先ecshop是如何定义ajax对象的. ecshop中的ajax对象是在js/transport.js文件中定义的.里面是 ...