{"id":411,"date":"2025-09-08T23:20:46","date_gmt":"2025-09-08T15:20:46","guid":{"rendered":"https:\/\/m.qzcy2.top\/?p=411"},"modified":"2025-09-09T00:07:35","modified_gmt":"2025-09-08T16:07:35","slug":"njtc-oj-testquestion-cpp","status":"publish","type":"post","link":"https:\/\/m.qzcy2.top\/index.php\/2025\/09\/08\/njtc-oj-testquestion-cpp\/","title":{"rendered":"\u9762\u5bf9\u65b0\u624b\u5411\u7684\u66b4\u529b\u8fc7\u9898\u6280\u5de7-cpp"},"content":{"rendered":"<h1>\u9762\u5bf9\u65b0\u624b\u5411\u7684\u66b4\u529b\u8fc7\u9898\u6280\u5de7-cpp<\/h1>\n<p>njtc-\u7b97\u7ade\u961f-yxy<\/p>\n<h2>\u5199\u5728\u524d\u9762<\/h2>\n<ul>\n<li>\u5728\u4e00\u4e9b\u7c7b\u4f3c\u4e8e\u8ff7\u5bab\u95ee\u9898\u3001\u8d70\u683c\u5b50\u3001\u6700\u77ed\u8def\u3001\u6700\u957f\u8def\u3001\u5bfb\u8def\u95ee\u9898\u3001\u6811\u4e0a\u5bfb\u5b9d\u95ee\u9898\u7b49 \u901a\u5e38\u4f1a\u91c7\u7528bfs\u3001dfs\u3001dp\u7b49\u65b9\u6cd5\u3002\u6211\u5229\u7528\u4e00\u4e9b\u9898\u578b\u6765\u7b80\u5355\u7684\u4f7f\u7528\u4e00\u4e0b\u8fc7\u9898\u7684\u601d\u60f3\u3002<\/li>\n<\/ul>\n<h2>\u4ecb\u7ecd<\/h2>\n<ul>\n<li>tips\uff1a\u4ee5\u4e0b\u89e3\u91ca\u6765\u81eadeepseek<\/li>\n<li>\n<p class=\"ds-markdown-paragraph\">\u5f53\u6211\u4eec\u60f3\u5728\u8bf8\u5982\u6811\u6216\u56fe\u8fd9\u7c7b\u590d\u6742\u6570\u636e\u7ed3\u6784\u4e2d\u5bfb\u627e\u7279\u5b9a\u4fe1\u606f\u65f6\uff0cBFS\uff08\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff09\u548cDFS\uff08\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff09\u662f\u4e24\u79cd\u6700\u57fa\u7840\u4e14\u91cd\u8981\u7684\u641c\u7d22\u7b56\u7565\u3002\u5b83\u4eec\u7684\u6838\u5fc3\u533a\u522b\u5728\u4e8e<strong>\u8bbf\u95ee\u8282\u70b9\u7684\u987a\u5e8f<\/strong>\u3002<\/p>\n<p class=\"ds-markdown-paragraph\">\u60f3\u8c61\u4e00\u4e0b\u4f60\u8eab\u5904\u4e00\u4e2a\u5de8\u5927\u7684\u8ff7\u5bab\uff1a<\/p>\n<ul>\n<li>\n<p class=\"ds-markdown-paragraph\"><strong>BFS\uff08\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff09<\/strong>\u00a0\u5c31\u50cf\u662f\u4e00\u652f\u8bad\u7ec3\u6709\u7d20\u7684\u519b\u961f\uff0c\u4f1a<strong>\u9010\u5c42\u63a8\u8fdb<\/strong>\u3002\u5b83\u4ece\u8d77\u70b9\u5f00\u59cb\uff0c\u9996\u5148\u8bbf\u95ee\u6240\u6709\u4e00\u6b65\u5c31\u80fd\u5230\u8fbe\u7684\u5730\u65b9\uff0c\u7136\u540e\u518d\u53bb\u8bbf\u95ee\u4ece\u8fd9\u4e9b\u5730\u65b9\u51fa\u53d1\u4e24\u6b65\u80fd\u5230\u8fbe\u7684\u6240\u6709\u65b0\u5730\u65b9\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u4f60\u627e\u5230\u7684\u8def\u5f84\u4e00\u5b9a\u662f<strong>\u6700\u77ed\u8def\u5f84<\/strong>\u3002\u5b83\u901a\u5e38\u501f\u52a9\u4e00\u4e2a\u961f\u5217\uff08Queue\uff09\u6765\u5b9e\u73b0\u201c\u5148\u6765\u5148\u670d\u52a1\u201d\u7684\u987a\u5e8f\u3002<\/p>\n<\/li>\n<li>\n<p class=\"ds-markdown-paragraph\"><strong>DFS\uff08\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff09<\/strong>\u00a0\u5219\u50cf\u4e00\u4e2a\u6267\u7740\u63a2\u9669\u5bb6\uff0c\u4f1a<strong>\u4e00\u6761\u8def\u8d70\u5230\u9ed1<\/strong>\u3002\u5b83\u4ece\u8d77\u70b9\u9009\u62e9\u4e00\u6761\u5c94\u8def\u4e00\u76f4\u8d70\u4e0b\u53bb\uff0c\u76f4\u5230\u5c3d\u5934\uff0c\u7136\u540e\u9000\u56de\u4e0a\u4e00\u4e2a\u5c94\u53e3\uff0c\u5c1d\u8bd5\u53e6\u4e00\u6761\u6ca1\u8d70\u8fc7\u7684\u8def\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f18\u5148\u63a2\u7d22\u7684\u662f\u56fe\u7684\u201c\u6df1\u5ea6\u201d\uff0c\u5b83\u53ef\u80fd\u4f1a\u5f88\u5feb\u5730\u8fdb\u5165\u6df1\u5c42\u5206\u652f\uff0c\u4f46\u627e\u5230\u7684\u8def\u5f84\u4e0d\u4e00\u5b9a\u662f\u6700\u77ed\u7684\u3002\u5b83\u901a\u5e38\u501f\u52a9\u4e00\u4e2a\u6808\uff08Stack\uff09\u6765\u5b9e\u73b0\uff0c\u6216\u8005\u76f4\u63a5\u4f7f\u7528\u51fd\u6570\u7684\u9012\u5f52\u8c03\u7528\u6765\u6a21\u62df\u8fd9\u4e2a\u201c\u56de\u6eaf\u201d\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/li>\n<\/ul>\n<p class=\"ds-markdown-paragraph\"><strong>\u7b80\u5355\u603b\u7ed3\u4e00\u4e0b\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p class=\"ds-markdown-paragraph\"><strong>BFS<\/strong>\uff1a\u5e7f\u5ea6\u4f18\u5148\uff0c\u5c42\u5c42\u626b\u8361\uff0c\u627e\u5230\u7684\u662f\u6700\u77ed\u8def\u5f84\u3002<\/p>\n<\/li>\n<li>\n<p class=\"ds-markdown-paragraph\"><strong>DFS<\/strong>\uff1a\u6df1\u5ea6\u4f18\u5148\uff0c\u52c7\u5f80\u76f4\u524d\uff0c\u56de\u6eaf\u53e6\u5bfb\u4ed6\u8def\u3002<\/p>\n<\/li>\n<\/ul>\n<p class=\"ds-markdown-paragraph\">\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u7b97\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\uff1a\u5982\u679c\u9700\u8981\u6700\u77ed\u8def\u5f84\u6216\u6700\u8fd1\u7684\u7ed3\u679c\uff08\u6bd4\u5982\u793e\u4ea4\u7f51\u7edc\u4e2d\u7684\u597d\u53cb\u5173\u7cfb\uff09\uff0cBFS\u66f4\u5408\u9002\uff1b\u5982\u679c\u9700\u8981\u904d\u5386\u6240\u6709\u53ef\u80fd\uff08\u6bd4\u5982\u6392\u5217\u7ec4\u5408\u3001\u7a77\u4e3e\u6240\u6709\u65b9\u6848\uff09\uff0c\u6216\u8005\u6811\u7ed3\u6784\u975e\u5e38\u6df1\u800c\u5bbd\uff0cDFS\u5f80\u5f80\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/li>\n<\/ul>\n<h2>\u76f8\u5173\u9898\u76ee<\/h2>\n<h3>\u8ff7\u5bab\u95ee\u9898(\u7ecf\u5178)<\/h3>\n<p>\u539f\u9898\u94fe\u63a5\u6765\u81ea\uff1a(njtc-oj\u5e73\u53f0)xfxcy <a href=\"https:\/\/xfxcy.com\/p\/P0230\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/xfxcy.com\/p\/P0230<\/a><\/p>\n<ul>\n<li>\u9898\u76ee\u63cf\u8ff0\uff1a\u8ff7\u5bab\u7531\u4e00\u4e2a\u7f51\u683c\u7ec4\u6210\uff0c\u6bcf\u4e2a\u5355\u5143\u683c\u8981\u4e48\u662f\u8def\u5f84\uff08\u7528 0 \u8868\u793a\uff09\uff0c\u8981\u4e48\u662f\u65e0\u6cd5\u901a\u8fc7\u7684\u5899\u58c1\uff08\u7528 1 \u8868\u793a\uff09\u3002\u5c0f\u98de\u4fa0\u4ece\u8ff7\u5bab\u7684\u5de6\u4e0a\u89d2\u5f00\u59cb\uff08\u5750\u6807\u4e3a ( 1 , 1 ) (1,1)\uff09\uff0c\u76ee\u6807\u662f\u5230\u8fbe\u8ff7\u5bab\u7684\u53f3\u4e0b\u89d2\uff08\u5750\u6807\u4e3a ( n , m ) (n,m)\uff09\u3002 \u5728\u8ff7\u5bab\u4e2d\uff0c\u5c0f\u98de\u4fa0\u53ef\u4ee5\u5411\u4e0a\u3001\u5411\u4e0b\u3001\u5411\u5de6\u6216\u5411\u53f3\u79fb\u52a8\u3002<\/li>\n<li>\u8f93\u5165\u683c\u5f0f\uff1a\u7b2c\u4e00\u884c\u5305\u542b\u4e24\u4e2a\u6574\u6570 n n \u548c m m\u3002 \u63a5\u4e0b\u6765 n n \u884c\uff0c\u6bcf\u884c\u5305\u542b m m \u4e2a\u6574\u6570\uff08 0 0 \u6216 1 1\uff09\uff0c\u8868\u793a\u5b8c\u6574\u7684\u4e8c\u7ef4\u6570\u7ec4\u8ff7\u5bab\uff1b<\/li>\n<li>\u8f93\u51fa\u683c\u5f0f\uff1a\u8f93\u51fa\u4e00\u4e2a\u6574\u6570\uff0c\u8868\u793a\u4ece\u5de6\u4e0a\u89d2\u79fb\u52a8\u81f3\u53f3\u4e0b\u89d2\u7684\u6700\u5c11\u79fb\u52a8\u6b21\u6570\uff1b<\/li>\n<li>\u6570\u636e\u8303\u56f4\uff1a1\u2264n\u22641000.\u00a0 \u00a01\u2264m\u22641000.\u00a0 \u00a01\u2264m\u22641000.<\/li>\n<\/ul>\n<p><strong>\u8003\u8651\u5230\u65f6\u95f4\u590d\u6742\u5ea6 \u672c\u9898\u65e0\u6cd5\u4f7f\u7528dfs\uff0c\u6240\u4ee5\u4f7f\u7528bfs\u89e3\u51b3\u3002<\/strong><\/p>\n<h4>\u53c2\u8003\u4ee3\u7801(bfs)\uff1a<\/h4>\n<p><span style=\"color: #008000;\">\u975e\u7279\u6b8a\u60c5\u51b5\u4e0d\u63a8\u8350\u4f7f\u7528\u4e07\u80fd\u5934\uff0c\u4f1a\u62d6\u6162\u6574\u4f53\u7684\u7f16\u8bd1\u901f\u5ea6\u3002<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;iostream&gt;\r\n#include &lt;vector&gt;\r\n#include &lt;algorithm&gt;\r\n#include &lt;queue&gt;\r\n#include &lt;deque&gt;\r\n#include &lt;unordered_map&gt;\r\n#include &lt;map&gt;\r\n#include &lt;cstring&gt;\r\n#include &lt;cmath&gt;\r\n#include &lt;unordered_set&gt;\r\n#include &lt;set&gt;\r\n#include &lt;utility&gt;\r\n#include &lt;climits&gt;\r\n#include &lt;iomanip&gt;\r\n#include &lt;stack&gt;\r\n#include &lt;bitset&gt;\r\n\r\n#define int long long\r\n#define PII pair&lt;int, int&gt; \r\n#define TLLL tuple&lt;int , int , int&gt;\r\n#define INF 0x3f3f3f3f3f3f3f3f\r\n#define all(v) v.begin() + 1 , v.end()\r\n#define ALL(v) v.begin() , v.end()\r\n#define endl \"\\n\"\r\nusing namespace std;\r\n\r\nconst int N = 1e3 + 10;\r\nint n , m;\r\nint g[N][N] , dist[N][N];\r\nbool st[N][N];\r\n\r\nint dx[] = {0 , 1 , 0 , -1} , dy[] = {1 , 0 , -1 , 0};  \/\/\u56db\u4e2a\u65b9\u5411\r\n\r\nvoid bfs(int sx , int sy)\r\n{\r\n    queue&lt;PII&gt; q;\r\n    q.push({sx , sy});\r\n    st[sx][sy] = true;\r\n\r\n    while (q.size())\r\n    {\r\n        auto t = q.front();\r\n        q.pop();\r\n\r\n        int x = t.first , y = t.second;\r\n\r\n        for (int i = 0 ; i &lt; 4 ; i ++ )\r\n        {\r\n            int xx = dx[i] + x , yy = dy[i] + y;\r\n\r\n            if (xx &lt; 1 || xx &gt; n || yy &lt; 1 || yy &gt; m || g[xx][yy]) continue;\r\n            if (st[xx][yy]) continue;\r\n\r\n            st[xx][yy] = true;\r\n            dist[xx][yy] = dist[x][y] + 1;\r\n            q.push({xx , yy});\r\n            \r\n            if (xx == n &amp;&amp; yy == m) return ;\r\n        }\r\n    }\r\n}\r\n\r\nvoid solve() \r\n{   \r\n    cin &gt;&gt; n &gt;&gt; m;\r\n    for (int i = 1 ; i &lt;= n ; i ++ ) \r\n        for (int j = 1 ; j &lt;= m ; j ++ )\r\n            cin &gt;&gt; g[i][j];\r\n    \r\n    bfs(1 , 1);\r\n\r\n    cout &lt;&lt; dist[n][m] &lt;&lt; endl;\r\n\r\n    return ;\r\n}\r\n\r\nsigned main() \r\n{\r\n    ios::sync_with_stdio(false);\r\n    cin.tie(nullptr), cout.tie(nullptr);\r\n    int T = 1;\r\n    while (T -- ) solve();\r\n    return 0;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u989d\u5916\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528dijkstra(\u65e0\u8d1f\u6743\u8fb9\u7684\u60c5\u51b5\u4e0b)\u00a0 \u00a0<del><span style=\"color: #008000;\">\u5bf9\uff01\u6ca1\u9519\uff0c\u5c31\u662f\u79bb\u6563\u6570\u5b66\u91cc\u4f60\u5b66\u8fc7\u7684\u4e1c\u897f\uff01<\/span><\/del><\/strong><\/p>\n<ul>\n<li>\u89e3\u91ca\uff1a\n<p class=\"ds-markdown-paragraph\">Dijkstra\u7b97\u6cd5\u662f\u4e00\u79cd\u975e\u5e38\u8457\u540d\u4e14\u9ad8\u6548\u7684\u7b97\u6cd5\uff0c\u7528\u4e8e\u5728<strong>\u5e26\u6743\u56fe<\/strong>\u4e2d\u5bfb\u627e\u4ece\u4e00\u4e2a\u8d77\u59cb\u70b9\u5230\u6240\u6709\u5176\u4ed6\u70b9\u7684<strong>\u6700\u77ed\u8def\u5f84<\/strong>\u3002\u8fd9\u91cc\u7684\u201c\u6743\u201d\u901a\u5e38\u4ee3\u8868\u4e86\u8ddd\u79bb\u3001\u6210\u672c\u3001\u65f6\u95f4\u7b49\u53ef\u91cf\u5316\u7684\u5ea6\u91cf\u3002<\/p>\n<p class=\"ds-markdown-paragraph\">\u5b83\u4e0eBFS\u7684\u6838\u5fc3\u533a\u522b\u5728\u4e8e<strong>\u201c\u6743\u201d<\/strong>\u3002BFS\u8ba4\u4e3a\u6bcf\u8d70\u4e00\u6b65\u7684\u6210\u672c\u90fd\u662f\u76f8\u540c\u7684\uff08\u53ef\u4ee5\u770b\u4f5c\u6743\u503c\u4e3a1\uff09\uff0c\u5b83\u901a\u8fc7\u201c\u5c42\u6570\u201d\u6765\u8ba1\u7b97\u6700\u77ed\u8ddd\u79bb\u3002\u800cDijkstra\u7b97\u6cd5\u5904\u7406\u7684\u662f<strong>\u6743\u503c\u5404\u4e0d\u76f8\u540c\u7684\u56fe<\/strong>\uff0c\u67d0\u4e9b\u8def\u5f84\u53ef\u80fd\u8fb9\u6570\u591a\u4f46\u603b\u6210\u672c\u4f4e\uff0c\u67d0\u4e9b\u53ef\u80fd\u8fb9\u6570\u5c11\u4f46\u603b\u6210\u672c\u9ad8\u3002<\/p>\n<p class=\"ds-markdown-paragraph\"><strong>\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u53ef\u4ee5\u7528\u4e00\u4e2a\u6bd4\u55bb\u6765\u7406\u89e3\uff1a<\/strong><\/p>\n<p class=\"ds-markdown-paragraph\">\u60f3\u8c61\u4f60\u662f\u4e00\u4f4d\u57ce\u5e02\u4ea4\u901a\u6307\u6325\u5b98\uff0c\u76ee\u6807\u662f\u8ba1\u7b97\u4ece\u5e02\u4e2d\u5fc3\uff08\u8d77\u59cb\u70b9\uff09\u5230\u57ce\u5e02\u4e2d\u6240\u6709\u5176\u4ed6\u5730\u70b9\u7684\u6700\u77ed\u884c\u8f66\u65f6\u95f4\u3002<\/p>\n<ol start=\"1\">\n<li>\n<p class=\"ds-markdown-paragraph\"><strong>\u521d\u59cb\u5316<\/strong>\uff1a\u4f60\u9996\u5148\u6807\u8bb0\u5e02\u4e2d\u5fc3\u7684\u65f6\u95f4\u4e3a0\uff0c\u800c\u6240\u6709\u5176\u4ed6\u5730\u70b9\u7684\u65f6\u95f4\u6682\u65f6\u6807\u8bb0\u4e3a\u201c\u65e0\u7a77\u5927\u201d\uff0c\u8868\u793a\u4f60\u8fd8\u4e0d\u77e5\u9053\u5982\u4f55\u5230\u8fbe\u90a3\u91cc\u3002<\/p>\n<\/li>\n<li>\n<p class=\"ds-markdown-paragraph\"><strong>\u8d2a\u5fc3\u9009\u62e9<\/strong>\uff1a\u4f60\u4ece\u5f53\u524d\u5df2\u77e5\u53ef\u8fbe\u7684\u3001\u4e14\u5c1a\u672a\u88ab\u6700\u7ec8\u786e\u8ba4\u7684\u5730\u70b9\u4e2d\uff0c\u9009\u51fa<strong>\u79bb\u5e02\u4e2d\u5fc3\u6700\u8fd1\u7684\u90a3\u4e2a<\/strong>\uff08\u4e5f\u5c31\u662f\u5f53\u524d\u8017\u65f6\u6700\u77ed\u7684\u5730\u70b9\uff09\u3002\u4f60\u786e\u4fe1\uff0c\u5bf9\u4e8e\u8fd9\u4e2a\u5730\u70b9\uff0c\u4f60\u5df2\u7ecf\u627e\u5230\u4e86\u7edd\u5bf9\u6700\u77ed\u7684\u8def\u5f84\u3002\u56e0\u4e3a\u5982\u679c\u5b58\u5728\u4e00\u6761\u66f4\u77ed\u7684\u8def\u5f84\uff0c\u90a3\u5b83\u5fc5\u7136\u8981\u7ecf\u8fc7\u4e00\u4e2a\u8017\u65f6\u66f4\u957f\u7684\u5176\u4ed6\u5730\u70b9\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u53ef\u80fd\u7684\uff08\u524d\u63d0\u662f\u6240\u6709\u6743\u91cd\u4e0d\u80fd\u4e3a\u8d1f\u6570\uff09\u3002<\/p>\n<\/li>\n<li>\n<p class=\"ds-markdown-paragraph\"><strong>\u6269\u6563\u66f4\u65b0<\/strong>\uff1a\u57fa\u4e8e\u8fd9\u4e2a\u521a\u88ab\u786e\u8ba4\u7684\u5730\u70b9\uff0c\u4f60\u53bb\u67e5\u770b\u4ece\u5b83\u51fa\u53d1\u7684\u6240\u6709\u9053\u8def\u3002\u4f60\u8ba1\u7b97\uff1a\u201c\u4ece\u5e02\u4e2d\u5fc3\u5230\u8fd9\u91cc\u7684\u65f6\u95f4\u201d + \u201c\u4ece\u8fd9\u91cc\u5230\u4e0b\u4e00\u4e2a\u8def\u53e3\u7684\u65f6\u95f4\u201d\u3002\u5982\u679c\u8fd9\u4e2a\u603b\u65f6\u95f4\u6bd4\u4e4b\u524d\u8bb0\u5f55\u7684\u5230\u8fbe\u90a3\u4e2a\u8def\u53e3\u7684\u65f6\u95f4\u66f4\u77ed\uff0c\u4f60\u5c31\u66f4\u65b0\u90a3\u4e2a\u8def\u53e3\u7684\u8bb0\u5f55\u3002<\/p>\n<\/li>\n<li>\n<p class=\"ds-markdown-paragraph\"><strong>\u91cd\u590d<\/strong>\uff1a\u4f60\u4e0d\u65ad\u5730\u91cd\u590d\u7b2c2\u548c\u7b2c3\u6b65\uff1a\u4ece\u6240\u6709\u5df2\u77e5\u4f46\u672a\u786e\u8ba4\u7684\u5730\u70b9\u4e2d\u9009\u51fa\u8017\u65f6\u6700\u77ed\u7684\uff0c\u786e\u8ba4\u5b83\uff0c\u7136\u540e\u66f4\u65b0\u5b83\u7684\u90bb\u5c45\u3002\u76f4\u5230\u6240\u6709\u5730\u70b9\u90fd\u88ab\u786e\u8ba4\uff0c\u6216\u8005\u4f60\u627e\u5230\u4e86\u5230\u76ee\u6807\u5730\u70b9\u7684\u6700\u77ed\u8def\u5f84\u3002<\/p>\n<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h4>\u53c2\u8003\u4ee3\u7801(dijkstra)\uff1a<\/h4>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;iostream&gt;\r\n#include &lt;vector&gt;\r\n#include &lt;algorithm&gt;\r\n#include &lt;queue&gt;\r\n#include &lt;deque&gt;\r\n#include &lt;unordered_map&gt;\r\n#include &lt;map&gt;\r\n#include &lt;cstring&gt;\r\n#include &lt;cmath&gt;\r\n#include &lt;unordered_set&gt;\r\n#include &lt;set&gt;\r\n#include &lt;utility&gt;\r\n#include &lt;climits&gt;\r\n#include &lt;iomanip&gt;\r\n#include &lt;stack&gt;\r\n#include &lt;bitset&gt;\r\n\r\n#define int long long\r\n#define PII pair&lt;int, int&gt; \r\n#define TLLL tuple&lt;int , int , int&gt;\r\n#define INF 0x3f3f3f3f3f3f3f3f\r\n#define inf 0x3f\r\n#define all(v) v.begin() + 1 , v.end()\r\n#define ALL(v) v.begin() , v.end()\r\n#define endl \"\\n\"\r\nusing namespace std;\r\n\r\nconst int N = 4e6 + 10;\r\nint e[N] , ne[N] , h[N] , idx , dist[N];\r\nbool st[N];\r\n\r\n\/\/\u94fe\u5f0f\u524d\u5411\u661f\u5b58\u56fe\uff0c\u76f8\u5173\u6982\u5ff5\u53ef\u4ee5\u53c2\u8003\u8fc7\u5f80\u6587\u7ae0-&gt;\u56fe\u8bba\u57fa\u7840-\u900f\u6790\u94fe\u5f0f\u524d\u5411\u661f\r\ninline void add(int a , int b)\r\n{\r\n    e[idx] = b , ne[idx] = h[a] , h[a] = idx ++;\r\n}\r\n\r\n\/\/\u5c06\u4e8c\u7ef4\u5750\u6807\u8f6c\u5316\u4e3a\u4e00\u7ef4\uff08\u5373\uff1a\u4ece\u4e0a\u5230\u4e0b\uff0c\u4ece\u5de6\u5230\u53f3\uff0c\u4f9d\u6b21\u7f16\u53f7\u4e3a1,2,3,...,n * m\uff09\u3002\r\ninline int transIndex(int a , int b , int m)\r\n{\r\n    return (a - 1) * m + b;\r\n}\r\n\r\n\/\/dijkstra\r\nvoid dijkstra()\r\n{\r\n    memset(st , false , sizeof st);\r\n    memset(dist , inf , sizeof dist);\r\n\r\n    priority_queue&lt;PII , vector&lt;PII&gt; , greater&lt;PII&gt; &gt; heap;\r\n    heap.push({0 , 1});\r\n\r\n    dist[1] = 0;\r\n    st[1] = true;\r\n\r\n    while (heap.size())\r\n    {\r\n        auto t = heap.top();\r\n        heap.pop();\r\n\r\n        int distance = t.first , index = t.second;\r\n\r\n        for (int i = h[index] ; ~i ; i = ne[i])\r\n        {\r\n            int j = e[i];\r\n\r\n            if (dist[j] &gt; distance + 1)\r\n            {\r\n                dist[j] = distance + 1;\r\n                heap.push({ dist[j] , j});\r\n            }\r\n        }\r\n    }\r\n\r\n    return ;\r\n}\r\n\r\n\/\/\u51fd\u6570\u672c\u4f53\r\nvoid solve()\r\n{\r\n    int n , m;\r\n    cin &gt;&gt; n &gt;&gt; m;\r\n    vector&lt;vector&lt;int&gt; &gt; g(n + 1 , vector&lt;int&gt;(m + 1));\r\n    for (int i = 1 ; i &lt;= n ; i ++ )\r\n        for (int j = 1 ; j &lt;= m ; j ++ )\r\n            cin &gt;&gt; g[i][j];\r\n    \r\n    memset(h , -1 , sizeof h);\r\n    int dx[] = {0 , 1 , 0 , -1} , dy[] = {1 , 0 , -1 , 0};  \/\/\u56db\u4e2a\u65b9\u5411\r\n\r\n\/\/\u5750\u6807\u8f6c\u6362\r\n    for (int i = 1 ; i &lt;= n ; i ++ )\r\n    {\r\n        for (int j = 1 ; j &lt;= m ; j ++ )\r\n        {\r\n            if (g[i][j]) continue;\r\n\r\n            int u = transIndex(i , j , m);\r\n\r\n            for (int k = 0 ; k &lt; 4 ; k ++ )\r\n            {\r\n                int x = i + dx[k] , y = j + dy[k];\r\n\r\n                if (x &lt; 1 || x &gt; n || y &lt; 1 || y &gt; m || g[x][y]) continue;\r\n                \r\n                int v = transIndex(x , y , m);\r\n                add(u , v) , add(v , u);  \/\/\u9898\u76ee\u63cf\u8ff0\u53ef\u4ee5\u4e0a\u4e0b\u5de6\u53f3\uff0c\u5373 \u53cc\u5411\u56fe\r\n            }\r\n        }\r\n    }\r\n\r\n    dijkstra();\r\n    int end = transIndex(n , m , m);\r\n\r\n    cout &lt;&lt; dist[end] &lt;&lt; endl;\r\n\r\n    return ;\r\n}\r\n\r\nsigned main() \r\n{\r\n    ios::sync_with_stdio(false);\r\n    cin.tie(nullptr), cout.tie(nullptr);\r\n    int T = 1;\r\n    \/\/ cin &gt;&gt; T;\r\n    while (T -- ) solve();\r\n    return 0;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u6301\u7eed\u66f4\u65b0\u4e2d...<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9762\u5bf9\u65b0\u624b\u5411\u7684\u66b4\u529b\u8fc7\u9898\u6280\u5de7-cpp njtc-\u7b97\u7ade\u961f-yxy \u5199\u5728\u524d\u9762 \u5728\u4e00\u4e9b\u7c7b\u4f3c\u4e8e\u8ff7\u5bab\u95ee\u9898\u3001\u8d70\u683c\u5b50\u3001\u6700\u77ed\u8def\u3001\u6700\u957f\u8def\u3001\u5bfb\u8def\u95ee\u9898\u3001\u6811\u4e0a &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[7],"tags":[12,17,16,15,13,14],"class_list":["post-411","post","type-post","status-publish","format-standard","hentry","category-algorithm","tag-algorithm","tag-bfs","tag-dfs","tag-dijkstra","tag-13","tag-14"],"_links":{"self":[{"href":"https:\/\/m.qzcy2.top\/index.php\/wp-json\/wp\/v2\/posts\/411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/m.qzcy2.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/m.qzcy2.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/m.qzcy2.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/m.qzcy2.top\/index.php\/wp-json\/wp\/v2\/comments?post=411"}],"version-history":[{"count":11,"href":"https:\/\/m.qzcy2.top\/index.php\/wp-json\/wp\/v2\/posts\/411\/revisions"}],"predecessor-version":[{"id":423,"href":"https:\/\/m.qzcy2.top\/index.php\/wp-json\/wp\/v2\/posts\/411\/revisions\/423"}],"wp:attachment":[{"href":"https:\/\/m.qzcy2.top\/index.php\/wp-json\/wp\/v2\/media?parent=411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/m.qzcy2.top\/index.php\/wp-json\/wp\/v2\/categories?post=411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/m.qzcy2.top\/index.php\/wp-json\/wp\/v2\/tags?post=411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}