HDOJ 4925 Apple Tree

題目:

Apple Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 46    Accepted Submission(s): 29


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
 
傳送門:點擊打開鏈接

解題思路:

策略:對於第一行,在第一個格子裏種樹,在後面的格子施肥,就這樣間隔着種樹施肥,以後的每行只要與前一行對應位置上的相反即可。以這樣的策略來種樹施肥,最後收穫的果實最多。

代碼:

#include <cstdio>
#include <cstring>

const int MAXN = 105;
int a[MAXN][MAXN];
int n, m, t;
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

long long fun(int num)
{
      long long ret = 1;
        for(int i = 0; i < num; ++i) ret *= 2;
    return ret;
}

int main()
{
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &n, &m);
        a[0][0] = 1;
        for(int i = 1; i < m; ++i)
        {
            a[0][i] = !a[0][i-1];
        }
        for(int i = 1; i < n; ++i)
        {
            for(int j = 0; j < m; ++j)
                a[i][j] = !a[i-1][j];
        }
        for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j < m; ++j)
            {
                if(0 == a[i][j])
                {
                    for(int k = 0; k < 4; ++k)
                    {
                        int x = i + dir[k][0];
                        int y = j + dir[k][1];
                        if(x>=0 && x<n && y>=0 && y<m)
                        {
                            if(a[x][y]) a[x][y]++;
                        }
                    }
                }
            }
        }
        long long ans = 0;
        for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j < m; ++j)
            {
                if(a[i][j]) ans += fun(a[i][j]-1);
            }
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


發佈了187 篇原創文章 · 獲贊 14 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章