{"id":1930,"date":"2018-03-03T21:08:04","date_gmt":"2018-03-04T05:08:04","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1930"},"modified":"2018-03-04T10:41:33","modified_gmt":"2018-03-04T18:41:33","slug":"leetcode-794-valid-tic-tac-toe-state","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-794-valid-tic-tac-toe-state\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 794. Valid Tic-Tac-Toe State"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u5224\u65ad\u4e00\u4e2a\u4e95\u5b57\u68cb\u7684\u68cb\u76d8\u662f\u5426\u6709\u6548\u3002<\/p>\n<p>A Tic-Tac-Toe board is given as a string array\u00a0<code>board<\/code>. Return True if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.<\/p>\n<p>The\u00a0<code>board<\/code>\u00a0is a 3 x 3 array, and consists of characters\u00a0<code>\" \"<\/code>,\u00a0<code>\"X\"<\/code>, and\u00a0<code>\"O\"<\/code>.\u00a0 The &#8221; &#8221; character represents an empty square.<\/p>\n<p>Here are the rules of Tic-Tac-Toe:<\/p>\n<ul>\n<li>Players take turns placing characters into empty squares (&#8221; &#8220;).<\/li>\n<li>The first player always places &#8220;X&#8221; characters, while the second player always places &#8220;O&#8221; characters.<\/li>\n<li>&#8220;X&#8221; and &#8220;O&#8221; characters are always placed into empty squares, never filled ones.<\/li>\n<li>The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal.<\/li>\n<li>The game also ends if all squares are non-empty.<\/li>\n<li>No more moves can be played if the game is over.<\/li>\n<\/ul>\n<pre class=\"\">Example 1:\r\nInput: board = [\"O\u00a0 \", \"\u00a0 \u00a0\", \"\u00a0 \u00a0\"]\r\nOutput: false\r\nExplanation: The first player always plays \"X\".\r\n\r\nExample 2:\r\nInput: board = [\"XOX\", \" X \", \"   \"]\r\nOutput: false\r\nExplanation: Players take turns making moves.\r\n\r\nExample 3:\r\nInput: board = [\"XXX\", \"   \", \"OOO\"]\r\nOutput: false\r\n\r\nExample 4:\r\nInput: board = [\"XOX\", \"O O\", \"XOX\"]\r\nOutput: true\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>board<\/code>\u00a0is a length-3 array of strings, where each string\u00a0<code>board[i]<\/code>\u00a0has length 3.<\/li>\n<li>Each\u00a0<code>board[i][j]<\/code>\u00a0is a character in the set\u00a0<code>{\" \", \"X\", \"O\"}<\/code>.<\/li>\n<\/ul>\n<p><b>Idea: Verify\u00a0all rules<\/b><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:  \r\n  bool validTicTacToe(vector&lt;string&gt;&amp; board) {    \r\n    int x = 0;\r\n    int o = 0;\r\n    int wins_x = getWins(board, 'X', x);\r\n    int wins_o = getWins(board, 'O', o);    \r\n    return wins_x + wins_o &lt;= 1 &amp;&amp; x &gt;= o + wins_x &amp;&amp; x &lt;= o + 1 - wins_o;\r\n  }\r\nprivate:\r\n  int getWins(const vector&lt;string&gt;&amp; board, char p, int&amp; count) {\r\n    vector&lt;string&gt; rows(3);\r\n    string diag1, diag2;\r\n    for (int i = 0; i &lt; 3; ++i) {\r\n      diag1 += board[i][i];\r\n      diag2 += board[i][2 - i];\r\n      for (int j = 0; j &lt; 3; ++j) {\r\n        char c = board[i][j];\r\n        rows[j] += c;\r\n        count += c == p;\r\n      }\r\n    }    \r\n    string win(3, p);\r\n    int wins = (diag1 == win) + (diag2 == win);\r\n    for (int i = 0; i &lt; 3; ++i) {\r\n      wins += rows[i] == win;\r\n      wins += board[i] == win;\r\n    }\r\n    return wins;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u5224\u65ad\u4e00\u4e2a\u4e95\u5b57\u68cb\u7684\u68cb\u76d8\u662f\u5426\u6709\u6548\u3002 A Tic-Tac-Toe board is given as a string array\u00a0board. Return True if and only if it is possible to reach this board position during&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,47],"tags":[27,177,4],"class_list":["post-1930","post","type-post","status-publish","format-standard","hentry","category-simulation","category-string","tag-game","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1930","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1930"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1930\/revisions"}],"predecessor-version":[{"id":1932,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1930\/revisions\/1932"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}