CodeForces - 111C Petya and Spiders

来源:互联网 发布:数据自动统计软件 编辑:程序博客网 时间:2024/06/02 03:26

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 andm (1 ≤ n, m ≤ 40, n·m ≤ 40) — the board sizes.

Output

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

Example
Input
1 1
Output
0
Input
2 3
Output
4
Note

In the first sample the only possible answer is:

s

In the second sample one of the possible solutions is:

rdlrul

s denotes command "stay idle", l, r, d, u denote commands "crawl left", "crawl right", "crawl down", "crawl up", correspondingly

题意:给你一个N*M的格子 

 每个格子上有一个蜘蛛

每个蜘蛛可以进行5个动作

1 不动 2 向左 3 向右 4 向上 5 向下

n*m的矩形  ==  m*n的矩形  减小状态数目

逆向思维  放入k个蜘蛛 把所有点占据满

蜘蛛最多能影响上一行的状态

用二进制模拟

ACcode:

#include<bits/stdc++.h>using namespace std;const int maxn = 1<< 7;int dp[50][maxn][maxn],res[maxn],n,m,mask;bool check(int a,int b,int c)  //保证当前的格子被占满  当前的格子会作为下一行的上一个状态{    int x=b|(b<<1)|(b>>1)|a|c;    if( (x&mask) == mask) return 1;    return 0;}int num(int x)   //记录蜘蛛个数{    int cnt = 0;    while(x>0){        x-=(x&(-x));        cnt++;    }    return m-cnt;}int main(){    int ans=0;    scanf("%d%d",&n,&m);    if(n<m)  swap(n,m);   //优化进制    mask=(1<<m)-1;    memset(dp,0,sizeof dp);    for(int i=0;i<=mask;i++)  res[i]=num(i);    for(int k=0;k<=mask;k++){//预处理第一行        for(int l=0;l<=mask;l++){            if(check(0,k,l) == 1)                dp[1][k][l]=max(dp[1][k][l],dp[0][0][k]+res[k]);              }    }    for(int i=1;i<n;i++){       for(int j=0;j<=mask;j++){         for(int k=0;k<=mask;k++){             if(dp[i][j][k] != 0 ){                for(int l=0;l<=mask;l++){                  if(check(j,k,l) == 1)                    dp[i+1][k][l]=max(dp[i+1][k][l],dp[i][j][k]+res[k]);                  }            }         }       }    }    for(int j=0;j<=mask;j++)        ans=max(ans,dp[n][j][0]);      printf("%d\n",ans);      return 0;}

0 0
原创粉丝点击