{"id":249,"date":"2022-10-30T19:28:46","date_gmt":"2022-10-30T11:28:46","guid":{"rendered":"https:\/\/muidar.com\/blog\/?p=249"},"modified":"2026-08-05T23:16:57","modified_gmt":"2026-08-05T15:16:57","slug":"dfs","status":"publish","type":"post","link":"https:\/\/muidar.com\/blog\/posts\/cmuidar\/dfs\/","title":{"rendered":"\u6df1\u5ea6\u4f18\u5148\u641c\u7d22"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u6df1\u5ea6\u4f18\u5148\u641c\u7d22<\/p>\n<cite>Depth First Search\uff0c\u7b80\u79f0DFS\uff1a\u4e00\u79cd\u7528\u4e8e\u904d\u5386\u6216\u641c\u7d22\u6811\u6216\u56fe\u7684\u7b97\u6cd5\u3002 \u6cbf\u7740\u6811\u7684\u6df1\u5ea6\u904d\u5386\u6811\u7684\u8282\u70b9\uff0c\u5c3d\u53ef\u80fd\u6df1\u7684\u641c\u7d22\u6811\u7684\u5206\u652f\u3002\u5f53\u8282\u70b9v\u7684\u6240\u5728\u8fb9\u90fd\u5df1\u88ab\u63a2\u5bfb\u8fc7\u6216\u8005\u5728\u641c\u5bfb\u65f6\u7ed3\u70b9\u4e0d\u6ee1\u8db3\u6761\u4ef6\uff0c\u641c\u7d22\u5c06\u56de\u6eaf\u5230\u53d1\u73b0\u8282\u70b9v\u7684\u90a3\u6761\u8fb9\u7684\u8d77\u59cb\u8282\u70b9\u3002\u6574\u4e2a\u8fdb\u7a0b\u53cd\u590d\u8fdb\u884c\u76f4\u5230\u6240\u6709\u8282\u70b9\u90fd\u88ab\u8bbf\u95ee\u4e3a\u6b62\u3002\u5c5e\u4e8e\u76f2\u76ee\u641c\u7d22,\u6700\u7cdf\u7cd5\u7684\u60c5\u51b5\u7b97\u6cd5\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n!)\u3002<\/cite><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u672c\u6846\u67b6<\/h3>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">void dfs(int step) {\n    if (\u5230\u8fbe\u76ee\u7684\u5730) { \/\/\u76ee\u6807\u72b6\u6001\n        \u8f93\u51fa\u89e3;\n    } else {\n        for (int i=1; i&lt;=\u5b50\u72b6\u6001\u6570; i++) {\/\/\u4ea7\u751f\u5f0f\n            if (\u6ee1\u8db3\u6761\u4ef6) {\/\/\u6269\u5c55\u6761\u4ef6\n                \u4fdd\u5b58\u7ed3\u679c;\n                dfs(step+1); \/\/\u4e0b\u4e00\u6b65\n                \u56de\u6eaf; \n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">void dfs(int step) {\n    for (int i=1; i&lt;=\u5b50\u72b6\u6001\u6570; i++) {\n        if (\u6ee1\u8db3\u6761\u4ef6) {\n            \u4fdd\u5b58\u7ed3\u679c;\n            if (\u5230\u8fbe\u76ee\u7684\u5730) \u8f93\u51fa\u89e3; \n            else dfs(step+1); \/\/\u4e0b\u4e00\u6b65\n            \u56de\u6eaf;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u53d1\u660e\u8005<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u662f\u7531Jonh E. Hopcroft \u548cRobert E. Tarjan\u53d1\u660e\u7684\u3002\u57281971~1972\u5e74\uff0c\u5728\u52a0\u5229\u798f\u5c3c\u4e9a\u5927\u5b66\u7814\u7a76\u56fe\u7684\u8fde\u901a\u6027\u548c\u5e73\u9762\u6027\u65f6\u53d1\u660e\u7684\u3002\u4ed6\u4eec\u56e0\u6b64\u83b7\u5f97\u4e861986\u5e74\u7684\u56fe\u7075\u5956\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f8b\u9898\uff08\u4e00\u7ef4\uff09<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"http:\/\/ybt.ssoier.cn:8088\/problem_show.php?pid=1317\" target=\"_blank\" rel=\"noreferrer noopener\">1317\uff1a\u3010\u4f8b5.2\u3011\u7ec4\u5408\u7684\u8f93\u51fa<\/a><\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u3010\u9898\u76ee\u63cf\u8ff0\u3011<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u6392\u5217\u4e0e\u7ec4\u5408\u662f\u5e38\u7528\u7684\u6570\u5b66\u65b9\u6cd5\uff0c\u5176\u4e2d\u7ec4\u5408\u5c31\u662f\u4ecen\u4e2a\u5143\u7d20\u4e2d\u62bd\u51far\u4e2a\u5143\u7d20(\u4e0d\u5206\u987a\u5e8f\u4e14r\u2264n)\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u5c06n\u4e2a\u5143\u7d20\u7406\u89e3\u4e3a\u81ea\u7136\u65701\uff0c2\uff0c\u2026\uff0cn\uff0c\u4ece\u4e2d\u4efb\u53d6r\u4e2a\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u8981\u6c42\u4f60\u7528\u9012\u5f52\u7684\u65b9\u6cd5\u8f93\u51fa\u6240\u6709\u7ec4\u5408\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982n\uff1d5\uff0cr\uff1d3\uff0c\u6240\u6709\u7ec4\u5408\u4e3a\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 2 3 &nbsp; 1 2 4 &nbsp; 1 2 5 &nbsp; 1 3 4 &nbsp; 1 3 5 &nbsp; 1 4 5 &nbsp; 2 3 4 &nbsp; 2 3 5 &nbsp; 2 4 5 &nbsp; 3 4 5<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u3010\u8f93\u5165\u3011<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u884c\u4e24\u4e2a\u81ea\u7136\u6570n\u3001r(1&lt;n&lt;21\uff0c1\u2264r\u2264n)\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u3010\u8f93\u51fa\u3011<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u6709\u7684\u7ec4\u5408\uff0c\u6bcf\u4e00\u4e2a\u7ec4\u5408\u5360\u4e00\u884c\u4e14\u5176\u4e2d\u7684\u5143\u7d20\u6309\u7531\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\uff0c\u6bcf\u4e2a\u5143\u7d20\u5360\u4e09\u4e2a\u5b57\u7b26\u7684\u4f4d\u7f6e\uff0c\u6240\u6709\u7684\u7ec4\u5408\u4e5f\u6309\u5b57\u5178\u987a\u5e8f\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u3010\u8f93\u5165\u6837\u4f8b\u3011<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">5 3<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u3010\u8f93\u51fa\u6837\u4f8b\u3011<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">  1  2  3\n  1  2  4\n  1  2  5\n  1  3  4\n  1  3  5\n  1  4  5\n  2  3  4\n  2  3  5\n  2  4  5\n  3  4  5\n<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">AC\u4ee3\u7801\u53c2\u8003<\/h5>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">#include &lt;bits\/stdc++.h>\nusing namespace std;\n\nint n,r;\nint a[24], vis[24];\n\nvoid dfs(int step) {\n\t\/\/\u8fb9\u754c \n\tif (step==r+1) {\n\t\t\/\/\u8f93\u51fa\u89e3 \n\t\tfor (int i=1; i&lt;=r; ++i) cout&lt;&lt;setw(3)&lt;&lt;a[i];\n\t\tcout&lt;&lt;endl;\n\t\treturn ;\n\t}\n\tfor (int i=1; i&lt;=n; ++i) {\n\t\tif (!vis[i] &amp;&amp; i>a[step-1]) { \/\/\u6761\u4ef6 \n\t\t\ta[step]=i;\n\t\t\tvis[i]=1;\n\t\t\tdfs(step+1);\n\t\t\tvis[i]=0;\n\t\t}\n\t}\n}\n\nint main() {\n\t\/\/\u89e3\u7ed1 \n\tios::sync_with_stdio(0);\n\tcin.tie(0);\n\tcout.tie(0);\n\tcin>>n>>r;\n\tdfs(1);\n\treturn 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5ea6\u4f18\u5148\u641c\u7d22 Depth First Search\uff0c\u7b80\u79f0DFS\uff1a\u4e00\u79cd\u7528\u4e8e\u904d\u5386\u6216\u641c\u7d22\u6811\u6216\u56fe\u7684\u7b97\u6cd5\u3002 \u6cbf\u7740\u6811\u7684\u6df1\u5ea6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-249","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/muidar.com\/blog\/wp-json\/wp\/v2\/posts\/249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/muidar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/muidar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/muidar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/muidar.com\/blog\/wp-json\/wp\/v2\/comments?post=249"}],"version-history":[{"count":1,"href":"https:\/\/muidar.com\/blog\/wp-json\/wp\/v2\/posts\/249\/revisions"}],"predecessor-version":[{"id":2070,"href":"https:\/\/muidar.com\/blog\/wp-json\/wp\/v2\/posts\/249\/revisions\/2070"}],"wp:attachment":[{"href":"https:\/\/muidar.com\/blog\/wp-json\/wp\/v2\/media?parent=249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/muidar.com\/blog\/wp-json\/wp\/v2\/categories?post=249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/muidar.com\/blog\/wp-json\/wp\/v2\/tags?post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}