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
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.
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 = ;;
const int M = 1e5;
const int mod = ;
const int mo=;
//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=;i<n;i++){
for(int j=,x;j<m;j++){
scanf("%d",&x);
if(!x)sta[i]+=(<<j);
}
}
int cnt=;
for(int i=;i<(<<m);i++){
if(i&(i<<))continue;
a[cnt++]=i;
if(i&sta[])continue;
dp[][cnt-]++;
}
for(int i=;i<n;i++){
for(int j=;j<cnt;j++){
if(a[j]&sta[i])continue;
for(int k=;k<cnt;k++){
if(a[j]&a[k]||a[k]&sta[i-])continue;
dp[i][j]+=dp[i-][k];
}
}
}
int ans=;
for(int i=;i<cnt;i++){
ans+=dp[n-][i];
ans%=;
}
printf("%d\n",ans);
return ;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 29369 | Accepted: 11377 |
Description
如果在地图中的灰色所标识的平原上部署一支炮兵部队,则图中的黑色的网格表示它能够攻击到的区域:沿横向左右各两格,沿纵向上下各两格。图上其它白色网格均攻击不到。从图上可见炮兵的攻击范围不受地形的影响。
现在,将军们规划如何部署炮兵部队,在防止误伤的前提下(保证任何两支炮兵部队之间不能互相攻击,即任何一支炮兵部队都不在其他支炮兵部队的攻击范围内),在整个地图区域内最多能够摆放多少我军的炮兵部队。
Input
接下来的N行,每一行含有连续的M个字符('P'或者'H'),中间没有空格。按顺序表示地图中每一行的数据。N <= 100;M <= 10。
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 = ;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,m,cas;
char str[];
int sta[],a[M],dp[][M][M];
int sum[M];
int getsum(int x){
int ret=;
for(int i=;i<m;i++){
if(x&(<<i))ret++;
}
return ret;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%s",str);
for(int j=;j<m;j++){
if(str[j]=='H')sta[i]+=(<<j);
}
}
int cnt=;
for(int i=;i<(<<m);i++){
if(i&(i<<)||i&(i<<))continue;
a[cnt++]=i;
if(i&sta[])continue;
dp[][cnt-][]=getsum(i);
}
for(int i=;i<cnt;i++){
sum[i]=getsum(a[i]);
if(a[i]&sta[])continue;
int res=;
for(int j=;j<cnt;j++){
if(a[i]&a[j])continue;
dp[][i][j]= dp[][j][] + getsum(a[i]);
}
}
for(int i=;i<n;i++){
for(int j=;j<cnt;j++){
if(a[j]&sta[i])continue;
for(int k=;k<cnt;k++){
if(a[j]&a[k]||a[k]&sta[i-])continue;
for(int l=;l<cnt;l++){
if(a[j]&a[l]||a[l]&sta[i-]||a[k]&a[l])continue;
dp[i][j][k]=max(dp[i-][k][l]+sum[j],dp[i][j][k]);
}
}
} }
int ans=;
for(int i=;i<cnt;i++){
for(int j=;j<cnt;j++){
ans=max(ans,dp[n-][i][j]);
}
}
printf("%d\n",ans);
return ;
}