| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.FindAllGroupOfFarmland; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class FindAllGroupOfFarmlandBruteForce : IFindAllGroupOfFarmland |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(m * n) |
| | 19 | | /// Space complexity - O(k), where k is the number of distinct rectangles found |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="land"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int[][] FindFarmland(int[][] land) |
| 6 | 24 | | { |
| 6 | 25 | | var result = new List<int[]>(); |
| | 26 | |
|
| 42 | 27 | | for (var i = 0; i < land.Length; i++) |
| 15 | 28 | | { |
| 124 | 29 | | for (var j = 0; j < land[i].Length; j++) |
| 47 | 30 | | { |
| 47 | 31 | | if (land[i][j] != 1) |
| 41 | 32 | | { |
| 41 | 33 | | continue; |
| | 34 | | } |
| | 35 | |
|
| 6 | 36 | | FindFarmland(land, result, i, j, i, j); |
| 6 | 37 | | } |
| 15 | 38 | | } |
| | 39 | |
|
| 6 | 40 | | return [.. result]; |
| 6 | 41 | | } |
| | 42 | |
|
| | 43 | | private static void FindFarmland(IReadOnlyList<int[]> land, ICollection<int[]> result, int r1, int c1, int r2, |
| | 44 | | int c2) |
| 6 | 45 | | { |
| 12 | 46 | | while (true) |
| 12 | 47 | | { |
| 12 | 48 | | if (r2 + 1 < land.Count && land[r2 + 1][c2] == 1 && c2 + 1 < land[r2].Length && land[r2][c2 + 1] == 1) |
| 4 | 49 | | { |
| 4 | 50 | | MarkAsVisited(land, r1, c1, r2 + 1, c2 + 1); |
| | 51 | |
|
| 4 | 52 | | r2 += 1; |
| 4 | 53 | | c2 += 1; |
| | 54 | |
|
| 4 | 55 | | continue; |
| | 56 | | } |
| | 57 | |
|
| 8 | 58 | | if (r2 + 1 < land.Count && land[r2 + 1][c2] == 1) |
| 1 | 59 | | { |
| 1 | 60 | | MarkAsVisited(land, r1, c1, r2 + 1, c2); |
| | 61 | |
|
| 1 | 62 | | r2 += 1; |
| | 63 | |
|
| 1 | 64 | | continue; |
| | 65 | | } |
| | 66 | |
|
| 7 | 67 | | if (c2 + 1 < land[r2].Length && land[r2][c2 + 1] == 1) |
| 1 | 68 | | { |
| 1 | 69 | | MarkAsVisited(land, r1, c1, r2, c2 + 1); |
| | 70 | |
|
| 1 | 71 | | c2 += 1; |
| | 72 | |
|
| 1 | 73 | | continue; |
| | 74 | | } |
| | 75 | |
|
| 6 | 76 | | result.Add([r1, c1, r2, c2]); |
| | 77 | |
|
| 6 | 78 | | break; |
| | 79 | | } |
| 6 | 80 | | } |
| | 81 | |
|
| | 82 | | private static void MarkAsVisited(IReadOnlyList<int[]> land, int r1, int c1, int r2, int c2) |
| 6 | 83 | | { |
| 36 | 84 | | for (var i = r1; i <= r2; i++) |
| 12 | 85 | | { |
| 74 | 86 | | for (var j = c1; j <= c2; j++) |
| 25 | 87 | | { |
| 25 | 88 | | land[i][j] = -1; |
| 25 | 89 | | } |
| 12 | 90 | | } |
| 6 | 91 | | } |
| | 92 | | } |