{"id":1188,"date":"2024-09-21T09:00:00","date_gmt":"2024-09-21T01:00:00","guid":{"rendered":"https:\/\/seanxd.com\/?p=1188"},"modified":"2024-08-25T12:40:44","modified_gmt":"2024-08-25T04:40:44","slug":"zerojudge-d165","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/zerojudge-d165\/","title":{"rendered":"ZeroJudge D165: Pasture Census"},"content":{"rendered":"\n\n\n<p>Z\u5148\u751f\u70ba\u4e86\u5408\u7406\u653e\u7267\uff0c\u6c7a\u5b9a\u8acb\u4f60\u70ba\u5b83\u7684\u7267\u5834\u9032\u884c\u666e\u67e5\u3002\u7267\u5834\u53ef\u4ee5\u7528 N*M \u683c\u8868\u793a\u3002\u6bcf\u500b\u683c\u5b50\u4e0a\u6a19\u6709\u4e00\u500b\u975e\u8ca0\u6574\u6578\uff0c\u5176\u4e2d\u7528 0 \u8868\u793a\u7a7f\u884c\u5728\u7267\u5834\u4e2d\u7684\u9053\u8def\uff0c\u800c\u7528 1\uff5e255 \u7684\u6574\u6578\u8868\u793a\u9019\u500b\u683c\u5b50\u4e2d\u6240\u7a2e\u7267\u8349\u7684\u6578\u91cf\uff0c\u6578\u5b57\u8d8a\u5927\uff0c\u7267\u8349\u8d8a\u591a\u3002\u4e00\u500b\u7a2e\u6709\u7267\u8349\u7684\u683c\u5b50\u53ef\u4ee5\u901a\u904e\u300c\u4e0a\u3001\u4e0b\u3001\u5de6\u3001\u53f3\u300d\u56db\u500b\u65b9\u5411\u8207\u5176\u5b83\u7a2e\u6709\u7267\u8349\u7684\u683c\u5b50\u5f62\u6210\u4e00\u500b\u5927\u7684\u300c\u8349\u5834\u300d\uff0c\u800c\u4e00\u500b\u8349\u5834\u4e2d\u6240\u6709\u683c\u5b50\u6a19\u6709\u7684\u6574\u6578\u7684\u548c\u5c31\u662f\u9019\u500b\u8349\u5834\u7684\u7267\u8349\u7684\u860a\u85cf\u91cf\u3002<br><br>\u5982\uff1a<br>0 1 0 0<br>0 0 0 7<br>8 3 0 9<br><br>\u4e0a\u5716\u662f\u4e00\u500b 3*4 \u7684\u7267\u5834\u7bc4\u4f8b\uff0c\u5206\u6210\u4e09\u500b\u8349\u5834\uff0c\u5b83\u5011\u7684\u860a\u85cf\u91cf\u5206\u5225\u662f 1\u300111 \u548c 16\uff0c\u6700\u5927\u7684\u7267\u5834\u7267\u8349\u860a\u85cf\u91cf\u662f 16\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b\u6e2c\u8cc7<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7bc4\u4f8b\u8f38\u5165<\/th><th>\u7bc4\u4f8b\u8f38\u51fa<\/th><\/tr><\/thead><tbody><tr><td>EOF \u8f38\u5165\uff0c\u6bcf\u7d44\u6e2c\u8a66\u8cc7\u6599\u7684\u7b2c\u4e00\u884c\u6709\u5169\u500b\u6574\u6578 N \u548c M \uff0c1 &lt;= N\u3001M &lt;= 100\uff0c\u5169\u500b\u6574\u6578\u4e4b\u9593\u7528\u4e00\u500b\u7a7a\u683c\u9694\u958b\u3002\u4ee5\u4e0b N \u884c\uff0c\u6bcf\u884c\u6709 M \u500b 0 \u5230 255 \u7684\u6574\u6578\uff0c\u8868\u793a\u9019\u500b N*M \u7684\u7db2\u7d61\uff0c\u540c\u4e00\u884c\u4e2d\u76f8\u9130\u5169\u500b\u6578\u5b57\u4e4b\u9593\u7528\u4e00\u500b\u7a7a\u683c\u9694\u958b\u3002<\/td><td>\u5c0d\u65bc\u6bcf\u7d44\u6e2c\u8a66\u6578\u64da\uff0c\u8f38\u51fa\u5169\u884c\u3002\u7b2c\u4e00\u884c\u662f\u8349\u5834\u7684\u6578\u91cf\uff0c\u7b2c\u4e8c\u884c\u662f\u6700\u5927\u7684\u8349\u5834\u7684\u7267\u8349\u860a\u85cf\u91cf\u3002<\/td><\/tr><tr><td>3 4<br>0 1 0 0<br>0 0 0 7<br>8 3 0 9<\/td><td>3<br>16<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">ZeroJudge D165 \u7bc4\u4f8b\u6e2c\u8cc7<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u984c\u601d\u8def<\/h2>\n\n\n\n<p>\u4f7f\u7528 BFS \u7684\u65b9\u5f0f\u4f86\u78ba\u8a8d\u6bcf\u4e00\u584a\u5340\u57df\uff0c\u4e26\u4e14\u7d00\u9304\u5df2\u7d93\u8d70\u904e\u7684\u9ede\uff0c\u5df2\u7d93\u8d70\u904e\u7684\u8a71\u5c31\u6c92\u6709\u5fc5\u8981\u518d\u8a2d\u70ba\u65b0\u7684 BFS \u8d77\u9ede\u4e86\u3002BFS \u53ef\u4ee5\u56de\u50b3\u4e00\u500b\u6578\u5b57\uff0c\u7576\u6c92\u6709\u8d77\u9ede\u6642\u5c31\u56de\u50b3\u9019\u500b\u6578\u5b57\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=d165\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge D165: \u8349\u5834\u666e\u67e5<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nint N, M, farm[500][500] = {}, MAP[500][500] = {}, ans = 0, loc[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n\nvoid BFS(const vector&lt;pair&lt;int, int&gt;&gt;start, int count) {\n    if (start.size() == 0) {\n        ans = max(ans, count);\n        return;\n    }\n    vector&lt;pair&lt;int, int&gt;&gt;newStart;\n    for (int i = 0; i&lt;start.size(); i++) {\n        const int y = start[i].first, x = start[i].second;\n        if (MAP[y][x] != 0) continue;\n        MAP[y][x]++;\n        count += farm[y][x];\n        for (int j = 0; j&lt;4; j++) {\n            const int yy = y + loc[j][0], xx = x + loc[j][1];\n            if (yy &gt;= 0 && yy &lt; N && xx &gt;= 0 && xx &lt; M && MAP[yy][xx] == 0 && farm[yy][xx] != 0) {\n                newStart.push_back(make_pair(yy, xx));\n            }\n        }\n    }\n    BFS(newStart, count);\n}\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    while (cin &gt;&gt; N &gt;&gt; M) {\n        ans = 0;\n        for (int i = 0; i&lt;N; i++) {\n            for (int j = 0; j&lt;M; j++) {\n                cin &gt;&gt; farm[i][j];\n                MAP[i][j] = 0;\n            }\n        }\n        int land = 0;\n        for (int i = 0; i&lt;N; i++) {\n            for (int j = 0; j&lt;M; j++) {\n                if (farm[i][j] != 0 && MAP[i][j] == 0) {\n                    land++;\n                    vector&lt;pair&lt;int, int&gt;&gt;start;\n                    start.push_back(make_pair(i, j));\n                    BFS(start, 0);\n                }\n            }\n        }\n        cout &lt;&lt; land &lt;&lt; &quot;\\n&quot; &lt;&lt; ans &lt;&lt; &quot;\\n&quot;;\n    }\n}\n\n\/\/ZeroJudge D165\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Z\u5148\u751f\u70ba\u4e86\u5408\u7406\u653e\u7267\uff0c\u6c7a\u5b9a\u8acb\u4f60\u70ba\u5b83\u7684\u7267\u5834\u9032\u884c\u666e\u67e5\u3002\u7267\u5834\u53ef\u4ee5\u7528 N*M \u683c\u8868\u793a\u3002\u6bcf\u500b\u683c\u5b50\u4e0a\u6a19\u6709\u4e00\u500b\u975e\u8ca0\u6574\u6578\uff0c\u5176\u4e2d\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[25],"tags":[30,23,20,22,8,9],"class_list":["post-1188","post","type-post","status-publish","format-standard","hentry","category-ioi-apcs","tag-bfs","tag-pair","tag-20","tag-22","tag-8","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/comments?post=1188"}],"version-history":[{"count":2,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1188\/revisions"}],"predecessor-version":[{"id":1190,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1188\/revisions\/1190"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=1188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=1188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=1188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}