C++ program to find out the maximum number of cells that can be illuminated

C++Server Side ProgrammingProgramming

<p>Suppose, we are given a grid of dimensions h * w. The cells in the grid can contain either a bulb or obstacles. A light bulb cell illuminates the cells in its right, left, up, and down and the light can shine through the cells unless an obstacle cell blocks the light. An obstacle cell can not be illuminated and it blocks the light from a bulb cell from reaching the other cells. We are given the grid in an array of strings, where &#39;#&#39; represents an obstacle and &#39;.&#39; represents a vacant cell. We have only one bulb and we have to find out the maximum number of cells that we can illuminate placing the bulb optimally in the grid.</p><p>So, if the input is like h = 4, w = 4, grid = {&quot;#...&quot;, &quot;....&quot;, &quot;...#&quot;, &quot;....&quot;}, then the output will be 7.</p><p><img src="https://www.tutorialspoint.com/assets/questions/media/62640/dimensions.jpg" class="fr-fic fr-dib" width="219" height="198"></p><p>From the image, we can see the illuminated cells in the grid.</p><h2>Steps</h2><p>To solve this, we will follow these steps &minus;</p><pre class="prettyprint notranslate">Define one 2D array first for initialize i := 0, when i &lt; h, update (increase i by 1), do: &nbsp; &nbsp;count := 0 &nbsp; &nbsp;for initialize j := 0, when j &lt; w, update (increase j by 1), do: &nbsp; &nbsp; &nbsp; if grid[i, j] is same as &#39;#&#39;, then: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count := 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ignore following part, skip to the next iteration &nbsp; &nbsp; &nbsp; else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;first[i, j] := count &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(increase count by 1) &nbsp; &nbsp;k := 0 &nbsp; &nbsp;for initialize j := w - 1, when j &gt;= 0, update (decrease j by 1), do: &nbsp; &nbsp; &nbsp; if grid[i, j] is same as &#39;#&#39;, then: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k := 0 &nbsp; &nbsp; &nbsp; &nbsp; Ignore following part, skip to the next iteration &nbsp; &nbsp; &nbsp;else: &nbsp; &nbsp; &nbsp; &nbsp; k := maximum of k and first[i, j] &nbsp; &nbsp; &nbsp; &nbsp; first[i, j] := k Define one 2D array second for initialize j := 0, when j &lt; w, update (increase j by 1), do: &nbsp; &nbsp;count := 0 &nbsp; &nbsp;for initialize i := 0, when i &lt; h, update (increase i by 1), do: &nbsp; &nbsp; &nbsp; if grid[i, j] is same as &#39;#&#39;, then: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count := 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ignore following part, skip to the next iteration &nbsp; &nbsp; &nbsp; else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;second[i, j] := count &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(increase count by 1) k := 0 for initialize i := h - 1, when i &gt;= 0, update (decrease i by 1), do: &nbsp; &nbsp;if grid[i, j] is same as &#39;#&#39;, then: &nbsp; &nbsp; &nbsp; k := 0 &nbsp; &nbsp; &nbsp; Ignore following part, skip to the next iteration &nbsp; &nbsp;else: &nbsp; &nbsp; &nbsp; k := maximum of k and second[i, j] &nbsp; &nbsp; &nbsp; second[i, j] := k result := 0 for initialize i := 0, when i &lt; h, update (increase i by 1), do: &nbsp; &nbsp;for initialize j := 0, when j &lt; w, update (increase j by 1), do: &nbsp; &nbsp; &nbsp; result := maximum of result and first[i, j] + second[i, j] return result + 1</pre><h2>Example</h2><p>Let us see the following implementation to get better understanding &minus;</p><pre class="demo-code notranslate language-cpp" data-lang="cpp">#include &lt;bits/stdc++.h&gt; using namespace std; int solve(int h, int w, vector&lt;string&gt; grid){ &nbsp; &nbsp;vector&lt;vector&lt;int&gt;&gt; first(h, vector&lt;int&gt; (w)); &nbsp; &nbsp;for(int i = 0; i &lt; h; i++) { &nbsp; &nbsp; &nbsp; int count = 0; &nbsp; &nbsp; &nbsp; for(int j = 0; j &lt; w; j++) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(grid[i][j] == &#39;#&#39;) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first[i][j] = count; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; int k = 0; &nbsp; &nbsp; &nbsp; for(int j = w-1; j &gt;= 0; j--) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(grid[i][j] == &#39;#&#39;) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k = max(k, first[i][j]); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first[i][j] = k; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;vector&lt;vector&lt;int&gt;&gt; second(h, vector&lt;int&gt; (w)); &nbsp; &nbsp;for(int j = 0; j &lt; w; j++) { &nbsp; &nbsp; &nbsp; int count = 0; &nbsp; &nbsp; &nbsp; for(int i = 0; i &lt; h; i++) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(grid[i][j] == &#39;#&#39;) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; second[i][j] = count; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; int k = 0; &nbsp; &nbsp; &nbsp; for(int i = h-1; i &gt;= 0; i--) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(grid[i][j] == &#39;#&#39;) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k = max(k, second[i][j]); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; second[i][j] = k; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; } &nbsp; &nbsp; int result = 0; &nbsp; &nbsp; for(int i = 0; i &lt; h; i++) { &nbsp; &nbsp; &nbsp; &nbsp;for(int j = 0; j &lt; w; j++) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = max(result, first[i][j] + second[i][j]); &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; } &nbsp; &nbsp; return result + 1; } int main() { int h = 4, w = 4; vector&lt;string&gt; grid = {&quot;#...&quot;, &quot;....&quot;, &quot;...#&quot;, &quot;....&quot;}; cout&lt;&lt; solve(h, w, grid); return 0; }</pre><h2>Input</h2><pre class="result notranslate">4, 4, {&quot;#...&quot;, &quot;....&quot;, &quot;...#&quot;, &quot;....&quot;}</pre><h2>Output</h2><pre class="result notranslate">7</pre>
raja
Updated on 02-Mar-2022 09:38:22

Advertisements