{"id":490,"date":"2021-03-02T21:22:29","date_gmt":"2021-03-02T13:22:29","guid":{"rendered":"https:\/\/www.wangliguang.cn\/?p=490"},"modified":"2021-03-02T21:22:29","modified_gmt":"2021-03-02T13:22:29","slug":"%e3%80%90java%e9%a2%98%e8%a7%a3%e3%80%91%e5%89%91%e6%8c%87-offer-12-%e7%9f%a9%e9%98%b5%e4%b8%ad%e7%9a%84%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/wangliguang.cn\/?p=490","title":{"rendered":"\u3010Java\u9898\u89e3\u3011\u5251\u6307 Offer 12. \u77e9\u9635\u4e2d\u7684\u8def\u5f84"},"content":{"rendered":"<h2>\u4e00\u3001\u9898\u76ee<\/h2>\n<blockquote>\n<p>\u8bf7\u8bbe\u8ba1\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u6765\u5224\u65ad\u5728\u4e00\u4e2a\u77e9\u9635\u4e2d\u662f\u5426\u5b58\u5728\u4e00\u6761\u5305\u542b\u67d0\u5b57\u7b26\u4e32\u6240\u6709\u5b57\u7b26\u7684\u8def\u5f84\u3002\u8def\u5f84\u53ef\u4ee5\u4ece\u77e9\u9635\u4e2d\u7684\u4efb\u610f\u4e00\u683c\u5f00\u59cb\uff0c\u6bcf\u4e00\u6b65\u53ef\u4ee5\u5728\u77e9\u9635\u4e2d\u5411\u5de6\u3001\u53f3\u3001\u4e0a\u3001\u4e0b\u79fb\u52a8\u4e00\u683c\u3002\u5982\u679c\u4e00\u6761\u8def\u5f84\u7ecf\u8fc7\u4e86\u77e9\u9635\u7684\u67d0\u4e00\u683c\uff0c\u90a3\u4e48\u8be5\u8def\u5f84\u4e0d\u80fd\u518d\u6b21\u8fdb\u5165\u8be5\u683c\u5b50\u3002\u4f8b\u5982\uff0c\u5728\u4e0b\u9762\u76843\u00d74\u7684\u77e9\u9635\u4e2d\u5305\u542b\u4e00\u6761\u5b57\u7b26\u4e32\u201cbfce\u201d\u7684\u8def\u5f84\uff08\u8def\u5f84\u4e2d\u7684\u5b57\u6bcd\u7528\u52a0\u7c97\u6807\u51fa\uff09\u3002<\/p>\n<p>[[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;e&quot;], [&quot;s&quot;,&quot;f&quot;,&quot;c&quot;,&quot;s&quot;], [&quot;a&quot;,&quot;d&quot;,&quot;e&quot;,&quot;e&quot;]]<\/p>\n<p>\u4f46\u77e9\u9635\u4e2d\u4e0d\u5305\u542b\u5b57\u7b26\u4e32\u201cabfb\u201d\u7684\u8def\u5f84\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26b\u5360\u636e\u4e86\u77e9\u9635\u4e2d\u7684\u7b2c\u4e00\u884c\u7b2c\u4e8c\u4e2a\u683c\u5b50\u4e4b\u540e\uff0c\u8def\u5f84\u4e0d\u80fd\u518d\u6b21\u8fdb\u5165\u8fd9\u4e2a\u683c\u5b50\u3002<\/p>\n<p>\u00a0<\/p>\n<p>\u793a\u4f8b 1\uff1a<br \/>\n\u8f93\u5165\uff1aboard = [[&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;E&quot;],[&quot;S&quot;,&quot;F&quot;,&quot;C&quot;,&quot;S&quot;],[&quot;A&quot;,&quot;D&quot;,&quot;E&quot;,&quot;E&quot;]],<br \/>\nword = &quot;ABCCED&quot;<br \/>\n\u8f93\u51fa\uff1atrue <\/p>\n<p>\u793a\u4f8b 2\uff1a<br \/>\n\u8f93\u5165\uff1aboard = [[&quot;a&quot;,&quot;b&quot;],[&quot;c&quot;,&quot;d&quot;]], word = &quot;abcd&quot; \u00a0<br \/>\n\u8f93\u51fa\uff1afalse<br \/>\n\u63d0\u793a\uff1a<\/p>\n<p>1 &lt;= board.length &lt;= 200<br \/>\n1 &lt;= board[i].length &lt;= 200<\/p>\n<\/blockquote>\n<h2>\u4e8c\u3001\u4ee3\u7801<\/h2>\n<pre><code class=\"language-Java\">class Solution {\n    private int rowl, coll;\n    private boolean[][] visited;\n    private int[][] direction;\n    public boolean exist(char[][] board, String word) {\n        if(board == null || word == null || board.length == 0 || board[0].length == 0){\n            return false;\n        }\n        rowl = board.length;\n        coll = board[0].length;\n        visited = new boolean[rowl][coll];\n        direction = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n        char[] mywords = word.toCharArray();\n        for(int i = 0; i &amp;lt; rowl; i++){\n            for(int j = 0; j &amp;lt; coll; j++){\n                visited[i][j] = false;\n            }\n        }\n        for(int row = 0; row &amp;lt; rowl; row++){\n            for(int col = 0; col &amp;lt; coll; col++){\n                boolean res = dfs(board, mywords, row, col, 0);\n                if(res){\n                    return true;\n                }\n            }\n        }\n        return false;\n    }\n    public boolean dfs(char[][] board, char[] mywords, int row, int col, int index){\n        if(visited[row][col] || board[row][col] != mywords[index]){\n            return false;\n        }\n        if(index == mywords.length - 1){\n            return true;\n        }\n        visited[row][col] = true;\n        boolean result = false;\n        for(int[] dir : direction){\n            int newr = row + dir[0];\n            int newc = col + dir[1];\n            if(newr &amp;gt;= 0 &amp;amp;&amp;amp; newr &amp;lt; rowl &amp;amp;&amp;amp; newc &amp;gt;= 0 &amp;amp;&amp;amp; newc &amp;lt;coll &amp;amp;&amp;amp; !visited[newr][newc]){\n                boolean res2 = dfs(board, mywords, newr, newc, index+1);\n                if(res2){\n                    return true;\n                }\n            }\n        }\n        visited[row][col] = false;\n        return false;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u9898\u76ee \u8bf7\u8bbe\u8ba1\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u6765\u5224\u65ad\u5728\u4e00\u4e2a\u77e9\u9635\u4e2d\u662f\u5426\u5b58\u5728\u4e00\u6761\u5305\u542b\u67d0\u5b57\u7b26\u4e32\u6240\u6709\u5b57\u7b26\u7684\u8def\u5f84\u3002\u8def\u5f84\u53ef\u4ee5\u4ece\u77e9\u9635\u4e2d\u7684\u4efb\u610f\u4e00&hellip; <a href=\"https:\/\/wangliguang.cn\/?p=490\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">\u3010Java\u9898\u89e3\u3011\u5251\u6307 Offer 12. \u77e9\u9635\u4e2d\u7684\u8def\u5f84<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,23],"tags":[],"class_list":["post-490","post","type-post","status-publish","format-standard","hentry","category-java","category-23"],"_links":{"self":[{"href":"https:\/\/wangliguang.cn\/index.php?rest_route=\/wp\/v2\/posts\/490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wangliguang.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wangliguang.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wangliguang.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wangliguang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=490"}],"version-history":[{"count":0,"href":"https:\/\/wangliguang.cn\/index.php?rest_route=\/wp\/v2\/posts\/490\/revisions"}],"wp:attachment":[{"href":"https:\/\/wangliguang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wangliguang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wangliguang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}