题目:
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.
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;
}