{"id":501,"date":"2024-05-23T09:00:00","date_gmt":"2024-05-23T01:00:00","guid":{"rendered":"https:\/\/seanxd.com\/?p=501"},"modified":"2024-05-04T20:52:27","modified_gmt":"2024-05-04T12:52:27","slug":"zerojudge-d406","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/zerojudge-d406\/","title":{"rendered":"ZeroJudge D406: Flowing Water"},"content":{"rendered":"\n\n\n<p class=\"\">\u6c34\u7531\u4e0a\u800c\u4e0b\u7684\u6d41\uff0c\u73fe\u5728\u7d66\u4f60\u6c34\u7ba1\u4e4b\u9593\u7684\u5730\u5716<br>\u6c34\u53ef\u4ee5\u7531\u4e0a\u800c\u4e0b\uff0c\u53ef\u4ee5\u5f80\u53f3\u6d41\uff0c\u5f80\u5de6\u6d41\u3002<\/p>\n\n\n\n<p class=\"\">\u53ef\u662f\u5462 \u2026 \u6709\u6642\u5019\u4e5f\u53ef\u4ee5\u5f80\u4e0a\u6d41<br>\u73fe\u5728\u5f9e\u5730\u5716\u7684\u6700\u4e0a\u65b9\u958b\u59cb\u5012\u6c34\uff0c\u8acb\u8f38\u51fa\u5230\u7684\u6642\u9593\u3002<\/p>\n\n\n\n<p class=\"\">\u203b \u958b\u59cb\u5012\u7684\u5730\u65b9\u53ea\u6709 1 \u500b\u4e14\u53ea\u5728\u7b2c\u4e00\u5217\u5012<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b\u6e2c\u8cc7<\/h2>\n\n\n\n<figure class=\"wp-block-table nfd-wb-animate nfd-wb-fade-in-bottom-short\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7bc4\u4f8b\u8f38\u5165<\/th><th>\u7bc4\u4f8b\u8f38\u51fa<\/th><\/tr><\/thead><tbody><tr><td><strong>EOF<\/strong> \u8f38\u5165\uff0c\u6bcf\u7d44\u6e2c\u8cc7\u7684\u7b2c\u4e00\u5217\u6709\u4e00\u500b\u6578\u5b57 S\uff0c<strong>\u82e5 S = 2 \u4ee3\u8868\u6c34\u4e0d\u80fd\u5f80\u4e0a\u6d41\uff0c S = 1 \u4ee3\u8868\u6c34\u53ef\u4ee5\u5f80\u4e0a\u6d41<\/strong>\u3002<br>\u7b2c\u4e8c\u5217\u6709\u5169\u500b\u6578\u5b57 N \u548c M\uff0c N \u4ee3\u8868\u63a5\u4e0b\u4f86\u6709 N \u5217\uff0cM\u4ee3\u8868\u6bcf\u5217\u4e0a\u6709\u591a\u5c11\u6578\u5b57\u3002<br>( 1\u2266 N\u3001M \u2266 100 )<br>\u63a5\u4e0b\u4f86\u6703\u6709 N \u5217\uff0c\u6bcf\u5217\u4e0a\u6709 M \u500b\u6578\u5b57\uff0c 1 \u4ee3\u8868\u6709\u6c34\u7ba1\uff0c 0 \u5247\u4ee3\u8868\u6c92\u6709\u3002<\/td><td>\u5c0d\u6bcf\u500b\u5730\u9ede\u8f38\u51fa\u5230\u7684\u6642\u9593\u3002<\/td><\/tr><tr><td>1<br>4 6<br>0 0 0 1 0 0<br>0 0 1 1 1 0<br>1 1 1 0 1 0<br>0 1 0 0 0 0<br>1<br>5 7<br>1 0 0 0 0 0 0<br>1 1 1 1 1 1 0<br>0 1 0 0 1 0 1<br>1 1 1 0 1 1 1<br>1 0 1 0 1 1 1<br>2<br>6 6<br>0 1 0 0 0 0<br>1 1 0 0 1 1<br>0 1 1 1 1 0<br>1 1 1 1 1 1<br>0 0 0 0 1 0<br>0 0 0 1 1 0<\/td><td>Case 1:<br>0 0 0 1 0 0<br>0 0 3 2 3 0<br>6 5 4 0 4 0<br>0 6 0 0 0 0<br>Case 2:<br>1 0 0 0 0 0 0<br>2 3 4 5 6 7 0<br>0 4 0 0 7 0 11<br>6 5 6 0 8 9 10<br>7 0 7 0 9 10 11<br>Case 3:<br>0 1 0 0 0 0<br>3 2 0 0 0 0<br>0 3 4 5 6 0<br>5 4 5 6 7 8<br>0 0 0 0 8 0<br>0 0 0 10 9 0<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">ZeroJudge D406 \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 class=\"\">\u5ba3\u544a\u4e00\u500b <strong>Vector&lt;Vector&lt;int>>ans \u4e26\u4e14\u5c07\u6240\u6709\u6578\u503c\u9810\u8a2d\u70ba 0<\/strong>\uff0c\u4f7f\u7528 BFS\uff0c\u56de\u50b3\u7684\u8cc7\u6599\u53ef\u4ee5\u65b0\u589e\u4e00\u500b Count \u7684\u8b8a\u6578\uff0c\u4ee3\u8868\u6642\u9593\uff0c\u6bcf\u6b21\u8dd1\u5230\u67d0\u4e00\u500b\u9ede\u7684\u6642\u5019\u5c31<strong>\u5c07\u76ee\u524d\u7684 Count \u8a2d\u5b9a\u5728\u76ee\u524d\u8d70\u9053\u7684\u9ede\u7684ans\u4e2d<\/strong>\u3002\u6bcf\u4e00\u6b21\u91cd\u65b0\u547c\u53eb BFS \u6642<strong> Count \u90fd\u8981 +1<\/strong>\u3002<\/p>\n\n\n\n<p class=\"\"><strong>\u8d70\u904e\u7684\u9ede\u5c31\u4e0d\u80fd\u518d\u8d70\u4e86<\/strong>\uff0c\u53ef\u4ee5\u4f7f\u7528 Map \u4f86\u78ba\u8a8d\u4e0b\u6b21\u7684\u8d77\u9ede\u662f\u5426\u6709\u88ab\u8d70\u904e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=d406\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge D406: \u5012\u6c34\u6642\u9593<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap nfd-wb-animate nfd-wb-reveal-right nfd-delay-50\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;map&gt;\nusing namespace std;\n\nint S, N, M, locsize = 3;\nvector&lt;vector&lt;int&gt;&gt;water, ans;\nvector&lt;pair&lt;int, int&gt;&gt;loc;\n\npair&lt;int, int&gt; axis (int a, int b)\n{\n    pair&lt;int, int&gt;tmp;\n    tmp.first = a;\n    tmp.second = b;\n    return tmp;\n}\n\nvoid BFS(vector&lt;pair&lt;int, int&gt;&gt;start, map&lt;pair&lt;int, int&gt;, int&gt;MAP, int count)\n{\n    if (start.size() == 0) return;\n    vector&lt;pair&lt;int, int&gt;&gt;newStart;\n    for (int i = 0; i&lt;start.size(); i++)\n    {\n        int y = start[i].first, x = start[i].second;\n        ans[y][x] = count;\n        MAP[start[i]]++;\n        for (int j = 0; j&lt;locsize; j++)\n        {\n            int yy = y + loc[j].first, xx = x + loc[j].second;\n            if (yy &gt;= 0 && yy &lt; N && xx &gt;= 0 && xx &lt; M && water[yy][xx] == 1 && MAP[axis(yy, xx)] == 0) newStart.push_back(axis(yy, xx));\n        }\n    }\n    BFS(newStart, MAP, count+1);\n}\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    int count = 1;\n    while (cin &gt;&gt; S &gt;&gt; N &gt;&gt; M)\n    {\n        loc.clear();\n        loc.push_back(axis(1, 0));\n        loc.push_back(axis(0, 1));\n        loc.push_back(axis(0, -1));\n        locsize = 3;\n        water.clear();\n        ans.clear();\n        vector&lt;pair&lt;int, int&gt;&gt;start;\n        for (int i = 0; i&lt;N; i++)\n        {\n            vector&lt;int&gt;v;\n            vector&lt;int&gt;hi;\n            for (int j = 0; j&lt;M; j++)\n            {\n                int tmp;\n                cin &gt;&gt; tmp;\n                if (i == 0 && tmp == 1) start.push_back(axis(i, j));\n                v.push_back(tmp);\n                hi.push_back(0);\n            }\n            water.push_back(v);\n            ans.push_back(hi);\n        }\n        if (S == 1)\n        {\n            loc.push_back(axis(-1, 0));\n            locsize++;\n        }\n        map&lt;pair&lt;int, int&gt;, int&gt;MAP;\n        BFS(start, MAP, 1);\n        cout &lt;&lt; &quot;Case &quot; &lt;&lt; count &lt;&lt; &quot;:\\n&quot;;\n        for (int i = 0; i&lt;N; i++)\n        {\n            for (int j = 0; j&lt;M; j++)\n            {\n                cout &lt;&lt; ans[i][j] &lt;&lt; &quot; &quot;;\n            }\n            cout &lt;&lt; &quot;\\n&quot;;\n        }\n        count++;\n    }\n}\n\n\/\/ZeroJudge D406\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6c34\u7531\u4e0a\u800c\u4e0b\u7684\u6d41\uff0c\u73fe\u5728\u7d66\u4f60\u6c34\u7ba1\u4e4b\u9593\u7684\u5730\u5716\u6c34\u53ef\u4ee5\u7531\u4e0a\u800c\u4e0b\uff0c\u53ef\u4ee5\u5f80\u53f3\u6d41\uff0c\u5f80\u5de6\u6d41\u3002 \u53ef\u662f\u5462 \u2026 \u6709\u6642\u5019\u4e5f\u53ef\u4ee5\u5f80\u4e0a\u6d41\u73fe\u5728 [&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":[29],"tags":[30,14,23,22,8,9],"class_list":["post-501","post","type-post","status-publish","format-standard","hentry","category-zerojudge-","tag-bfs","tag-map","tag-pair","tag-22","tag-8","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/501","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=501"}],"version-history":[{"count":2,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/501\/revisions"}],"predecessor-version":[{"id":503,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/501\/revisions\/503"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}