poj3254Corn Fields题解
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 9623 | Accepted: 5092 |
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
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.
题目大意:
给你一个N*M的矩阵,矩阵里的元素由0和1组成,1代表肥沃的土地可以种草,0则不可以种草。如下: N=2 M=3 1 1 1 0 1 0 现有若干头牛,请将它们放入有草的地方吃草,注意上下左右不能相邻。 那么问题来了,请问有多少种放法?
分析:
#include<iostream>
#include<sstream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<math.h>
#include<time.h>
#include<algorithm> #define LEN 1000000
#define INF 99999
#define ALLSTATES 4096 //最大状态 using namespace std; int allstates=;
int n,m;
int F[][ALLSTATES]={}; //方法数
int Matrix[][]={};//土地的样子 bool andMatrix(int row,int states)//是否和土地兼容
{
for(int i=;i<=m;i++)
{
if(Matrix[row][i]==)//如果这里是空的 那么肯定不能放牛
{
if(states&(<<i-))
{
return false;
}
} }
return true;
} bool linetest(int states)//判断行是否相邻
{
int i=; while(i<m)
{
if(states&(<<i))//如果是1 那么你的左边不应该是1
{
if(i+<m && states&(<<(i+))){ return false; }//是1返回错误
else{i+=;}//不是1 跳过一格
}
else{ i++; }
}
return true;
} bool upanddown(int upstates,int downstates)//判断上下是否相邻
{
int i=; while(i<m)
{
if(upstates&(<<i) && downstates&(<<i))
{
return false;
}
else
{
i++;
} }
return true;
} int main()
{
//读入--------------------------------------------
cin>>n>>m;
//读入矩阵
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
cin>>Matrix[i][j];
}
//处理--------------------------------------------
allstates=<<m;//m个格子的所有状态
// cout<<andMatrix(1,5)<<endl;
// cout<<linetest(5)<<endl;
//cout<<allstates;
//先处理第一行
for(int j=;j<allstates;j++)//allstates为所有状态
{
if(andMatrix(,j) && linetest(j))//
{
F[][j]=;
}
}
//cout<<allstates;
//处理后面的行数
for(int row=;row<=n;row++)
for(int j=;j<allstates;j++)
{
if(andMatrix(row,j)== || !linetest(j))//如果j不符合土地直接跳过
{
continue;
}
for(int k=;k<allstates;k++)
{
if(F[row-][k] && upanddown(j,k))
{
F[row][j]+=F[row-][k];
}
}
}
//统计所有方法数
int ct=; //for(int i=1;i<=n;i++)
// {
// for(int j=0;j<allstates;j++)
// {
// cout<<F[i][j];
// }
// cout<<endl;
// }
//cout<<ct;
for(int j=;j<allstates;j++)
{
ct+=F[n][j];
ct%=;
}
cout<<ct; return ;
}
poj3254Corn Fields题解的更多相关文章
- poj3254Corn Fields
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13765 Accepted: 7232 Desc ...
- 洛谷 P1879 [USACO06NOV]玉米田Corn Fields 题解
P1879 [USACO06NOV]玉米田Corn Fields 题目描述 Farmer John has purchased a lush new rectangular pasture compo ...
- 洛谷 P2212 [USACO14MAR]浇地Watering the Fields 题解
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
- POJ3254Corn Fields(状态压缩DP入门)
题目链接 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一 ...
- poj3254Corn Fields(状压)
http://poj.org/problem?id=3254 第一个状压题 思路挺好想 用二进制表示每行的状态 然后递推 用左移 右移来判断是不是01间隔出现 c大神教的 我的代码WA在这个地方了.. ...
- POJ3254Corn Fields(状压DP)
题意: John 有一个豪华的M*N个格子组成的新牧场 他想种美味的玉米 但是有些位置不能种 而且他种地不选择相邻的格子 求所有可能的种地方法 (不种也算一种选择)输入:第一行M和N, 第二行M*N地 ...
- POJ3254Corn Fields——状态压缩dp
题目:http://poj.org/problem?id=3254 1.枚举行: 2.把有影响的“放不放牛”加入参数中,用二进制数表示该位置放不放牛,再用十进制数表示二进制数: 3.优美的预处理lis ...
- POJ3254:Corn Fields——题解
http://poj.org/problem?id=3254 题面来自洛谷:https://www.luogu.org/problemnew/show/1879 农场主John新买了一块长方形的新牧场 ...
- POJ3254Corn Fields (状态压缩or插头DP)
Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...
随机推荐
- 修改MySQL默认最大连接数
修改MySQL默认最大连接数 MYSQL数据库安装完成后,默认最大连接数是100,一般流量稍微大一点的论坛或网站这个连接数是远远不够的,增加默认MYSQL连接数的方法有两个: 方法一: 进入MYSQL ...
- [Linked List]Copy List with Random Pointer
Total Accepted: 53943 Total Submissions: 209664 Difficulty: Hard A linked list is given such that ea ...
- (转)三角函数计算,Cordic 算法入门
由于最近要使用atan2函数,但是时间上消耗比较多,因而网上搜了一下简化的算法. 原帖地址:http://blog.csdn.net/liyuanbhu/article/details/8458769 ...
- Simple Daemon Shell
PROPATH="/var/www/html/" PROGRAM="vertical" LOGNAME="/tmp/monitor.vertical. ...
- android.view.InflateException: Binary XML file line #7: Error inflating class(OOM)
由于页面含有ImageView引起的内存溢出. 作如下处理:在OnDestroy中 Drawable d = imageView.getDrawable(); if (d != null) d.set ...
- make clean与make distclean的区别
make clean仅仅是清除之前编译的可执行文件及配置文件. 而make distclean要清除所有生成的文件. Makefile 在符合GNU Makefiel惯例的Makefile中,包含了一 ...
- jquery ajax 跨域提交(附IE浏览器解决方案)
后台输出内容之前需要指定header("Access-Control-Allow-Origin: *"); post 之前 jQuery.support.cors = true; ...
- Python之路第九天,高级(1)-网络编程
SOCKET编程 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. so ...
- A + B Problem II---hdu1002
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- JSON基础学习
定义 JSON时轻量级的文本数据交换格式,独立于语言,比xml更小更快更易解析 JSON解析器和JSON库支持不同的编程语言 4个基本规则 1. 并列数据间用 逗号, 2. 映射用冒号表示 3. 并列 ...