代码拉取完成,页面将自动刷新
<p>There is a game dungeon comprised of <code>n x n</code> rooms arranged in a grid.</p>
<p>You are given a 2D array <code>fruits</code> of size <code>n x n</code>, where <code>fruits[i][j]</code> represents the number of fruits in the room <code>(i, j)</code>. Three children will play in the game dungeon, with <strong>initial</strong> positions at the corner rooms <code>(0, 0)</code>, <code>(0, n - 1)</code>, and <code>(n - 1, 0)</code>.</p>
<p>The children will make <strong>exactly</strong> <code>n - 1</code> moves according to the following rules to reach the room <code>(n - 1, n - 1)</code>:</p>
<ul>
<li>The child starting from <code>(0, 0)</code> must move from their current room <code>(i, j)</code> to one of the rooms <code>(i + 1, j + 1)</code>, <code>(i + 1, j)</code>, and <code>(i, j + 1)</code> if the target room exists.</li>
<li>The child starting from <code>(0, n - 1)</code> must move from their current room <code>(i, j)</code> to one of the rooms <code>(i + 1, j - 1)</code>, <code>(i + 1, j)</code>, and <code>(i + 1, j + 1)</code> if the target room exists.</li>
<li>The child starting from <code>(n - 1, 0)</code> must move from their current room <code>(i, j)</code> to one of the rooms <code>(i - 1, j + 1)</code>, <code>(i, j + 1)</code>, and <code>(i + 1, j + 1)</code> if the target room exists.</li>
</ul>
<p>When a child enters a room, they will collect all the fruits there. If two or more children enter the same room, only one child will collect the fruits, and the room will be emptied after they leave.</p>
<p>Return the <strong>maximum</strong> number of fruits the children can collect from the dungeon.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">fruits = [[1,2,3,4],[5,6,8,7],[9,10,11,12],[13,14,15,16]]</span></p>
<p><strong>Output:</strong> <span class="example-io">100</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/10/15/example_1.gif" style="width: 250px; height: 214px;" /></p>
<p>In this example:</p>
<ul>
<li>The 1<sup>st</sup> child (green) moves on the path <code>(0,0) -> (1,1) -> (2,2) -> (3, 3)</code>.</li>
<li>The 2<sup>nd</sup> child (red) moves on the path <code>(0,3) -> (1,2) -> (2,3) -> (3, 3)</code>.</li>
<li>The 3<sup>rd</sup> child (blue) moves on the path <code>(3,0) -> (3,1) -> (3,2) -> (3, 3)</code>.</li>
</ul>
<p>In total they collect <code>1 + 6 + 11 + 16 + 4 + 8 + 12 + 13 + 14 + 15 = 100</code> fruits.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">fruits = [[1,1],[1,1]]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>In this example:</p>
<ul>
<li>The 1<sup>st</sup> child moves on the path <code>(0,0) -> (1,1)</code>.</li>
<li>The 2<sup>nd</sup> child moves on the path <code>(0,1) -> (1,1)</code>.</li>
<li>The 3<sup>rd</sup> child moves on the path <code>(1,0) -> (1,1)</code>.</li>
</ul>
<p>In total they collect <code>1 + 1 + 1 + 1 = 4</code> fruits.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n == fruits.length == fruits[i].length <= 1000</code></li>
<li><code>0 <= fruits[i][j] <= 1000</code></li>
</ul>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。