{"id":10412,"date":"2011-02-12T11:47:30","date_gmt":"2011-02-12T03:47:30","guid":{"rendered":"http:\/\/ukyoi.wordpress.com\/?p=10412"},"modified":"2011-02-12T11:47:30","modified_gmt":"2011-02-12T03:47:30","slug":"dfs%e8%a7%a3%e8%bf%b7%e5%ae%ab%e7%9a%84%e4%b8%80%e4%ba%9b%e6%83%b3%e6%b3%95","status":"publish","type":"post","link":"https:\/\/ukyoi.info\/wordpress\/?p=10412","title":{"rendered":"DFS\u89e3\u8ff7\u5bab\u7684\u4e00\u4e9b\u60f3\u6cd5"},"content":{"rendered":"<p>\u5199\u5728\u524d\u9762\uff1a\u79c1\u4e0d\u662f\u7a0b\u5e8f\u5458\uff0c\u4e5f\u4e0d\u662f\u5b66\u4fe1\u79d1\u7684\uff0c\u4e5f\u53ea\u662f\u6700\u8fd1\u5f00\u59cb\u6ca1\u4e8b\u7684\u65f6\u5019\u7ffb\u7ffbC\u8bed\u8a00\u7684\u4e66\u3002\u56e0\u6b64\u9601\u4e0b\u5f88\u53ef\u80fd\u4f1a\u89c9\u5f97\u4e0b\u6587\u6240\u8ff0\u7684\u60f3\u6cd5\u5f88\u539f\u59cb\u6216\u662f\u9601\u4e0b\u65e9\u5df2\u7528\u8fc7\u4e86\u3002\u79c1\u5728\u6b64\u53ea\u662f\u8bb0\u5f55\u4e00\u4e0b\u81ea\u5df1\u7684\u60f3\u6cd5\u3002<\/p>\n<p>\u73b0\u9636\u6bb5\u79c1\u5b66C\u8bed\u8a00\u7528\u7684\u4e66\u662f\u5b8b\u52b2\u6749\u7684\u300aLinux C\u7f16\u7a0b\u4e00\u7ad9\u5f0f\u5b66\u4e60\u300b\uff0c\u91cc\u9762\u8bb2\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7684\u65f6\u5019\u8bb2\u7684\u662f\u4e2a\u8d70\u8ff7\u5bab\u95ee\u9898\uff0c\u75281\u8868\u793a\u5899\uff0c0\u8868\u793a\u8def\uff0c\u4ece\u5de6\u4e0a\u8d70\u5230\u53f3\u4e0b\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<span style=\"color:#141312;font-family:Consolas, Monaco, 'Courier New', Courier, monospace;font-size:12px;line-height:18px;white-space:pre;\"> <\/span><\/p>\n<blockquote>\n<pre style=\"color:#141312;background-color:#ffffff;\"><span style=\"color:#006e28;\">#include <\/span><span style=\"color:#006e28;\">&lt;stdio.h&gt;<\/span>\n<span style=\"color:#006e28;\">#define MAX_ROW 5<\/span>\n<span style=\"color:#006e28;\">#define MAX_COL 5<\/span>\n\n<strong>struct<\/strong> point { <span style=\"color:#0057ae;\">int<\/span> row, col; } stack[<span style=\"color:#b08000;\">512<\/span>];\n<span style=\"color:#0057ae;\">int<\/span> top = <span style=\"color:#b08000;\">0<\/span>;\n\n<span style=\"color:#0057ae;\">void<\/span> push(<strong>struct<\/strong> point p)\n{\n    stack[top++] = p;\n}\n\n<strong>struct<\/strong> point pop(<span style=\"color:#0057ae;\">void<\/span>)\n{\n    <strong>return<\/strong> stack[--top];\n}\n\n<span style=\"color:#0057ae;\">int<\/span> is_empty(<span style=\"color:#0057ae;\">void<\/span>)\n{\n    <strong>return<\/strong> top == <span style=\"color:#b08000;\">0<\/span>;\n}\n\n<span style=\"color:#0057ae;\">int<\/span> maze[MAX_ROW][MAX_COL] = {\n    <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">1<\/span>, <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">0<\/span>,\n    <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">1<\/span>, <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">1<\/span>, <span style=\"color:#b08000;\">0<\/span>,\n    <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">0<\/span>,\n    <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">1<\/span>, <span style=\"color:#b08000;\">1<\/span>, <span style=\"color:#b08000;\">1<\/span>, <span style=\"color:#b08000;\">0<\/span>,\n    <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">1<\/span>, <span style=\"color:#b08000;\">0<\/span>,\n};\n\n<span style=\"color:#0057ae;\">void<\/span> print_maze(<span style=\"color:#0057ae;\">void<\/span>)\n{\n    <span style=\"color:#0057ae;\">int<\/span> i, j;\n    <strong>for<\/strong> (i = <span style=\"color:#b08000;\">0<\/span>; i &lt; MAX_ROW; i++) {\n        <strong>for<\/strong> (j = <span style=\"color:#b08000;\">0<\/span>; j &lt; MAX_COL; j++)\n            printf(<span style=\"color:#bf0303;\">\"%d \"<\/span>, maze[i][j]);\n        putchar(<span style=\"color:#ff80e0;\">'n'<\/span>);\n    }\n    printf(<span style=\"color:#bf0303;\">\"*********<\/span><span style=\"color:#ff80e0;\">n<\/span><span style=\"color:#bf0303;\">\"<\/span>);\n}\n\n<strong>struct<\/strong> point predecessor[MAX_ROW][MAX_COL] = {\n    {{-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}},\n    {{-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}},\n    {{-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}},\n    {{-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}},\n    {{-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}, {-<span style=\"color:#b08000;\">1<\/span>,-<span style=\"color:#b08000;\">1<\/span>}},\n};\n\n<span style=\"color:#0057ae;\">void<\/span> visit(<span style=\"color:#0057ae;\">int<\/span> row, <span style=\"color:#0057ae;\">int<\/span> col, <strong>struct<\/strong> point pre)\n{\n    <strong>struct<\/strong> point visit_point = { row, col };\n    maze[row][col] = <span style=\"color:#b08000;\">2<\/span>;\n    predecessor[row][col] = pre;\n    push(visit_point);\n}\n\n<span style=\"color:#0057ae;\">int<\/span> main(<span style=\"color:#0057ae;\">void<\/span>)\n{\n    <strong>struct<\/strong> point p = { <span style=\"color:#b08000;\">0<\/span>, <span style=\"color:#b08000;\">0<\/span> };\n\n    maze[p.row][p.col] = <span style=\"color:#b08000;\">2<\/span>;\n    push(p);\n\n    <strong>while<\/strong> (!is_empty()) {\n        p = pop();\n        <strong>if<\/strong> (p.row == MAX_ROW - <span style=\"color:#b08000;\">1<\/span>  <em><span style=\"color:#888786;\">\/* goal *\/<\/span><\/em>\n            &amp;&amp; p.col == MAX_COL - <span style=\"color:#b08000;\">1<\/span>)\n            <strong>break<\/strong>;\n        <strong>if<\/strong> (p.col+<span style=\"color:#b08000;\">1<\/span> &lt; MAX_COL     <em><span style=\"color:#888786;\">\/* right *\/<\/span><\/em>\n            &amp;&amp; maze[p.row][p.col+<span style=\"color:#b08000;\">1<\/span>] == <span style=\"color:#b08000;\">0<\/span>)\n            visit(p.row, p.col+<span style=\"color:#b08000;\">1<\/span>, p);\n        <strong>if<\/strong> (p.row+<span style=\"color:#b08000;\">1<\/span> &lt; MAX_ROW     <em><span style=\"color:#888786;\">\/* down *\/<\/span><\/em>\n            &amp;&amp; maze[p.row+<span style=\"color:#b08000;\">1<\/span>][p.col] == <span style=\"color:#b08000;\">0<\/span>)\n            visit(p.row+<span style=\"color:#b08000;\">1<\/span>, p.col, p);\n        <strong>if<\/strong> (p.col-<span style=\"color:#b08000;\">1<\/span> &gt;= <span style=\"color:#b08000;\">0<\/span>          <em><span style=\"color:#888786;\">\/* left *\/<\/span><\/em>\n            &amp;&amp; maze[p.row][p.col-<span style=\"color:#b08000;\">1<\/span>] == <span style=\"color:#b08000;\">0<\/span>)\n            visit(p.row, p.col-<span style=\"color:#b08000;\">1<\/span>, p);\n        <strong>if<\/strong> (p.row-<span style=\"color:#b08000;\">1<\/span> &gt;= <span style=\"color:#b08000;\">0<\/span>          <em><span style=\"color:#888786;\">\/* up *\/<\/span><\/em>\n            &amp;&amp; maze[p.row-<span style=\"color:#b08000;\">1<\/span>][p.col] == <span style=\"color:#b08000;\">0<\/span>)\n            visit(p.row-<span style=\"color:#b08000;\">1<\/span>, p.col, p);\n        print_maze();\n    }\n    <strong>if<\/strong> (p.row == MAX_ROW - <span style=\"color:#b08000;\">1<\/span> &amp;&amp; p.col == MAX_COL - <span style=\"color:#b08000;\">1<\/span>) {\n        printf(<span style=\"color:#bf0303;\">\"(%d, %d)<\/span><span style=\"color:#ff80e0;\">n<\/span><span style=\"color:#bf0303;\">\"<\/span>, p.row, p.col);\n        <strong>while<\/strong> (predecessor[p.row][p.col].row != -<span style=\"color:#b08000;\">1<\/span>) {\n            p = predecessor[p.row][p.col];\n            printf(<span style=\"color:#bf0303;\">\"(%d, %d)<\/span><span style=\"color:#ff80e0;\">n<\/span><span style=\"color:#bf0303;\">\"<\/span>, p.row, p.col);\n        }\n    } <strong>else<\/strong>\n        printf(<span style=\"color:#bf0303;\">\"No path!<\/span><span style=\"color:#ff80e0;\">n<\/span><span style=\"color:#bf0303;\">\"<\/span>);\n\n    <strong>return<\/strong> <span style=\"color:#b08000;\">0<\/span>;\n}<\/pre>\n<\/blockquote>\n<p>\u5927\u6982\u610f\u601d\u5c31\u662f\u6bcf\u6b21\u5f39\u51fa\u4e00\u4e2a\u6808\uff0c\u627e\u5230\u5468\u56f4\u6240\u6709\u7684\u5408\u6cd5\u6b65\u9aa4\u7136\u540e\u628a\u5b83\u4eec\u6808\u538b\u8fdb\u6808\u533a\uff0c\u4e0b\u4e00\u8f6e\u65f6\u518d\u5f39\u51fa\u6700\u540e\u538b\u5165\u7684\u6808\uff0c\u5982\u679c\u8d70\u5165\u6b7b\u8def\u5c31\u518d\u6b21\u5f39\u6808\uff0c\u627e\u53e6\u5916\u4e00\u6761\u53ef\u80fd\u8def\u7ebf\uff0c\u76f4\u5230\u627e\u5230\u8ff7\u5bab\u7684\u89e3\u3002\u5982\u679c\u6808\u7a7a\u4e86\u5c31\u8bf4\u660e\u65e0\u8def\u53ef\u8d70\uff0c\u6253\u51fa\u201cNo path !\u201d\u3002<br \/>\n\u4f46\u662f\u627e\u5230\u89e3\u4e4b\u540e\u5c31\u6bd4\u8f83\u9ebb\u70e6\u3002\u5982\u4f55\u8f93\u51fa\u8def\u5f84\u5462\uff1f\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u7684\u65b9\u6cd5\u662f\u5efa\u7acb\u4e00\u4e2apredecessor\u6570\u7ec4\uff0c\u7528\u6765\u8868\u793a\u6bcf\u4e2a\u70b9\u7684\u524d\u8d8b\uff0c\u7136\u540e\u4ece\u6700\u540e\u4e00\u4e2a\u70b9\u987a\u85e4\u6478\u74dc\u5f80\u524d\u627e\uff0c\u9010\u6e10\u6253\u5370\u51fa\u6765\u3002\u4f46\u8fd9\u4e2apredecessor\u6570\u7ec4\u5360\u7528\u7684\u7a7a\u95f4\u5f88\u5927\uff0c\u4e14\u53ea\u80fd\u4ece\u540e\u5411\u524d\u627e\uff0c\u4e0d\u4fbf\u4e8e\u627e\u5230\u5176\u4e2d\u7684\u67d0\u4e2a\u7279\u5b9a\u6b65\u9aa4\u3002\u600e\u4e48\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u5462\uff1f\u4f5c\u8005\u5b8b\u52b2\u6749\u51fa\u4e86\u4e00\u9053\u601d\u8003\u9898\uff0c\u8fd8\u95ee\u8bfb\u8005\u80fd\u591f\u60f3\u51fa\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u89c1\u65b9\u6cd5\u662f\u4e0d\u6b62\u4e00\u79cd\u7684\u3002<\/p>\n<p>\u5728\u6b63\u786e\u7406\u89e3\u8fd9\u6bb5\u4ee3\u7801\u4e4b\u524d\uff0c\u79c1\u4e00\u76f4\u8ba4\u4e3a\u6808\u533a\u4e2d\u81f3\u5c11\u5e94\u8be5\u5b58\u6709\u6b63\u786e\u8def\u5f84\u8d70\u8fc7\u7684\u6240\u6709\u7684\u70b9\uff0c\u540e\u6765\u624d\u660e\u767d\u8fd9\u4e2a\u60f3\u6cd5\u662f\u6709\u95ee\u9898\u7684\u3002\u7531\u4e8e\u6bcf\u4e2a\u5faa\u73af\u5f00\u59cb\u65f6\u6808\u9876\u5df2\u7ecf\u5f39\u51fa\u4e86\uff0c\u6b64\u65f6\u5982\u679c\u518d\u5728\u8fd9\u4e00\u70b9\u7ee7\u7eed\u5411\u6df1\u5904\u627e\uff0c\u65b0\u538b\u5165\u7684\u6808\u5c31\u4f1a\u628a\u521a\u624d\u5f39\u51fa\u7684\u6808\u9876\u8986\u76d6\u6389\uff0c\u8fd9\u6837\u627e\u5230\u6700\u540e\u6574\u4e2a\u6808\u4e2d\u5728\u6781\u7aef\u60c5\u51b5\u4e0b\uff08\u4f8b\u5982\u6574\u4e2a\u8ff7\u5bab\u90fd\u6ca1\u6709\u5c94\u8def\uff09\u53ef\u80fd\u4e00\u4e2a\u70b9\u4e5f\u4e0d\u5269\u3002\u6b64\u5916\u4e00\u4e2a\u5faa\u73af\u5185\u662f4\u4e2a\u65b9\u5411\u5747\u4f1a\u641c\u7d22\uff0c\u6240\u4ee5\u5982\u679c\u6709\u5c94\u8def\uff0c\u5c31\u4f1a\u538b\u5165\u591a\u4e2a\u70b9\uff0c\u4f46\u5f39\u51fa\u65f6\u53ea\u4f1a\u5f39\u51fa\u4e00\u4e2a\u70b9\uff0c\u6240\u4ee5\u6808\u4e2d\u4f1a\u6b8b\u7559\u9519\u8bef\u9053\u8def\u7684\u70b9\u3002<\/p>\n<p>\u600e\u4e48\u4fee\u6539\u5462\uff1f\u5176\u5b9e\u5f88\u5bb9\u6613\u3002\u539f\u6765\u6bcf\u4e2a\u5faa\u73af\u4f1a\u4ece\u6808\u9876\u5f39\u51fa\u4e00\u4e2a\u6808\uff0c\u5176\u5b9e\u627e\u5230\u8def\u5f84\u4e4b\u540e\uff0c\u5b8c\u5168\u53ef\u4ee5\u628a\u8fd9\u4e00\u6808\u518d\u538b\u56de\u53bb\uff0c\u6bd4\u5982\u8fd9\u6837\uff1a<\/p>\n<blockquote>\n<pre style=\"color:#141312;background-color:#ffffff;\"><span style=\"color:#0057ae;\">int<\/span><span style=\"color:#141312;\"> main(<\/span><span style=\"color:#0057ae;\">void<\/span><span style=\"color:#141312;\">)\n{\n    <\/span><strong>struct<\/strong><span style=\"color:#141312;\"> point p = { <\/span><span style=\"color:#b08000;\">0<\/span><span style=\"color:#141312;\">, <\/span><span style=\"color:#b08000;\">0<\/span><span style=\"color:#141312;\"> };\n\n    maze[p.row][p.col] = <\/span><span style=\"color:#b08000;\">2<\/span><span style=\"color:#141312;\">;\n    push(p);\n\n    <\/span><strong>while<\/strong><span style=\"color:#141312;\"> (!is_empty()) {\n        p = pop();\n        <\/span><strong>if<\/strong><span style=\"color:#141312;\"> (p.row == MAX_ROW - <\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\">  <\/span><em><span style=\"color:#888786;\">\/* goal *\/<\/span><\/em><span style=\"color:#141312;\">\n            &amp;&amp; p.col == MAX_COL - <\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\">)\n            <\/span><strong>break<\/strong><span style=\"color:#141312;\">;\n        <\/span><strong>if<\/strong><span style=\"color:#141312;\"> (p.col+<\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\"> &lt; MAX_COL     <\/span><em><span style=\"color:#888786;\">\/* right *\/<\/span><\/em><span style=\"color:#141312;\">\n            &amp;&amp; maze[p.row][p.col+<\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\">] == <\/span><span style=\"color:#b08000;\">0<\/span><span style=\"color:#141312;\">) {\n            top++;\n            visit(p.row, p.col+<\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\">, p);\n        } <\/span><strong>else<\/strong><span style=\"color:#141312;\"> <\/span><strong>if<\/strong><span style=\"color:#141312;\"> (p.row+<\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\"> &lt; MAX_ROW     <\/span><em><span style=\"color:#888786;\">\/* down *\/<\/span><\/em><span style=\"color:#141312;\">\n            &amp;&amp; maze[p.row+<\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\">][p.col] == <\/span><span style=\"color:#b08000;\">0<\/span><span style=\"color:#141312;\">) {\n            top++;\n            visit(p.row+<\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\">, p.col, p);\n        } <\/span><strong>else<\/strong><span style=\"color:#141312;\"> <\/span><strong>if<\/strong><span style=\"color:#141312;\"> (p.col-<\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\"> &gt;= <\/span><span style=\"color:#b08000;\">0<\/span><span style=\"color:#141312;\">          <\/span><em><span style=\"color:#888786;\">\/* left *\/<\/span><\/em><span style=\"color:#141312;\">\n            &amp;&amp; maze[p.row][p.col-<\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\">] == <\/span><span style=\"color:#b08000;\">0<\/span><span style=\"color:#141312;\">) {\n            top++;\n            visit(p.row, p.col-<\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\">, p);\n        } <\/span><strong>else<\/strong><span style=\"color:#141312;\"> <\/span><strong>if<\/strong><span style=\"color:#141312;\"> (p.row-<\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\"> &gt;= <\/span><span style=\"color:#b08000;\">0<\/span><span style=\"color:#141312;\">          <\/span><em><span style=\"color:#888786;\">\/* up *\/<\/span><\/em><span style=\"color:#141312;\">\n            &amp;&amp; maze[p.row-<\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\">][p.col] == <\/span><span style=\"color:#b08000;\">0<\/span><span style=\"color:#141312;\">) {\n            top++;\n            visit(p.row-<\/span><span style=\"color:#b08000;\">1<\/span><span style=\"color:#141312;\">, p.col, p);\n        }\n        print_maze();\n    }\n\n    <\/span><span style=\"color:#0057ae;\">int<\/span><span style=\"color:#141312;\"> i;\n    <\/span><strong>if<\/strong><span style=\"color:#141312;\"> (!is_empty()) {\n        <\/span><strong>for<\/strong><span style=\"color:#141312;\"> (i=<\/span><span style=\"color:#b08000;\">0<\/span><span style=\"color:#141312;\">; i&lt;top; i++)\n            printf(<\/span><span style=\"color:#bf0303;\">\"(%d, %d)<\/span><span style=\"color:#ff80e0;\">n<\/span><span style=\"color:#bf0303;\">\"<\/span><span style=\"color:#141312;\">, stack[i]);\n    } <\/span><strong>else<\/strong><span style=\"color:#141312;\">\n        printf(<\/span><span style=\"color:#bf0303;\">\"No path.<\/span><span style=\"color:#ff80e0;\">n<\/span><span style=\"color:#bf0303;\">\"<\/span><span style=\"color:#141312;\">);\n\n    <\/span><strong>return<\/strong><span style=\"color:#141312;\"> <\/span><span style=\"color:#b08000;\">0<\/span><span style=\"color:#141312;\">;\n}<\/span><\/pre>\n<\/blockquote>\n<p>\u5982\u679c\u627e\u5230\u4e86\u53ef\u7528\u7684\u8def\uff0c\u7531\u4e8e\u6709\u4e86top++\uff0c\u76f8\u5f53\u4e8e\u4ec5\u4ec5\u662f\u8bfb\u53d6\u5230\u6808\u9876\u7684\u6570\u636e\uff08\u800c\u6808\u6ca1\u6709\u5f39\u51fa\uff09\uff0c\u8fd9\u6837\u5728\u641c\u7d22\u8def\u5f84\u538b\u6808\u7684\u65f6\u5019\u5c31\u4e0d\u4f1a\u628a\u539f\u6765\u7684\u6808\u9876\u8986\u76d6\u6389\uff0c\u4ece\u800c\u4fdd\u8bc1\u4e86\u6808\u4e2d\u5b58\u5728\u6240\u6709\u6b63\u786e\u8def\u5f84\u4e0a\u7684\u70b9\u3002\u89e3\u51b3\u6b8b\u7559\u9519\u8bef\u9053\u8def\u7684\u70b9\u7684\u95ee\u9898\u4e5f\u5f88\u5bb9\u6613\uff0c\u53ea\u9700\u8ba9\u4e00\u4e2a\u5faa\u73af\u5185\u5982\u679c\u641c\u7d22\u5230\u4e86\u4e00\u6761\u53ef\u4ee5\u6df1\u5165\u7684\u8def\u5f84\u5c31\u505c\u6b62\uff08\u7528else\u5b9e\u73b0\uff09\uff0c\u5426\u5219\u518d\u641c\u7d22\u4e0b\u4e00\u6761\u8def\u5f84\u3002\u5982\u679c\u627e\u5230\u6b7b\u8def\u6808\u4f1a\u9010\u4e2a\u5f39\u51fa\uff0c\u76f4\u5230\u5c94\u8def\u53e3\uff0c\u6240\u4ee5\u4e0d\u7528\u62c5\u5fc3\u627e\u4e0d\u5230\u6b63\u89e3\u3002\u5230\u6700\u540e\u5982\u679c\u8ff7\u5bab\u5b58\u5728\u89e3\u7684\u8bdd\uff0c\u6808\u4e2d\u6240\u4fdd\u5b58\u7684\u5c31\u662f\u6b63\u786e\u7684\u8def\u5f84\u3002\u7531\u4e8e\u4f8b\u5b50\u4e2d\u7684\u201c\u6808\u201d\u662f\u4e00\u4e2a\u6570\u7ec4\uff08\u5f53\u7136\u60a8\u53ef\u80fd\u4f1a\u53cd\u9a73\u8bf4\u8fd9\u4e2a\u4f8b\u5b50\u4e2dstack[]\u5e76\u4e0d\u662f\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u6808\uff0c\u201c\u6808\u201d\u5728\u7406\u8bba\u4e0a\u5e94\u8be5\u53ea\u80fd\u8bbf\u95ee\u9876\u7aef\u7684\u5143\u7d20\uff0c\u4f46\u90a3\u53ea\u662f\u4e2a\u5f62\u5f0f\u95ee\u9898\uff09\uff0c\u4e8e\u662f\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8bbf\u95ee\u4efb\u4f55\u4e00\u6b65\u7684\u70b9\uff0c\u8f93\u51fa\u7684\u65f6\u5019\u6b63\u7740\u6253\u53cd\u7740\u6253\u8df3\u7740\u6253\u5747\u53ef\uff0c\u7a0b\u5e8f\u4e2d\u6240\u6709\u548cpredecessor\u6709\u5173\u7684\u5185\u5bb9\u4fbf\u53ef\u4ee5\u5220\u53bb\u4e86\u3002<\/p>\n<p>\u4f46\u662f\u4e8b\u5b9e\u4e0a\u8fd9\u4e2a\u529e\u6cd5\u5728\u7a0b\u5e8f\u6548\u7387\u4e0a\u662f\u52a3\u5316\u800c\u4e0d\u662f\u4f18\u5316\uff0c\u9996\u5148\u867d\u7136\u4e0d\u9700\u8981predecessor\u4e86\uff0c\u4f46\u662f\u6808\u7a7a\u95f4\u6d88\u8017\u53d8\u5f97\u66f4\u5927\u3002\u539f\u672c\u6808\u7a7a\u95f4\u7684\u6d88\u8017\u548c\u5206\u5c94\u8def\u7684\u591a\u5c11\u6709\u5173\uff0c\u73b0\u5728\u5219\u662f\u548c\u8def\u5f84\u7684\u957f\u5ea6\u6709\u5173\u800c\u4e0e\u6709\u591a\u5c11\u5c94\u8def\u65e0\u5173\u3002\u90a3\u4e48\u6808\u7a7a\u95f4\u7684\u6700\u5c0f\u503c\u5e94\u8be5\u662f\u591a\u5927\u5462\uff1f\u79c1\u8fd8\u6ca1\u8003\u8651\u597d\uff0c\u4f46\u5206\u914d\u4e3a\u6574\u4e2a\u8ff7\u5bab\u7684\u5927\u5c0f\u80af\u5b9a\u662f\u8db3\u591f\u7684\u3002\u8fd8\u6709\u4e2a\u95ee\u9898\u662f\u539f\u6765\u7684\u65b9\u5f0f\u7531\u4e8e\u8986\u76d6\u4e86\u4e4b\u524d\u8d70\u8fc7\u7684\u8def\u5f84\uff0c\u53ea\u4fdd\u7559\u5c94\u8def\u4e0a\u5176\u4ed6\u65b9\u5411\u7684\u70b9\uff0c\u5982\u679c\u627e\u5230\u6b7b\u8def\uff0c\u5f39\u6808\u540e\u4f1a\u76f4\u63a5\u8df3\u5230\u53e6\u5916\u4e00\u4e2a\u53ef\u80fd\u7684\u5c94\u8def\u7ee7\u7eed\u641c\u7d22\uff0c\u800c\u4fee\u6539\u540e\u7684\u65b9\u6cd5\u5982\u679c\u627e\u5230\u6b7b\u8def\uff0c\u5fc5\u987b\u6cbf\u539f\u8def\u4e00\u6b65\u6b65\u8df3\u56de\u5c94\u8def\u53e3\u3002\u518d\u52a0\u4e0atop++\u6240\u7528\u7684\u65f6\u95f4\uff0c\u7a0b\u5e8f\u6548\u7387\u6bd4\u539f\u6765\u8981\u4f4e\uff0c\u800c\u4e14\u5c94\u8def\u8d8a\u591a\u8d8a\u6df1\u5165\uff0c\u6548\u7387\u4e0a\u7684\u5dee\u8ddd\u8d8a\u660e\u663e\u3002<\/p>\n<p>\u524d\u51e0\u5929\u770b\u5230\u4e2a\u597d\u73a9\u7684\u63a8\uff0c\u8bf4\u201c\u60c5\u4eba\u8282\u6211\u73a9\u4e00\u5929\u8fde\u8fde\u770b\uff0c\u6d88\u706d\u4e00\u5bf9\u662f\u4e00\u5bf9\u201d\u3002\u79c1\u5bfb\u601d\u65e2\u7136\u5b66\u4e86DFS\u548cBFS\uff0c\u4e0d\u5982\u81ea\u5df1\u4e5f\u5199\u4e2a\u6587\u672c\u8fde\u8fde\u770b\u73a9\u73a9\uff1f\u518d\u8bf4\u5427\u3002<\/p>\n<p>\u53e6\uff1a\u4e3a\u561b\u6211Tag\u91cc\u5199C\uff0cwordpress\u4f1a\u7ed9\u81ea\u52a8\u8865\u5168\u6210C++\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5199\u5728\u524d\u9762\uff1a\u79c1\u4e0d\u662f\u7a0b\u5e8f\u5458\uff0c\u4e5f\u4e0d\u662f\u5b66\u4fe1\u79d1\u7684\uff0c\u4e5f\u53ea\u662f\u6700\u8fd1\u5f00\u59cb\u6ca1\u4e8b\u7684\u65f6\u5019\u7ffb\u7ffbC\u8bed\u8a00\u7684\u4e66\u3002\u56e0\u6b64\u9601\u4e0b\u5f88\u53ef\u80fd\u4f1a\u89c9\u5f97\u4e0b\u6587\u6240\u8ff0\u7684 &#8230; <a title=\"DFS\u89e3\u8ff7\u5bab\u7684\u4e00\u4e9b\u60f3\u6cd5\" class=\"read-more\" href=\"https:\/\/ukyoi.info\/wordpress\/?p=10412\" aria-label=\"\u9605\u8bfb DFS\u89e3\u8ff7\u5bab\u7684\u4e00\u4e9b\u60f3\u6cd5\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,9,12],"tags":[19],"class_list":["post-10412","post","type-post","status-publish","format-standard","hentry","category-4","category-9","category--internet","tag-c"],"_links":{"self":[{"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/10412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10412"}],"version-history":[{"count":0,"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/10412\/revisions"}],"wp:attachment":[{"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ukyoi.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}