hdu 4925 黑白格

http://acm.hdu.edu.cn/showproblem.php?pid=4925

给定一个N*M的网格,对于每个格子可以选择种树和施肥,默认一个苹果树收获1个苹果,在一个位置施肥的话,周围四个格子如果有种树,那么产量加倍。为最大产量。

黑白格方法最优,模拟即可

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;

int n,m;
int f[] = {1,2,4,8,16};
bool a[200][200];

void work() {
    int ans = 0;
    for (int i = 0; i <= n + 1; i++)
        for (int j = 0; j <= m + 1; j++)
            a[i][j] = false;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if ((i + j) % 2 == 1)
                a[i][j] = true;

    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
        if (a[i][j] == true)
            continue;
        int cnt = 0;
        if (a[i - 1][j])
            cnt++;
        if (a[i + 1][j])
            cnt++;
        if (a[i][j - 1])
            cnt++;
        if (a[i][j + 1])
            cnt++;
        ans += f[cnt];
    }
    printf("%d\n",ans);
    return;
}

int main(){
    int _;
    RD(_);
    while(_--){
        RD2(n,m);
        work();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值