【codeforces 111C】 Petya and Spiders

来源:互联网 发布:表格删除重复数据 编辑:程序博客网 时间:2024/05/19 19:43

H - Petya and Spiders
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status
Description
Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second.

Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 40, n·m ≤ 40) — the board sizes.

Output
In the first line print the maximum number of cells without spiders.

Sample Input
Input
1 1
Output
0
Input
2 3
Output
4
Hint
In the first sample the only possible answer is:

s

In the second sample one of the possible solutions is:

rdl
rul
s denotes command “stay idle”, l, r, d, u denote commands “crawl left”, “crawl right”, “crawl down”, “crawl up”, correspondingly.

#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int n,m;int ans;int full;int dp[45][(1<<6)][(1<<6)];int get[(1<<6)];bool check(int a,int b,int c)      //     sta is legal is(↑,↓,←,→,中的合法情况){    int sta=a|b|c|(b<<1)|(b>>1);    return ((sta&(full))==(full));}void work(){    for(int i=0;i<n;i++)    for(int j=0;j<=full;j++)    for(int k=0;k<=full;k++)     for(int l=0;l<=full;l++)     if(check(j,k,l))    dp[i+1][k][l]=max(dp[i+1][k][l],dp[i][j][k]+get[k]);    for(int i=0;i<=full;i++)    ans=max(ans,dp[n][i][0]);}int main(){       scanf("%d%d",&n,&m);        if(n<m) swap(n,m);     //maintain the chracter n<=6    full=(1<<m)-1;    for(int i=0;i<n+1;i++)    for(int j=0;j<=full;j++)    for(int k=0;k<=full;k++)    dp[i][j][k]=-99999;    for(int i=0;i<=full;i++) dp[0][0][i]=0,get[i]=m-__builtin_popcount(i);      work();    printf("%d",ans);}
1 0