{"id":5946,"date":"2019-12-08T02:56:30","date_gmt":"2019-12-08T10:56:30","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5946"},"modified":"2019-12-09T20:30:11","modified_gmt":"2019-12-10T04:30:11","slug":"leetcode-1284-minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1284-minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1284. Minimum Number of Flips to Convert Binary Matrix to Zero - \u5237\u9898\u627e\u5de5\u4f5c EP283\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/oxTtbZ6t-F0?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>\n<\/div><\/figure>\n\n\n\n<p>Given a&nbsp;<code>m x n<\/code>&nbsp;binary matrix&nbsp;<code>mat<\/code>. In one step, you can choose one cell and flip it and all the four neighbours of it&nbsp;if they exist (Flip is changing 1 to 0 and 0 to 1). A pair of cells are called neighboors if they share one edge.<\/p>\n\n\n\n<p>Return the&nbsp;<em>minimum number of steps<\/em>&nbsp;required to convert&nbsp;<code>mat<\/code>&nbsp;to a zero matrix or&nbsp;<strong>-1<\/strong>&nbsp;if you cannot.<\/p>\n\n\n\n<p>Binary matrix is a matrix with all cells equal to 0 or 1 only.<\/p>\n\n\n\n<p>Zero matrix is a matrix with all cells equal to 0.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/11\/28\/matrix.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[0,0],[0,1]]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> One possible solution is to flip (1, 0) then (0, 1) and finally (1, 1) as shown.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[0]]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> Given matrix is a zero matrix. We don't need to change it.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[1,1,1],[1,0,1],[0,0,0]]\n<strong>Output:<\/strong> 6\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[1,0,0],[1,0,0]]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> Given matrix can't be a zero matrix\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m ==&nbsp;mat.length<\/code><\/li><li><code>n ==&nbsp;mat[0].length<\/code><\/li><li><code>1 &lt;= m&nbsp;&lt;= 3<\/code><\/li><li><code>1 &lt;= n&nbsp;&lt;= 3<\/code><\/li><li><code>mat[i][j]<\/code>&nbsp;is 0 or 1.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: BFS + bitmask<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1284-ep283.png\" alt=\"\" class=\"wp-image-5954\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1284-ep283.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1284-ep283-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1284-ep283-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Time complexity: O(2^(m*n))<br>Space complexity: O(2^(m*n))<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minFlips(vector<vector<int>>& mat) {    \n    const int m = mat.size();\n    const int n = mat[0].size();\n    const vector<int> dirs{0, 1, 0, -1, 0, 0};\n    \n    auto flip = [&](int s, int x, int y) { \n      for (int i = 0; i < 5; ++i) {\n        const int tx = x + dirs[i];\n        const int ty = y + dirs[i + 1];\n        if (tx < 0 || tx >= n || ty < 0 || ty >= m) continue;\n        s ^= (1 << ty * n + tx);\n      }\n      return s;\n    };\n    \n    int steps = 0;\n    queue<int> q;\n    vector<int> seen(1 << (n * m));\n    int start = 0;\n    for (int y = 0; y < m; ++y)\n      for (int x = 0; x < n; ++x)\n        start |= (mat[y][x] << (y * n + x));\n    q.push(start);\n    seen[start] = 1;\n    \n    while (!q.empty()) {\n      int size = q.size();\n      while (size--) {\n        int s = q.front();\n        if (s == 0) return steps;\n        q.pop();\n        for (int y = 0; y < m; ++y)\n          for (int x = 0; x < n; ++x) {\n            int t = flip(s, x, y);\n            if (seen[t]) continue;            \n            seen[t] = 1;\n            q.push(t);\n          }\n      }\n      ++steps;\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;m x n&nbsp;binary matrix&nbsp;mat. In one step, you can choose one cell and flip it and all the four neighbours of it&nbsp;if they exist&#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":[34,217,42],"class_list":["post-5946","post","type-post","status-publish","format-standard","hentry","category-searching","tag-bfs","tag-hard","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5946","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=5946"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5946\/revisions"}],"predecessor-version":[{"id":5955,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5946\/revisions\/5955"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}