HDU 4925 Apple Tree(推理)

來源:互聯網
上載者:User

標籤:style   http   color   os   io   for   ar   代碼   

HDU 4925 Apple Tree

題目連結

題意:給一個m*n矩陣種樹,每個位置可以選擇種樹或者施肥,如果種上去的位置就不能施肥,如果施肥則能讓周圍果樹產量乘2,問最大收益

思路:推理得到肯定是果樹和肥料交叉種好,類似國際象棋棋盤,黑的種,白的施肥,由於格子數不多,直接去枚舉每個位置即可。如果題目格子數多的話,其實也可以推出公式一步得到答案

代碼:

#include <cstdio>#include <cstring>const int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};int t, n, m;int main() {    scanf("%d", &t);    while (t--) {scanf("%d%d", &n, &m);int flag = 1;int ans = 0;for (int i = 0; i < n; i++) {    for (int j = 0; j < m; j++) {if (flag) {    int cnt = 1;    for (int k = 0; k < 4; k++) {int xx = i + d[k][0];int yy = j + d[k][1];if (xx < 0 || xx >= n || yy < 0 || yy >= m) continue;cnt *= 2;    }    ans += cnt;}flag ^= 1;    }}printf("%d\n", ans);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.