hdu4925Apple Tree【水】2014多校联合


Problem Description
I’ve bought an orchard and decide to plant some apple trees on it. The orchard seems like an N * M two-dimensional map. In each grid, I can either plant an apple tree to get one apple or fertilize the soil to speed up its neighbors’ production. When a grid is fertilized, the grid itself doesn’t produce apples but the number of apples of its four neighbor trees will double (if it exists). For example, an apple tree locates on (x, y), and (x - 1, y), (x, y - 1) are fertilized while (x + 1, y), (x, y + 1) are not, then I can get four apples from (x, y). Now, I am wondering how many apples I can get at most in the whole orchard?
 

Input
The input contains multiple test cases. The number of test cases T (T<=100) occurs in the first line of input.
For each test case, two integers N, M (1<=N, M<=100) are given in a line, which denote the size of the map.
 

Output
For each test case, you should output the maximum number of apples I can obtain.
 

Sample Input
2 2 2 3 3
 

Sample Output
8 32
 

Author
BUPT
 

Source
 

Recommend

前言:我要是知道这个是水题我就不做了QAQ,有一个总结篇把它放到了最小点覆盖里面了

题意:给出n*m规格的网格,每个网格可以种一个苹果树,或者不种树只施肥,施肥是使它周围的四个点的产量翻番

做法:很容易想到是黑白染色的情况最合适

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int num[200][200],n,m;
int cal(int x,int y)
{
    int ans=1;
    if(num[x][y-1]==0)ans*=2;
    if(num[x][y+1]==0)ans*=2;
    if(num[x+1][y]==0)ans*=2;
    if(num[x-1][y]==0)ans*=2;
    return ans;
}
int main()
{
   // freopen("cin.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        int num1=0,num2=0;

        for(int i=1;i<=n;i++)
        {
            bool flag=i&1;
            for(int j=1;j<=m;j++)
            {
                if(flag)
                {
                    num1++;
                    num[i][j]=1;
                }
                else
                {
                    num2++;
                    num[i][j]=0;
                }
                flag=flag^1;
            }
        }
        if(num2>num1)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    num[i][j]=1-num[i][j];
                }
            }
        }
       // for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)printf("%d ",num[i][j]);
        for(int i=0;i<=m+1;i++)num[0][i]=1;
        for(int i=1;i<=n+1;i++)num[i][0]=1;
        for(int i=1;i<=m+1;i++)num[n+1][i]=1;
        for(int i=1;i<=n+1;i++)num[i][m+1]=1;
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
               if(num[i][j])
                ans+=cal(i,j);
              //  printf("i=%d,j=%d,cal=%d\n",i,j,cal(i,j));
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}



全部评论

相关推荐

07-18 14:34
门头沟学院 Java
感觉招聘要求好高,这都是招什么人才
电气电子小朋友:你都说了这是大疆,无人机顶级企业,薪资30k-50k,要求高是正常的
点赞 评论 收藏
分享
07-16 17:55
门头沟学院 Java
点赞 评论 收藏
分享
06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务