{"id":150717,"date":"2020-09-03T02:11:35","date_gmt":"2020-09-02T18:11:35","guid":{"rendered":"http:\/\/4563.org\/?p=150717"},"modified":"2020-09-03T02:11:35","modified_gmt":"2020-09-02T18:11:35","slug":"%e4%b8%80%e9%81%93%e4%b8%ad%e7%ad%89%e9%9a%be%e5%ba%a6%e9%a2%98-%e8%a2%ab%e5%9b%b4%e7%bb%95%e7%9a%84%e5%8c%ba%e5%9f%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=150717","title":{"rendered":"[\u4e00\u9053\u4e2d\u7b49\u96be\u5ea6\u9898] \u88ab\u56f4\u7ed5\u7684\u533a\u57df"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  [\u4e00\u9053\u4e2d\u7b49\u96be\u5ea6\u9898] \u88ab\u56f4\u7ed5\u7684\u533a\u57df               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 16<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u7ed9\u4e00\u4e2a\u4e8c\u7ef4\u7684\u77e9\u9635\uff0c\u5305\u542b &#8216;X&#8217; \u548c &#8216;O&#8217;, \u627e\u5230\u6240\u6709\u88ab &#8216;X&#8217; \u56f4\u7ed5\u7684\u533a\u57df\uff0c\u5e76\u7528 &#8216;X&#8217; \u66ff\u6362\u5176\u4e2d\u6240\u6709\u7684 &#8216;O&#8217;\u3002<\/p>\n<p>\u70b9\u6b64\u5728\u7ebf\u505a\u9898<\/p>\n<h2>\u6837\u4f8b 1:<\/h2>\n<pre><code>\u8f93\u5165:   X X X X   X O O X   X X O X   X O X X \u8f93\u51fa:    X X X X   X X X X   X X X X   X O X X <\/code><\/pre>\n<h2>\u6837\u4f8b 2:<\/h2>\n<pre><code>\u8f93\u5165:    X X X X   X O O X   X O O X   X O X X \u8f93\u51fa:    X X X X   X O O X   X O O X   X O X X <\/code><\/pre>\n<h2>[\u9898\u89e3]<\/h2>\n<p>\u7b97\u6cd5\uff1aDFS \u601d\u8def\uff1a\u5148\u4ece\u5916\u56f4\u5f00\u59cb\uff0c\u53ea\u6709\u8eab\u5904\u5916\u56f4\u7684 O \u662f\u65e0\u6cd5\u88ab\u5305\u56f4\u7684\uff0c\u540c\u7406\uff0c\u4e0e\u4e00\u4e2a\u65e0\u6cd5\u88ab\u5305\u56f4\u7684 O \u76f8\u8fde\u7684 O \u4e5f\u662f\u65e0\u6cd5\u88ab\u5305\u56f4\u7684\u3002<\/p>\n<ul>\n<li>\u4ece\u5916\u56f4\u5f00\u59cb\uff0c\u82e5\u9047\u5230\u8fb9\u754c\u7684 O\uff0c\u5219\u5c06\u5176\u6807\u8bb0\u4e3a <em>\uff0c\u610f\u5473\u65e0\u6cd5\u88ab\u5305\u56f4\u7684 O \u4f9d\u6b21\u5bf9\u5916\u5708\u7684\u201cO\u201d\u7684\u56db\u4e2a\u65b9\u5411\u505a DFS\uff0c\u5c06\u5176\u53ef\u4ee5\u5230\u8fbe O \u4e34\u65f6\u8bbe\u7f6e\u4e3a<\/em><\/li>\n<li>\u6700\u540e\uff0c\u904d\u5386\u6570\u7ec4\uff0c \u5c06\u65e0\u6cd5\u88ab\u5305\u56f4\u7684 O\uff0c\u4e5f\u5c31\u662f\u6b64\u65f6\u88ab\u6807\u8bb0\u4e3a * \u6539\u4e3a O \uff1b X \u4e0d\u53d8\uff1b \u5c06\u53ef\u4ee5\u88ab\u5305\u56f4\u7684\u533a\u57df\uff0c\u5373\u6ca1\u88ab\u6807\u8bb0\u8fc7\u7684 O \u53d8\u4e3a X<\/li>\n<\/ul>\n<h2>\u590d\u6742\u5ea6\u5206\u6790<\/h2>\n<ul>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6 O(n*m) \u641c\u7d22\u4e00\u6b21\u56fe\u5373\u53ef<\/li>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6 O(n*m) \u56fe\u7684\u5927\u5c0f<\/li>\n<\/ul>\n<pre><code>public class Solution {     \/**      * @param board a 2D board containing 'X' and 'O'      * @return void      *\/     public void surroundedRegions(char[][] board) {         if (board == null || board.length == 0 || board[0].length == 0) {             return ;         }         int n = board.length,m = board[0].length;         \/\/\u56db\u5468\u5411\u4e2d\u95f4\u641c         for (int i = 0; i &lt; n;i++) {             for (int j = 0;j &lt; m;j++) {                 if (!is_border(i,j,n,m)) {                     continue;                 }                 if (board[i][j] == 'O') {                     dfs(board , i , j);                 }             }         }                  \/\/\u904d\u5386\u56fe\uff0c\u66f4\u65b0\u7ed3\u679c         for (int i = 0;i &lt; n;i++) {             for (int j = 0;j &lt; m;j++) {                 if (board[i][j] == 'O') {                     board[i][j] = 'X';                 }                 else if (board[i][j] == '*') {                     board[i][j] = 'O';                  }             }         }     }          public void dfs(char[][] board , int x , int y){         \/\/\u4fdd\u8bc1\u5f53\u524d\u70b9\u5728\u56fe\u4e2d         if (x &lt; 0 || y &lt; 0 || x &gt;= board.length || y &gt;= board[0].length) {             return ;         }         if (board[x][y] != 'O') {             return ;         }         board[x][y] = '*';         dfs(board , x + 1, y);         dfs(board , x - 1, y);         dfs(board , x , y + 1);         dfs(board , x , y - 1);     }     public boolean is_border(int x,int y,int n,int m) {\/\/\u5224\u65ad\u662f\u5426\u8fb9\u754c         if (x == 0 || y == 0 || x == n - 1||y == m - 1) {             return true;         }         return false;     } } <\/code><\/pre>\n<p>\u66f4\u591a\u9898\u89e3\u53c2\u89c1<\/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>[\u4e00\u9053\u4e2d\u7b49\u96be\u5ea6\u9898] \u88ab\u56f4\u7ed5\u7684\u533a\u57df &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\/150717"}],"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=150717"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/150717\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=150717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=150717"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=150717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}