{"id":319062,"date":"2021-02-05T08:29:52","date_gmt":"2021-02-05T00:29:52","guid":{"rendered":"http:\/\/4563.org\/?p=319062"},"modified":"2021-02-05T08:29:52","modified_gmt":"2021-02-05T00:29:52","slug":"%e8%b0%b7%e6%ad%8c%e9%ab%98%e9%a2%91%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e7%a8%8b%e5%ba%8f%e6%a3%80%e6%9f%a5","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=319062","title":{"rendered":"\u8c37\u6b4c\u9ad8\u9891\u9762\u8bd5\u9898\uff1a\u7a0b\u5e8f\u68c0\u67e5"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8c37\u6b4c\u9ad8\u9891\u9762\u8bd5\u9898\uff1a\u7a0b\u5e8f\u68c0\u67e5               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h3>\u63cf\u8ff0<\/h3>\n<p>\u6709\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ea\u6709\u4ee5\u4e0b\u4e94\u79cd\u547d\u4ee4\uff0c\u6bcf\u79cd\u547d\u4ee4\u6700\u591a\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u8bf7\u68c0\u67e5\u7ed9\u5b9a\u7684\u7a0b\u5e8f\u662f\u5426 \u53ef\u80fd \u65e0\u9650\u5faa\u73af\u3002 \u8fd9\u4e9b\u547d\u4ee4\u5206\u522b\u662f\uff1a<\/p>\n<ol>\n<li>label &lt;string&gt;\uff1a\u58f0\u660e\u4e00\u4e2a\u6807\u7b7e\uff0c\u53c2\u6570\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4e14\u6bcf\u4e2a\u6807\u7b7e\u53ea\u58f0\u660e\u4e00\u6b21\u3002<\/li>\n<li>goto &lt;string&gt;\uff1a\u8df3\u8f6c\u5230\u4e00\u4e2a\u6807\u7b7e\uff0c\u5e76\u4ece\u6807\u7b7e\u5904\u5f00\u59cb\u6309\u987a\u5e8f\u6267\u884c\u7a0b\u5e8f\u3002<\/li>\n<li>halt\uff1a\u505c\u673a\uff0c\u7a0b\u5e8f\u7ec8\u6b62\u3002<\/li>\n<li>print &lt;string&gt;\uff1a\u6253\u5370\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u6267\u884c\u4e0b\u4e00\u4e2a\u547d\u4ee4\u3002<\/li>\n<li>gotorand &lt;label1&gt; &lt;label2&gt;\uff1a\u968f\u673a\u8df3\u8f6c\u5230\u4e24\u4e2a\u6807\u7b7e\u4e2d\u7684\u4e00\u4e2a\uff0c\u5e76\u4ece\u6807\u7b7e\u5904\u5f00\u59cb\u6309\u987a\u5e8f\u6267\u884c\u7a0b\u5e8f\u3002<\/li>\n<\/ol>\n<p>\u5f53\u7a0b\u5e8f\u6267\u884c\u5b8c\u6700\u540e\u4e00\u53e5\uff0c\u4e14\u6ca1\u6709\u8df3\u8f6c\u65f6\uff0c\u7a0b\u5e8f\u7ec8\u6b62\u3002 \u8bbe\u7ed9\u5b9a\u7684\u7a0b\u5e8f\u7684\u547d\u4ee4\u6761\u6570\u4e3a nn\uff0c1\u2264n\u22641031\u2264n\u2264103 \u3002 \u6240\u6709\u6807\u7b7e\u4e2d\u53ea\u542b\u6709\u82f1\u6587\u5b57\u7b26\uff0c\u4e14\u957f\u5ea6 mm\uff0c1\u2264m\u226451\u2264m\u22645 \u3002 \u8f93\u51fa\u547d\u4ee4\u7684\u5b57\u7b26\u4e32\u957f\u5ea6 ll\uff0c1\u2264l\u2264201\u2264l\u226420 \u3002<\/p>\n<p>\u5728\u7ebf\u8bc4\u6d4b\u5730\u5740<\/p>\n<h3>\u6837\u4f8b 1<\/h3>\n<pre><code>\u8f93\u5165\uff1a label start print \"hello world!\" gotorand start end print \"good bye\" halt label end \u8f93\u51fa\uff1a true <\/code><\/pre>\n<p>\u8bf4\u660e<\/p>\n<pre><code>\u6837\u4f8b\u4e2d\uff0c\u6bcf\u5f53\u7a0b\u5e8f\u6267\u884c\u5230\u7b2c\u4e09\u53e5 \"gotorand start end\" \u540e\uff0c\u90fd\u6709\u53ef\u80fd\u56de\u5230\u7b2c\u4e00\u53e5\uff0c\u4ece\u5934\u6267\u884c\uff0c\u4e5f\u53ef\u80fd\u8df3\u5230\u6700\u540e\u4e00\u53e5\u3002\u7b2c\u56db\uff0c\u4e94\u53e5\u5c06\u4e0d\u4f1a\u88ab\u6267\u884c\u3002 <\/code><\/pre>\n<h3>\u89e3\u9898\u601d\u8def<\/h3>\n<p>\u6211\u4eec\u53ef\u4ee5\u5c06\u6bcf\u4e00\u884c\u547d\u4ee4\u62bd\u8c61\u6210\u4e00\u4e2a\u6709\u5411\u56fe\uff0c\u7136\u540e\u5c06\u547d\u4ee4\u95f4\u7684\u8df3\u8f6c\u62bd\u8c61\u6210\u4e00\u4e2a\u6709\u5411\u8fb9\uff0c\u5982\u679c\u8fd9\u4e2a\u6709\u5411\u56fe\u4e2d\u6709\u73af\u7684\u8bdd\uff0c\u5c31\u8ba4\u4e3a\u53ef\u80fd\u4f1a\u6b7b\u5faa\u73af\u3002<\/p>\n<h3>\u4ee3\u7801\u601d\u8def<\/h3>\n<p>\u6709\u5411\u56fe\u5224\u73af\u7684\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\uff1a \u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u6709\u5411\u56fe\u662f\u5426\u6709\u73af\uff0c\u5728\u4ed6\u7684\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u6811\u4e0a\u5982\u679c\u6709\u4e00\u4e2a\u8282\u70b9\u6307\u56de\u5b83\u7684\u7956\u5148\u8282\u70b9\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6709\u5411\u56fe\u662f\u6709\u73af\u7684\u3002<\/p>\n<ul>\n<li>\u5728\u9012\u5f52\u904d\u5386\u56fe\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5b9e\u9645\u4e0a\u6709\u4e00\u4e2a\u6808\u5728\u8bb0\u5f55\u904d\u5386\u65f6\u7684\u8def\u5f84\uff0c\u90a3\u4e48\u8282\u70b9\u5c31\u6709\u4e09\u79cd\u72b6\u6001\uff1a<\/li>\n<li>0 \u4ee3\u8868\u8282\u70b9\u672a\u88ab\u8bbf\u95ee\u8fc7\u3002<\/li>\n<li>1 \u4ee3\u8868\u8282\u70b9\u5728\u6808\u4e2d\uff0c\u4e5f\u5c31\u662f\u5728\u6839\u8282\u70b9\u5230\u8be5\u8282\u70b9\u7684\u8def\u5f84\u4e0a\uff0c\u5982\u679c\u6307\u56de\u53bb\u7684\u8bdd\uff0c\u4ee3\u8868\u6709\u73af\u3002<\/li>\n<li>2 \u4ee3\u8868\u8282\u70b9\u5df2\u51fa\u6808\uff0c\u5df2\u7ecf\u8bbf\u95ee\u8fc7\u3002<\/li>\n<li>\u6240\u4ee5\u5728\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u56fe\u7684\u57fa\u7840\u4e0a\uff0c\u5982\u679c\u904d\u5386\u5230\u67d0\u4e2a\u8282\u70b9\u800c\u4e14\u8fd9\u4e2a\u8282\u70b9\u7684\u72b6\u6001\u662f 1 \u7684\u8bdd\uff0c\u4ee3\u8868\u6709\u73af<\/li>\n<\/ul>\n<h3>\u590d\u6742\u5ea6\u5206\u6790<\/h3>\n<ul>\n<li>\u8bbe\u547d\u4ee4\u6570\u4e3a N \u3002<\/li>\n<\/ul>\n<h3>\u65f6\u95f4\u590d\u6742\u5ea6<\/h3>\n<ul>\n<li>\u904d\u5386\u56fe\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(N)\u3002<\/li>\n<\/ul>\n<h3>\u7a7a\u95f4\u590d\u6742\u5ea6<\/h3>\n<ul>\n<li>\u9700\u8981\u6808\u4e0a O(N)\u7684\u5f00\u9500\u6765\u9012\u5f52\u3002<\/li>\n<\/ul>\n<pre><code>public class Solution {     \/**      * @param commands: the commands of this program.      * @return: return is the program may be in endless loop.      *\/     public boolean check(List&lt;String&gt; commands) {         int n = commands.size();         \/\/ \u6807\u7b7e\u5bf9\u5e94\u7684\u884c\u6570         Map&lt;String, Integer&gt; labelIdx = new HashMap&lt;&gt;();         \/\/ \u6bcf\u4e2a\u8282\u70b9\u7684\u8bbf\u95ee\u72b6\u6001         Map&lt;Integer, Integer&gt; visitState = new HashMap&lt;&gt;();                  for (int i = 0; i &lt; n; i++) {             if (commands.get(i).charAt(0) == 'l') {                 labelIdx.put(commands.get(i).substring(6), i);             }         }                  return dfs(0, visitState, labelIdx, commands);     }     \/**      * idx \u7684\u72b6\u6001      * 0 - \u8fd9\u4e2a\u8282\u70b9\u672a\u88ab\u8bbf\u95ee\u8fc7      * 1 - \u8fd9\u4e2a\u8282\u70b9\u88ab\u8bbf\u95ee\u8fc7\u4e14\u5728\u6808\u4e2d      * 2 - \u8fd9\u4e2a\u8282\u70b9\u5df2\u88ab\u8bbf\u95ee\u8fc7\u4e14\u5df2\u51fa\u6808      *\/     private boolean dfs(int idx,                         Map&lt;Integer, Integer&gt; visitState,                         Map&lt;String, Integer&gt; labelIdx,                         List&lt;String&gt; commands) {         \/\/ \u7a0b\u5e8f\u7ed3\u675f         if (idx == commands.size()) {             return false;         }         \/\/ \u7a0b\u5e8f\u4e2d\u6709\u73af\uff0c\u53ef\u80fd\u4f1a\u6b7b\u5faa\u73af         if (visitState.containsKey(idx) &amp;&amp; (int)visitState.get(idx) == 1) {             return true;         }         \/\/ visitState = 2, \u4ee3\u8868\u4e4b\u540e\u90fd\u4e0d\u4f1a\u6b7b\u5faa\u73af         if (visitState.containsKey(idx) &amp;&amp; (int)visitState.get(idx) == 2) {             return false;         }         \/\/ \u5c06 idx \u8282\u70b9\u52a0\u5165\u6808\u4e2d         visitState.put(idx, 1);                  boolean flag = false;                  if (commands.get(idx).charAt(0) == 'h') {             visitState.put(idx, 2);             \/\/ \u505c\u673a             return false;         }         if (commands.get(idx).charAt(0) == 'g') {             String[] parameters = commands.get(idx).split(\" \");             if (parameters.length == 2) {                 flag |= dfs(labelIdx.get(parameters[1]), visitState, labelIdx, commands);             }             else {                 flag |= dfs(labelIdx.get(parameters[1]), visitState, labelIdx, commands);                 flag |= dfs(labelIdx.get(parameters[2]), visitState, labelIdx, commands);             }         }         else {             flag |= dfs(idx + 1, visitState, labelIdx, commands);         }         visitState.put(idx, 2);         return flag;     } } <\/code><\/pre>\n<p>\u66f4\u591a\u9898\u89e3\u53c2\u8003<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8c37\u6b4c\u9ad8\u9891\u9762\u8bd5\u9898\uff1a\u7a0b\u5e8f\u68c0\u67e5 \u8cc7\u6df1\u5927\u4f6c&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/319062"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=319062"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/319062\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=319062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=319062"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=319062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}