Test Instructions:n*m in the grid of apples, each grid either fertilization, or grow an apple, each kind of apple lattice, if it's up and down there are each fertilization, each one, the number of apples, how to grow the number of apples.
idea: The best solution can be obtained by cross planting, that is, black and white dyeing. Pay attention to the situation when the n=m=1 is in a special sentence.
#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; }} A 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;}