[ACM] POJ 3254 Corn Fields(状态压缩)

2024-01-28 13:18

本文主要是介绍[ACM] POJ 3254 Corn Fields(状态压缩),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8062 Accepted: 4295

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 34  

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行共有的方案数。

代码:

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int mod=100000000;
const int maxn=12;
int dp[maxn+1][(1<<maxn)+1];
int num[maxn+1];
int n,m;bool check(int i,int x)//检查第i行出现状态x是否合法
{if((x&num[i])!=x)//很巧妙,判断第i行出现的状态x是否合法,为什么这么写,因为0的地方不能放牛,//合法状态与原始状态0位且为0,原始状态1位的地方与合法状态对应位且都等于合法状态位上的数字return 0;if(x&(x>>1)||x&(x<<1))//不能有相邻的两个1return 0;return 1;
}int main()
{cin>>n>>m;int x;memset(num,0,sizeof(num));memset(dp,0,sizeof(dp));for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){cin>>x;if(x)num[i]=num[i]|(1<<(j-1));//把每一行的状态保存到num[i]中去}int Max=(1<<m);dp[0][0]=1;//注意这一句啊!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[n][i];if(ans>=mod)ans%=mod;}cout<<ans;return 0;
}


 

这篇关于[ACM] POJ 3254 Corn Fields(状态压缩)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/653733

相关文章

Flutter监听当前页面可见与隐藏状态的代码详解

《Flutter监听当前页面可见与隐藏状态的代码详解》文章介绍了如何在Flutter中使用路由观察者来监听应用进入前台或后台状态以及页面的显示和隐藏,并通过代码示例讲解的非常详细,需要的朋友可以参考下... flutter 可以监听 app 进入前台还是后台状态,也可以监听当http://www.cppcn

MySQL 中的服务器配置和状态详解(MySQL Server Configuration and Status)

《MySQL中的服务器配置和状态详解(MySQLServerConfigurationandStatus)》MySQL服务器配置和状态设置包括服务器选项、系统变量和状态变量三个方面,可以通过... 目录mysql 之服务器配置和状态1 MySQL 架构和性能优化1.1 服务器配置和状态1.1.1 服务器选项

linux进程D状态的解决思路分享

《linux进程D状态的解决思路分享》在Linux系统中,进程在内核模式下等待I/O完成时会进入不间断睡眠状态(D状态),这种状态下,进程无法通过普通方式被杀死,本文通过实验模拟了这种状态,并分析了如... 目录1. 问题描述2. 问题分析3. 实验模拟3.1 使用losetup创建一个卷作为pv的磁盘3.

Python利用PIL进行图片压缩

《Python利用PIL进行图片压缩》有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所以本文为大家介绍了Python中图片压缩的方法,需要的可以参考下... 有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所有可以对文件中的图

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

通过prometheus监控Tomcat运行状态的操作流程

《通过prometheus监控Tomcat运行状态的操作流程》文章介绍了如何安装和配置Tomcat,并使用Prometheus和TomcatExporter来监控Tomcat的运行状态,文章详细讲解了... 目录Tomcat安装配置以及prometheus监控Tomcat一. 安装并配置tomcat1、安装

Linux之进程状态&&进程优先级详解

《Linux之进程状态&&进程优先级详解》文章介绍了操作系统中进程的状态,包括运行状态、阻塞状态和挂起状态,并详细解释了Linux下进程的具体状态及其管理,此外,文章还讨论了进程的优先级、查看和修改进... 目录一、操作系统的进程状态1.1运行状态1.2阻塞状态1.3挂起二、linux下具体的状态三、进程的

Qt实现文件的压缩和解压缩操作

《Qt实现文件的压缩和解压缩操作》这篇文章主要为大家详细介绍了如何使用Qt库中的QZipReader和QZipWriter实现文件的压缩和解压缩功能,文中的示例代码简洁易懂,需要的可以参考一下... 目录一、实现方式二、具体步骤1、在.pro文件中添加模块gui-private2、通过QObject方式创建

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。