UVa 10360 Rat Attack:枚舉和最佳化

來源:互聯網
上載者:User

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1301

由于格子數遠大於鼠窩,所以以鼠窩為出發點,增加鼠窩周圍d範圍內的“格子的值”,最後掃描每個格子輸出最大格子值即可。

完整代碼:

01./*0.125s*/02.  03.#include<bits/stdc++.h>  04.using namespace std;  05.const int MAXN = 1030;  06.  07.int cnt[MAXN][MAXN];  08.  09.int main()  10.{  11.    int T, d, n, i, j, p, q;  12.    int x, y, num, lmax;  13.    int x_min, x_max, y_min, y_max;  14.    scanf("%d", &T);  15.    while (T--)  16.    {  17.        scanf("%d%d", &d, &n);  18.        memset(cnt, 0, sizeof(cnt));  19.        for (i = 0; i < n; ++i)  20.        {  21.            scanf("%d%d%d", &x, &y, &num);  22.            x_min = max(0, x - d), x_max = min(1024, x + d);  23.            y_min = max(0, y - d), y_max = min(1024, y + d);  24.            for (p = x_min; p <= x_max; ++p)  25.                for (q = y_min; q <= y_max; ++q)  26.                    cnt[p][q] += num;  27.        }  28.        lmax = x = y = 0;  29.        for (i = 0; i < 1025; ++i)  30.            for (j = 0; j < 1025; ++j)  31.                if (cnt[i][j] > lmax)  32.                    x = i, y = j, lmax = cnt[i][j];  33.        printf("%d %d %d\n", x, y, lmax);  34.    }  35.    return 0;  36.}

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.