poj 3254 Corn Fields(DP-状态DP)
来源:互联网 发布:在线网络视频下载工具 编辑:程序博客网 时间:2024/04/29 06:51
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 31 1 10 1 0
Sample Output
9
从上往下,从左往右遍历,会发现,当前点只跟上一行和左边这个点有关系,所以我们记录的状态就是dp[i][j][sta]当前点i,j以及上一行和左边点的状态sta < (1<<13)。
#include <iostream>#include <cstdio>using namespace std;const int maxn = 1<<13 + 1;const int mod = 100000000;int dp[13][13][maxn] , N , M , mp[13][13];void initial(){ for(int i = 0; i < 13; i++){ for(int j = 0; j < 13; j++){ mp[i][j] = 0; for(int k = 0; k < maxn; k++) dp[i][j][k] = -1; } }}int DP(int i , int j , int k){ if(i >= N) return 1; if(dp[i][j][k] != -1) return dp[i][j][k]; int ans = 0; if(mp[i][j]==0 || (k&(1<<j)) > 0 || (k&(1<<M)) > 0){ int sta = (k&((1<<M)-1)); if((sta&(1<<j)) > 0) sta = (sta-(1<<j)); if(j+1>=M) return dp[i][j][k] = DP(i+1 , 0 , sta)%mod; return dp[i][j][k] = DP(i , j+1 , sta)%mod; }else{ int sta = (k|(1<<j)); if(j+1 >= M){ sta = (sta&((1<<M)-1)); ans = (ans+DP(i+1 , 0 , sta))%mod; }else{ sta = (sta|(1<<M)); ans = (ans+DP(i , j+1 , sta))%mod; } sta = (sta&((1<<M)-1)); if((sta&(1<<j)) > 0) sta = sta-(1<<j); if(j+1>=M) ans = (ans+DP(i+1 , 0 , sta))%mod; else ans = (ans+DP(i , j+1 , sta))%mod; return dp[i][j][k] = ans%mod; }}void readcase(){ for(int i = 0; i < N; i++){ for(int j = 0; j < M; j++) scanf("%d" , &mp[i][j]); }}void computing(){ printf("%d\n" , DP(0 , 0 , 0));}int main(){ while(~scanf("%d%d" , &N , &M)){ initial(); readcase(); computing(); } return 0;}
- poj 3254 Corn Fields(DP-状态DP)
- poj 3254 Corn Fields //状态压缩DP
- POJ 3254 Corn Fields 状态压缩DP
- POJ --3254--Corn Fields--状态DP
- poj 3254 Corn Fields(状态压缩dp)
- POJ 3254 Corn Fields (状态压缩DP)
- poj-3254-Corn Fields-状态压缩DP
- poj 3254 Corn Fields (状态压缩DP)
- POJ 3254 Corn Fields (状态压缩DP)
- POJ 3254 Corn Fields(状态压缩dp)
- POJ 3254 Corn Fields 状态压缩dp
- POJ 3254 Corn Fields 状态压缩DP
- poj 3254 Corn Fields ,状态压缩DP
- poj Corn Fields 3254 状态压缩dp
- poj 3254 Corn Fields (状态压缩DP)
- poj 3254 Corn Fields 状态压缩dp
- poj 3254 Corn Fields 状态压缩DP
- poj 3254 Corn Fields (状态压缩dp)
- 配置管理工具使用(资料索引)
- 收藏的一系列教程帖子,很适合有一定基础,想要进阶的同学
- LeetCode - Array/Binary Search - Search Insert Position
- 代码中获取root权限并随意修改根目录文件
- IOS面试题---005
- poj 3254 Corn Fields(DP-状态DP)
- 完全背包问题
- 关于JS跨域请求的若干方式
- bitset的使用
- 【Android學習專題】視覺介面篇:Android布局及属性归总(查询用)《转自cnblog》
- eys4fhf韩国
- Android修炼之道——设置状态栏颜色
- HDU 4940 Destroy Transportation system (瞎搞)
- MKNetworkKit 详细介绍