标签:tor 字符 choice get namespace tab 地形 不能 poj
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16773 | Accepted: 8860 |
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
Output
Sample Input
2 3 1 1 1 0 1 0
Sample Output
9
Hint
1 2 3
4
Source
#include <cstdio> #include <cstring> #define inf 0x3f3f3f3f #define met(a,b) memset(a,b,sizeof a) #define pb push_back #define mp make_pair #define rep(i,l,r) for(int i=(l);i<=(r);++i) #define inf 0x3f3f3f3f using namespace std; typedef long long ll; const int N = 13;; const int M = 1e5; const int mod = 19260817; const int mo=123; //const double pi= acos(-1.0); //typedef pair<int,int>pii; int n,m,cas; int sta[N],a[M],dp[N][M]; int main(){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ for(int j=0,x;j<m;j++){ scanf("%d",&x); if(!x)sta[i]+=(1<<j); } } int cnt=0; for(int i=0;i<(1<<m);i++){ if(i&(i<<1))continue; a[cnt++]=i; if(i&sta[0])continue; dp[0][cnt-1]++; } for(int i=1;i<n;i++){ for(int j=0;j<cnt;j++){ if(a[j]&sta[i])continue; for(int k=0;k<cnt;k++){ if(a[j]&a[k]||a[k]&sta[i-1])continue; dp[i][j]+=dp[i-1][k]; } } } int ans=0; for(int i=0;i<cnt;i++){ ans+=dp[n-1][i]; ans%=1000000000; } printf("%d\n",ans); return 0; }
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 29369 | Accepted: 11377 |
Description
Input
Output
Sample Input
5 4 PHPP PPHH PPPP PHPP PHHP
Sample Output
6
Source
#include <cstdio> #include <map> #include <algorithm> #include <vector> #include <iostream> #include <set> #include <queue> #include <string> #include <cstdlib> #include <cstring> #include <cmath> #define inf 0x3f3f3f3f #define met(a,b) memset(a,b,sizeof a) #define pb push_back #define mp make_pair #define rep(i,l,r) for(int i=(l);i<=(r);++i) #define inf 0x3f3f3f3f using namespace std; typedef long long ll; const int N = 13;; const int M = 165; const int mod = 19260817; const int mo=123; const double pi= acos(-1.0); typedef pair<int,int>pii; int n,m,cas; char str[15]; int sta[105],a[M],dp[105][M][M]; int sum[M]; int getsum(int x){ int ret=0; for(int i=0;i<m;i++){ if(x&(1<<i))ret++; } return ret; } int main(){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ scanf("%s",str); for(int j=0;j<m;j++){ if(str[j]==‘H‘)sta[i]+=(1<<j); } } int cnt=0; for(int i=0;i<(1<<m);i++){ if(i&(i<<1)||i&(i<<2))continue; a[cnt++]=i; if(i&sta[0])continue; dp[0][cnt-1][0]=getsum(i); } for(int i=0;i<cnt;i++){ sum[i]=getsum(a[i]); if(a[i]&sta[1])continue; int res=0; for(int j=0;j<cnt;j++){ if(a[i]&a[j])continue; dp[1][i][j]= dp[0][j][0] + getsum(a[i]); } } for(int i=2;i<n;i++){ for(int j=0;j<cnt;j++){ if(a[j]&sta[i])continue; for(int k=0;k<cnt;k++){ if(a[j]&a[k]||a[k]&sta[i-1])continue; for(int l=0;l<cnt;l++){ if(a[j]&a[l]||a[l]&sta[i-2]||a[k]&a[l])continue; dp[i][j][k]=max(dp[i-1][k][l]+sum[j],dp[i][j][k]); } } } } int ans=0; for(int i=0;i<cnt;i++){ for(int j=0;j<cnt;j++){ ans=max(ans,dp[n-1][i][j]); } } printf("%d\n",ans); return 0; }
标签:tor 字符 choice get namespace tab 地形 不能 poj
原文地址:http://www.cnblogs.com/jianrenfang/p/7616785.html