| | 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.CountUnguardedCellsInTheGrid; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class CountUnguardedCellsInTheGridSimulation : ICountUnguardedCellsInTheGrid |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(m * n) |
| | 19 | | /// Space complexity - O(m * n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="m"></param> |
| | 22 | | /// <param name="n"></param> |
| | 23 | | /// <param name="guards"></param> |
| | 24 | | /// <param name="walls"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | public int CountUnguarded(int m, int n, int[][] guards, int[][] walls) |
| 2 | 27 | | { |
| 2 | 28 | | var result = m * n; |
| | 29 | |
|
| 2 | 30 | | var cells = new int[m, n]; |
| | 31 | |
|
| 14 | 32 | | foreach (var guard in guards) |
| 4 | 33 | | { |
| 4 | 34 | | var row = guard[0]; |
| 4 | 35 | | var col = guard[1]; |
| | 36 | |
|
| 4 | 37 | | cells[row, col] = 1; |
| | 38 | |
|
| 4 | 39 | | result--; |
| 4 | 40 | | } |
| | 41 | |
|
| 20 | 42 | | foreach (var wall in walls) |
| 7 | 43 | | { |
| 7 | 44 | | var row = wall[0]; |
| 7 | 45 | | var col = wall[1]; |
| | 46 | |
|
| 7 | 47 | | cells[row, col] = 2; |
| | 48 | |
|
| 7 | 49 | | result--; |
| 7 | 50 | | } |
| | 51 | |
|
| 18 | 52 | | for (var i = 0; i < m; i++) |
| 7 | 53 | | { |
| 80 | 54 | | for (var j = 0; j < n; j++) |
| 33 | 55 | | { |
| 33 | 56 | | if (cells[i, j] == 1) |
| 4 | 57 | | { |
| 4 | 58 | | result -= Mark(cells, m, n, i, j); |
| 4 | 59 | | } |
| 33 | 60 | | } |
| 7 | 61 | | } |
| | 62 | |
|
| 2 | 63 | | return result; |
| 2 | 64 | | } |
| | 65 | |
|
| | 66 | | private static int Mark(int[,] cells, int m, int n, int x, int y) |
| 4 | 67 | | { |
| 4 | 68 | | var result = 0; |
| | 69 | |
|
| 20 | 70 | | for (var i = x + 1; i < m; i++) |
| 7 | 71 | | { |
| 7 | 72 | | if (cells[i, y] == 2 || cells[i, y] == 1) |
| 1 | 73 | | { |
| 1 | 74 | | break; |
| | 75 | | } |
| | 76 | |
|
| 6 | 77 | | if (cells[i, y] != 0) |
| 0 | 78 | | { |
| 0 | 79 | | continue; |
| | 80 | | } |
| | 81 | |
|
| 6 | 82 | | cells[i, y] = -1; |
| | 83 | |
|
| 6 | 84 | | result++; |
| 6 | 85 | | } |
| | 86 | |
|
| 12 | 87 | | for (var i = x - 1; i >= 0; i--) |
| 4 | 88 | | { |
| 4 | 89 | | if (cells[i, y] == 2 || cells[i, y] == 1) |
| 2 | 90 | | { |
| 2 | 91 | | break; |
| | 92 | | } |
| | 93 | |
|
| 2 | 94 | | if (cells[i, y] != 0) |
| 1 | 95 | | { |
| 1 | 96 | | continue; |
| | 97 | | } |
| | 98 | |
|
| 1 | 99 | | cells[i, y] = -1; |
| | 100 | |
|
| 1 | 101 | | result++; |
| 1 | 102 | | } |
| | 103 | |
|
| 16 | 104 | | for (var j = y + 1; j < n; j++) |
| 7 | 105 | | { |
| 7 | 106 | | if (cells[x, j] == 2 || cells[x, j] == 1) |
| 3 | 107 | | { |
| 3 | 108 | | break; |
| | 109 | | } |
| | 110 | |
|
| 4 | 111 | | if (cells[x, j] != 0) |
| 0 | 112 | | { |
| 0 | 113 | | continue; |
| | 114 | | } |
| | 115 | |
|
| 4 | 116 | | cells[x, j] = -1; |
| | 117 | |
|
| 4 | 118 | | result++; |
| 4 | 119 | | } |
| | 120 | |
|
| 10 | 121 | | for (var j = y - 1; j >= 0; j--) |
| 3 | 122 | | { |
| 3 | 123 | | if (cells[x, j] == 2 || cells[x, j] == 1) |
| 2 | 124 | | { |
| 2 | 125 | | break; |
| | 126 | | } |
| | 127 | |
|
| 1 | 128 | | if (cells[x, j] != 0) |
| 1 | 129 | | { |
| 1 | 130 | | continue; |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | cells[x, j] = -1; |
| | 134 | |
|
| 0 | 135 | | result++; |
| 0 | 136 | | } |
| | 137 | |
|
| 4 | 138 | | return result; |
| 4 | 139 | | } |
| | 140 | | } |