{"id":10032,"date":"2023-04-30T08:30:49","date_gmt":"2023-04-30T15:30:49","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10032"},"modified":"2023-04-30T08:34:31","modified_gmt":"2023-04-30T15:34:31","slug":"leetcode-2658-maximum-number-of-fish-in-a-grid","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-2658-maximum-number-of-fish-in-a-grid\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2658. Maximum Number of Fish in a Grid"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D matrix&nbsp;<code>grid<\/code>&nbsp;of size&nbsp;<code>m x n<\/code>, where&nbsp;<code>(r, c)<\/code>&nbsp;represents:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A&nbsp;<strong>land<\/strong>&nbsp;cell if&nbsp;<code>grid[r][c] = 0<\/code>, or<\/li><li>A&nbsp;<strong>water<\/strong>&nbsp;cell containing&nbsp;<code>grid[r][c]<\/code>&nbsp;fish, if&nbsp;<code>grid[r][c] &gt; 0<\/code>.<\/li><\/ul>\n\n\n\n<p>A fisher can start at any&nbsp;<strong>water<\/strong>&nbsp;cell&nbsp;<code>(r, c)<\/code>&nbsp;and can do the following operations any number of times:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Catch all the fish at cell&nbsp;<code>(r, c)<\/code>, or<\/li><li>Move to any adjacent&nbsp;<strong>water<\/strong>&nbsp;cell.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;number of fish the fisher can catch if he chooses his starting cell optimally, or&nbsp;<\/em><code>0<\/code>&nbsp;if no water cell exists.<\/p>\n\n\n\n<p>An&nbsp;<strong>adjacent<\/strong>&nbsp;cell of the cell&nbsp;<code>(r, c)<\/code>, is one of the cells&nbsp;<code>(r, c + 1)<\/code>,&nbsp;<code>(r, c - 1)<\/code>,&nbsp;<code>(r + 1, c)<\/code>&nbsp;or&nbsp;<code>(r - 1, c)<\/code>&nbsp;if it exists.<\/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\/2023\/03\/29\/example.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> The fisher can start at cell <code>(1,3)<\/code> and collect 3 fish, then move to cell <code>(2,3)<\/code>&nbsp;and collect 4 fish.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2023\/03\/29\/example2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The fisher can start at cells (0,0) or (3,3) and collect a single fish. \n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == grid.length<\/code><\/li><li><code>n == grid[i].length<\/code><\/li><li><code>1 &lt;= m, n &lt;= 10<\/code><\/li><li><code>0 &lt;= grid[i][j] &lt;= 10<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Connected Component<\/strong><\/h2>\n\n\n\n<p>Similar to <a href=\"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-695-max-area-of-island\/\">\u82b1\u82b1\u9171 LeetCode 695. Max Area of Island<\/a><\/p>\n\n\n\n<p>Find the connected component that has the max sum.<\/p>\n\n\n\n<p>Time complexity: O(mn)<br>Space complexity: O(mn)<\/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 findMaxFish(vector<vector<int>>& grid) {\n    const int m = grid.size();\n    const int n = grid[0].size();\n    function<int(int, int)> dfs = [&](int r, int c) {\n      if (r < 0 || r >= m || c < 0 || c >= n || !grid[r][c]) return 0;      \n      return exchange(grid[r][c], 0) \n        + dfs(r - 1, c) + dfs(r, c - 1) + dfs(r + 1, c) + dfs(r, c + 1);\n    };\n    int ans = 0;\n    for (int r = 0; r < m; ++r)\n      for (int c = 0; c < n; ++c)\n        ans = max(ans, dfs(r, c));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;2D matrix&nbsp;grid&nbsp;of size&nbsp;m x n, where&nbsp;(r, c)&nbsp;represents: A&nbsp;land&nbsp;cell if&nbsp;grid[r][c] = 0, or A&nbsp;water&nbsp;cell containing&nbsp;grid[r][c]&nbsp;fish, if&nbsp;grid[r][c] &gt; 0. A fisher can start at&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[102,33,77,177],"class_list":["post-10032","post","type-post","status-publish","format-standard","hentry","category-graph","tag-connected-components","tag-dfs","tag-graph","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10032","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=10032"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10032\/revisions"}],"predecessor-version":[{"id":10035,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10032\/revisions\/10035"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}