{"id":612,"date":"2017-10-16T19:56:40","date_gmt":"2017-10-17T02:56:40","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=612"},"modified":"2018-04-19T08:37:52","modified_gmt":"2018-04-19T15:37:52","slug":"leetcode-37-sudoku-solver","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-37-sudoku-solver\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 37. Sudoku Solver"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 37. Sudoku Solver - \u5237\u9898\u627e\u5de5\u4f5c EP89\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/ucugbKwjtRs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Write a program to solve a Sudoku puzzle by filling the empty cells.<\/p>\n<p>Empty cells are indicated by the character\u00a0<code>'.'<\/code>.<\/p>\n<p>You may assume that there will be only one unique solution.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/f\/ff\/Sudoku-by-L2G-20050714.svg\/250px-Sudoku-by-L2G-20050714.svg.png\" \/><\/p>\n<p>A sudoku puzzle&#8230;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/3\/31\/Sudoku-by-L2G-20050714_solution.svg\/250px-Sudoku-by-L2G-20050714_solution.svg.png\" \/><\/p>\n<p>&#8230;and its solution numbers marked in red.<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>DFS + backtracking<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/37-ep89.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-619\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/37-ep89.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/37-ep89.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/37-ep89-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/37-ep89-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/37-ep89-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 3 ms\r\nclass Solution {\r\npublic:\r\n    void solveSudoku(vector&lt;vector&lt;char&gt;&gt;&amp; board) {\r\n        rows_ = vector&lt;vector&lt;int&gt;&gt;(9, vector&lt;int&gt;(10));\r\n        cols_ = vector&lt;vector&lt;int&gt;&gt;(9, vector&lt;int&gt;(10));\r\n        boxes_ = vector&lt;vector&lt;int&gt;&gt;(9, vector&lt;int&gt;(10));\r\n        \r\n        for(int i = 0; i &lt; 9; i++)\r\n            for(int j = 0; j &lt; 9; j++) {\r\n                const char c = board[i][j];                \r\n                if ( c != '.') {\r\n                    int n = c - '0';                    \r\n                    int bx = j \/ 3;\r\n                    int by = i \/ 3;\r\n                    rows_[i][n] = 1;\r\n                    cols_[j][n] = 1;\r\n                    boxes_[by * 3 + bx][n] = 1;\r\n                }\r\n            }\r\n        \r\n        fill(board, 0, 0);\r\n    }\r\n    \r\nprivate:\r\n    vector&lt;vector&lt;int&gt;&gt; rows_, cols_, boxes_;\r\n    \r\n    bool fill(vector&lt;vector&lt;char&gt;&gt;&amp; board, int x, int y) {\r\n        if (y == 9)\r\n            return true;\r\n        \r\n        int nx = (x + 1) % 9;\r\n        int ny = (nx == 0) ? y + 1 : y;\r\n        \r\n        if (board[y][x] != '.') return fill(board, nx, ny);\r\n        \r\n        for (int i = 1; i &lt;= 9; i++) {\r\n            int bx = x \/ 3;\r\n            int by = y \/ 3;\r\n            int box_key = by * 3 + bx;\r\n            if (!rows_[y][i] &amp;&amp; !cols_[x][i] &amp;&amp; !boxes_[box_key][i]) {\r\n                rows_[y][i] = 1;\r\n                cols_[x][i] = 1;\r\n                boxes_[box_key][i] = 1;\r\n                board[y][x] = i + '0';\r\n                if (fill(board, nx, ny)) return true;\r\n                board[y][x] = '.';\r\n                boxes_[box_key][i] = 0;\r\n                cols_[x][i] = 0;\r\n                rows_[y][i] = 0;\r\n            }\r\n        }\r\n        return false;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-51-n-queens\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 51. N-Queens<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character\u00a0&#8216;.&#8217;. You may assume that&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[33,131],"class_list":["post-612","post","type-post","status-publish","format-standard","hentry","category-searching","tag-dfs","tag-pruning","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/612","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=612"}],"version-history":[{"count":9,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/612\/revisions"}],"predecessor-version":[{"id":2655,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/612\/revisions\/2655"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}