HDU4925-Apple Tree

题意:n*m网格中种苹果,每个网格要么施肥,要么种一个苹果,每个种苹果的格子,如果它的上下左右有各自有施肥的话,每有一个,苹果数量*2,求怎么种使得苹果数量最多。

思路:交叉种植,即黑白染色法可得到最优解。注意特判当n=m=1时的情况。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 105;

int n, m;
int g[MAXN][MAXN];

int deal(int x, int y) {
    int sum = 1;
    if (g[x][y - 1] == 1)
        sum *= 2;
    if (g[x][y + 1] == 1)
        sum *= 2;
    if (g[x - 1][y] == 1)
        sum *= 2;
    if (g[x + 1][y] == 1)
        sum *= 2;
    return sum;
}

int main() {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d%d", &n, &m);
        if (n == 1 && m == 1) {
            printf("1\n");
            continue;
        }
        memset(g, 0, sizeof(g));
        int flag = 1;
        if (m % 2 == 1) {
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= m; j++) {
                    g[i][j] = flag;
                    flag = -flag;
                }
        }
        else {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    g[i][j] = flag;
                    flag = -flag;
                }
                flag = -flag;
            }
        }

        long long ans = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                if (g[i][j] == -1)
                    ans += deal(i, j); 

        printf("%lld\n", ans);
    }
    return 0;
}

HDU4925-Apple Tree,布布扣,bubuko.com

时间: 2024-10-10 02:45:25

HDU4925-Apple Tree的相关文章

HDU4925:Apple Tree

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 neighbor

POJ3321 Apple Tree

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26989   Accepted: 8004 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been

timus 1018. Binary Apple Tree

1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enu

poj 2486 a apple tree

题意:n节点的树,从1开始走,总共v步,每个点都有一个价值,求可以获得的最大价值 分析:这个显然可以走回来,那么就加一维表示是否走回祖先 dp[u][i][j]表示从u为根节点的子树,往下走i步,j=0表示不走回来,j=1表示走回来 那么可以得到状态转移方程,不走回来的可能会影响走回来的,如果先算不会来的,那么一个节点在不走回来算一次 在走回来又算一次,所以先算走回来的就可以避免 dp[u][i][0]=max(dp[u][i][0],dp[u][i-j][1]+dp[v][j-1][0]);

cf202-div 1-B - Apple Tree:搜索,数论,树的遍历

http://codeforces.com/contest/348/problem/B B. Apple Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a rooted tree with n vertices. In each leaf vertex there's a single

ural 1018 Binary Apple Tree

1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enu

2014 Multi-University Training Contest 6 Apple Tree(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 Apple Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 176    Accepted Submission(s): 120 Problem Description I've bought an orchard an

HDU 4925 Apple Tree(推理)

HDU 4925 Apple Tree 题目链接 题意:给一个m*n矩阵种树,每个位置可以选择种树或者施肥,如果种上去的位置就不能施肥,如果施肥则能让周围果树产量乘2,问最大收益 思路:推理得到肯定是果树和肥料交叉种好,类似国际象棋棋盘,黑的种,白的施肥,由于格子数不多,直接去枚举每个位置即可.如果题目格子数多的话,其实也可以推出公式一步得到答案 代码: #include <cstdio> #include <cstring> const int d[4][2] = {{0, 1}

HDU 4925 Apple Tree (瞎搞)

找到规律,各一个种一棵树,或者施肥.先施肥,先种树一样. Apple Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 197    Accepted Submission(s): 135 Problem Description I've bought an orchard and decide to plant some

hdu 4925 Apple Tree

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 思路:直接计算坐标和,如果为奇数就种树,但要注意行或列为1的情况. 写啦两种代码:一种直接判断计算的,另一种优化计算的 code1: #include<cstdio> #include<iostream> #include<algorithm> #include<cmath> using namespace std; int main() { int T;