{"id":10315,"date":"2025-04-10T20:34:14","date_gmt":"2025-04-11T03:34:14","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10315"},"modified":"2025-04-10T20:36:04","modified_gmt":"2025-04-11T03:36:04","slug":"leetcode-1992-find-all-groups-of-farmland","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1992-find-all-groups-of-farmland\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1992. Find All Groups of Farmland"},"content":{"rendered":"\n<p>\u5df2\u7ecf\u544a\u8bc9\u4f60\u6240\u6709\u7684\u519c\u7530\u90fd\u662f\u89c4\u6574\u7684\u77e9\u5f62\uff0c\u9898\u76ee\u96be\u5ea6\u4e00\u4e0b\u5b50\u964d\u4f4e\u4e86\u3002<br>\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u519c\u7530\u7684\u5de6\u4e0a\u89d2\uff0c\u627e\u5230\u5b83\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u5373\u53ef\u3002<br>\u7136\u540e\u628a\u6574\u4e2a\u519c\u7530\u77e9\u5f62\u6807\u8bb0\u4e3a\u6797\u5730\/\u975e\u519c\u7530\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(m*n) \u6bcf\u4e2a\u683c\u5b50\u6700\u591a\u904d\u53863\u904d\u3002<br>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(1)<\/p>\n\n\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  vector<vector<int>> findFarmland(vector<vector<int>>& land) {\n    const int m = land.size();\n    const int n = land[0].size();\n    vector<vector<int>> ans;\n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < n; ++j) {\n        if (!land[i][j]) continue;\n        int w = 0;\n        int h = 0;\n        while (j + w < n &#038;&#038; land[i][j + w]) ++w;\n        while (i + h < m &#038;&#038; land[i + h][j]) ++h;\n        ans.push_back({i, j, i + h - 1, j + w - 1});\n        for (int p = 0; p < h; ++p)\n          for (int q = 0; q < w; ++q)\n            land[i + p][j + q] = 0; \/\/ mark as seen.\n      }\n    return ans;\n  }\n};\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5df2\u7ecf\u544a\u8bc9\u4f60\u6240\u6709\u7684\u519c\u7530\u90fd\u662f\u89c4\u6574\u7684\u77e9\u5f62\uff0c\u9898\u76ee\u96be\u5ea6\u4e00\u4e0b\u5b50\u964d\u4f4e\u4e86\u3002\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u519c\u7530\u7684\u5de6\u4e0a\u89d2\uff0c\u627e\u5230\u5b83\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u5373\u53ef\u3002\u7136\u540e\u628a\u6574\u4e2a\u519c\u7530\u77e9\u5f62\u6807\u8bb0\u4e3a\u6797\u5730\/\u975e\u519c\u7530\u5373\u53ef\u3002 \u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(m*n) \u6bcf\u4e2a\u683c\u5b50\u6700\u591a\u904d\u53863\u904d\u3002\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(1) class Solution { public: vector findFarmland(vector&#038; land) { const int m = land.size(); const int n = land[0].size(); vector ans; for (int&#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],"tags":[278,177,179],"class_list":["post-10315","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-grid","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10315","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=10315"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10315\/revisions"}],"predecessor-version":[{"id":10321,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10315\/revisions\/10321"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}