Hdu4925apple Tree (Finding the law)

Source: Internet
Author: User

Title: Hdu4925apple Tree (Law-seeking)


The main idea: give the matrix of N M, and then each lattice or apple, or fertilization: After (x, y) fertilization, its upper and lower apple tree yield will double. The number of apples was planted and the yield was 1. The maximum number of apples that can be obtained by such a n * M matrix.


Problem Solving Ideas:

For the 2 * 2 matrix: X for fertilization, and the number for Tree x 2 is the best. The respective numbers represent the lattice being fertilized several times. Then 2 * 2 of the maximum number of apples 2 ^2 + 2^2 = 8;

2 X

3 * 3:x 3 X

3 X 3

X 3 x 4 * 2^3 = 32;

Find the law: The first line is the first to get fertilizer, every time from the first row of the 2nd start to find, it up and down on each side of the fertilization, so that it can maximize the impact of its output, yield 2^n (up and down the number of squares), and fertilization where to mark the next time you can not plant trees here. Look back and get the total output.


Code:

#include <cstdio> #include <cstring>const int N = 105;const int M = 4;int visit[n][n];const int dir[m][2] = {{0  ,-1}, {-1, 0}, {0, 1}, {1, 0}};int main () {int t;int n, m;int ans, temp;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n, &m); memset (visit, 0, sizeof (visit)); Visit[0][0] = 1;ans = 0;for (int i = 0; i < n; i++) for (int j = 0; j < m;  J + +) {if (visit[i][j]) continue;temp = 0;for (int k = 0; k < M; k++) {if (i + dir[k][0] >= 0 && i + dir[k][0] < n && j + dir[k][1] >= 0 && j + dir[k][1] < m) {temp++;visit[i + dir[k][0]][j + dir[k][1]] = 1; }}ans + = 1<<temp;} if (!ans) printf ("1\n"); elseprintf ("%d\n", ans);} return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.