POJ3254——Corn Fields

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor等20+工具畅享,效率翻倍! 阅读详情
Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8186 Accepted: 4362

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

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.

Source


水题,直接看代码吧


#include <map>    
#include <set>    
#include <list>    
#include <stack>    
#include <vector>    
#include <queue>    
#include <cmath>    
#include <cstdio>    
#include <cstring>    
#include <iostream>    
#include <algorithm>    
  
using namespace std;  

int dp[15][500];
int s[500];
int a[15];
int res;
const int mod = 100000000;

int main()
{
	int m, n;
	while (~scanf("%d%d", &m, &n))
	{
		memset (dp, 0, sizeof(dp) );
		res = 0;
		for (int i = 0; i < (1 << n); i++)
		{
			if ( !(i & ( i << 1)) && !( i & ( i >> 1)) )
			{
				s[res++] = i;
			}
		}
		int is_ok;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				scanf("%d", &is_ok);
				if (!is_ok)
				{
					a[i] |= (1 << j);
				}
			}
		}
		for (int i = 0; i < res; i++)
		{
			if (a[0] & s[i])
			{
				continue;
			}
			dp[0][i] = 1;
		}
		for (int i = 1; i < m; i++)
		{
			for (int j = 0; j < res; j++)
			{
				if (a[i] & s[j])
				{
					continue;
				}
				for (int k = 0; k < res; k++)
				{
					if (a[i - 1] & s[k])
					{
						continue;
					}
					if (s[j] & s[k])
					{
						continue;
					}
					dp[i][j] += dp[i - 1][k];
					dp[i][j] %= mod;
				}
			}
		}
		int ans = 0;
		for (int i = 0; i < res; i++)
		{
			ans += dp[m - 1][i];
			ans %= mod;
		}
		printf("%d\n", ans);
	}
	return 0;
}


POJ 3254 Corn Fields(状态压缩)代码详解 Corn FieldsPOJ 3254) Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18002 Accepted: 9478 Description Farmer John has purchased a lush new rectangular pasture composed of M... 阅读详情

相关推荐

Corn Fields

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.

Corn Fields(状压DP)

描述 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. Regrettab...

__1_1__的博客 447

IPMI的SOL (Serial Over LAN) 和 UART

UART全名是Universal Asynchronous Receiver/Transmitter(通用異步接收/发送器),它是一个常用的異步串行传输协议。透过UART传输前,接收方须要先知道传输方的鲍率(Baud rate):单位时间内,讯号状态变化的次数。例如 115200 bps,表示每秒会传送115200个bits--> 因此读取率就是 1/115200=8.6微秒读取一次讯号。封包内的资料长度:通常是 8bits。

yeiris的博客 1万+

POJ3254:状态DP入门级理解

状态DP与二进制及位运算密切相关。 在这道题中,对于 一行0或1 所表示的状态,我们就可以把它们当作一个数的二进制,而它们所代表的“数”,即是表示这一行的状态。 比如(111) 就可以用 一个数 7 来表示 这一行的状态 那么接下来就要去判断 我们安排的装态 是否合法,由题意,只能在“1”上放牧,而且任意两只牛相邻是不合法的, 那么要考虑的主要有三个方面: 1: 一行安排的状态,

lxp6164的博客 376

POJ-3254(状压dp

POJ-3254

Terabyte_Dance的博客 313

POJ - 3254 - Corn Fields ( 状压dp )

题目链接:点击进入 题目 题意 n * m 的土地,告诉你每个位置可以不可以种玉米,摆放奶牛需要放在可以种玉米的地方,同时奶牛不能相邻( 上下左右 ),问共有多少种摆放奶牛的方案 思路 牛与牛之间不能相邻,每只牛能不能放只与上一行和当前这一行有关,所以,我们可以想到一行一行的递推,这是行与行之间,行内部之间也不能相邻,这个,我们可以预处理出一行所有的可行的不相邻的状态( 最多 1<<m 种 ),二维dp,一维表示第几行,一维表示行内部的状态,每行内部的状态可以用二进制来表示( 因为 m 只

Cosmic_Tree的博客 208

POJ - 3254 Corn Fields (状压DP)

题意:个格子,有些可以放牛,有些不可以放牛,放的牛不能相邻,问共有多少种放牛的方案? 思路:状压,表示第行状态为前行的方案数目,外层循环一行一行的枚举,内层嵌套枚举第行的状态和第行的状态,最后答案为 #include <stdio.h> #include <string.h> const int mod = 100000000; int dp[15][1 <&l...

GYH0730的博客 346

【题解】poj 3254 Corn Fields

题目描述 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地。John打算在牧场上的某几格里种上美味的玉米,供他的奶牛们享用。遗憾的是,有些土地相当贫瘠,不能用来种玉米。并且,奶牛们喜欢独占一块地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块玉米地有公共边。John想知道,如...

C@bMv7GRVt)Q 478

poj 3254 Corn Fields 状压dp

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19399   Accepted: 10189 Description Farmer John has purchased a lush new rectangular pasture composed of M by...

liluoyu_1016的博客 225

POJ 3254 Corn Fields 状态压缩(dp)

链接:http://poj.org/problem?id=3254Corn FieldsTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 18865 Accepted: 9912DescriptionFarmer John has purchased a lush new rectangular pasture composed o...

SYP___ 279

POJ3254 Corn Fields

原题链接http://poj.org/problem?id=3254 POJ 3254 corn fields 状态压缩动态规划

昨天的我现在的未来 340

POJ 3254 Corn Fields

<br />记录状态的DP.<br />每一行的状态有2^12种可能(其中一部分状态是无效的),且与前一行的状态相关,<br />题目要求计算的可以理解为到达最后一行各个有效状态的次数之和。<br />输入1 for fertile, 0 for infertile,为了计算方便进行取反,0 for fertile, 1 for infertile。<br /><br /><br />/*Corn Fields Time Limit: 2000MS Memory Limit: 65536K Tota

mimo9527的专栏 846

POJ-3254 Corn Fields

题目链接: \quad POJ-3254 题目大意: \quad 给一个 N×MN×MN \times M 块土地,111 表示这块土地肥沃,000 表示土地贫瘠;现在只能在肥沃的土地上种玉米,问有多少种种法使得玉米两两不相邻 (相邻指的是上下走右四个方向) ? \quad Tips : 也可以一颗都不种。还要取模。 数据范围: \quad 1≤N,M≤121≤N,M≤121 \l...

XzzF1024的博客 423

Corn Fields POJ - 3254

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, s...

weixin_41544329的博客 159

poj3254 Corn Fields

poj3254分析这道题算是状态压缩dp的入门题吧,知道状态压缩是个什么套路。题目http://poj.org/problem?id=3254代码#include <cstdio> #include <cstring> #include <algorithm> #include <cmath>using namespace std;const int mod=100000000; int n,m,t

天道酬勤 227

poj3254Corn Fields 状态压缩dp

AC通道:http://vjudge.net/problem/POJ-3254 【题目大意】 农夫约翰购买了一处肥沃的矩形牧场,分成M*N(1<=M<=12; 1<=N<=12)个格子。他想在那里的一些格子中种植美味的玉米。遗憾的是,有些格子区域的土地是贫瘠的,不能耕种。 精明的约翰知道奶牛们进食时不喜欢和别的牛相邻,所以一旦在一个格子中种植玉米,那...

anzhi7214的博客 251

POJ3254Corn Fields

http://poj.org/problem?id=3254 题意:给你一块n*m(0<n,m<=12)的地图,其中有的方格是肥沃的(用1表示),有的方格是贫瘠的(用0表示)。现在约翰要在肥沃的土地上放奶牛,且要求不能有两个奶牛相邻,请问有多少种方案数。 状压DP入门题。 首先预处理每一行不考虑贫瘠地时所有可行的放置状态,用states数组存着。 然后令f(i,j)为...

anlongshi2245的博客 218
上一篇: POJ1185——炮兵阵地
下一篇: POJ2411——Mondriaan's Dream
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值