Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added in C++

C++Server Side ProgrammingProgramming

<p>Given a matrix [ ][ ] having dimensions as row x col. The goal is to find the count of cells of matrix that meet the given condition:</p><p>Value of cell matrix [i][j] + no. of adjacent cells to it = a Fibonacci number</p><p>Numbers in Fibonacci series:- 0, 1, 1, 2, 3, 5, 8, 13, 21, 43 &hellip;..</p><h2>Let us understand with examples.</h2><p><strong>For Example</strong></p><p><strong>Input -</strong> matrix[row][col] = {{1, 4, 1}, {2, 0, 1}, {5, 1, 1}</p><p><strong>Output -&nbsp;</strong>Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added are: 4</p><p><strong>Explanation</strong></p><p>&nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp;1 &nbsp; 2 &nbsp;</p><p>0 &nbsp; &nbsp;1 &nbsp; &nbsp;4 &nbsp; &nbsp;1 &nbsp;&nbsp;</p><p>1 &nbsp; &nbsp;2 &nbsp; &nbsp;0 &nbsp; &nbsp;1 &nbsp;&nbsp;</p><p>2 &nbsp; &nbsp;5 &nbsp; &nbsp;1 &nbsp; &nbsp;1 &nbsp;&nbsp;</p><p>Cell(0,0) &rarr; 1+2=3 ( 2 adjacent cells (1,0) and (0,1) )</p><p>Cell(0,2) &rarr; 1+2=3</p><p>Cell(1,0) &rarr; 2+3=5</p><p>Cell(2,2) &rarr; 1+2=3</p><p><strong>Input -</strong> matrix[row][col] = &nbsp;{{0,0,0}, {0, 1, 0}, {0, 0, 0} }</p><p><strong>Output -</strong>Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added are: 9</p><p><strong>Explanation</strong></p><p>&nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp;1 &nbsp; 2 &nbsp;</p><p>0 &nbsp; &nbsp;0 &nbsp; &nbsp;0 &nbsp; 0&nbsp;</p><p>1 &nbsp; &nbsp;0 &nbsp; &nbsp;1 &nbsp; 0 &nbsp;&nbsp;</p><p>2 &nbsp; &nbsp;0 &nbsp; &nbsp;0 &nbsp; 0 &nbsp;&nbsp;</p><p>Cell(0,0) &rarr; 0+2=2 ( 2 adjacent cells (1,0) and (0,1) ) Similarly cells (0,2), (2,2) and (2,0)</p><p>Cell(0,1) &rarr; 0+3=3 ( 3 adjacent cells (0,1) and (0,2) and (1,1) ) Similarly cells (1,0), (1,2) and (2,1)</p><p>Cell (1,1) &rarr; 1+4=5&nbsp;</p><p>All 9 cells are counted.</p><h2>Approach used in the below program is as follows</h2><p>In the matrix of any kind there will be three types of cells only. The corner cells will have 2 adjacent cells, the cells with 3 adjacent cells and the cells with 4 adjacent cells only. Add 2, 3 or 4 to the value of these cells and check if the sum is a fibonacci number using the function check_fibonacci(int num).</p><ul class="list"><li>Take a matrix[][] and initialize it.&nbsp;</li><li>Function check_square(long double num) takes a number and returns true if it is a perfect square.</li><li>Function check_fibonacci(int num) returns true if num is a Fibonacci number.</li><li>If check_square(5 * num * num + 4) || check_square(5 * num * num - 4) returns true then num is a Fibonacci number.</li><li>Function Fibonacci_cells(int matrix[row][col]) returns the count of cells in a matrix which gives a Fibonacci number when the count of adjacent cells is added.</li><li>Take the initial count as 0.</li><li>Traverse using for loops from i=0 to i&lt;row and j=0 to j&lt;col. Take total = matrix[i][j].</li><li>Add 2 ,3 or 4 to total based on the cells adjacent to it.</li><li>If the new total is a fibonacci number then check_fibonacci(total) wil return true, so increment count.</li><li>At the end of all for loops return count as result.</li></ul><h2>Example</h2><p><a class="demo" href="http://tpcg.io/fW0u2qq9" rel="nofollow" target="_blank">Live Demo</a></p><pre class="prettyprint notranslate">#include &lt;bits/stdc++.h&gt; using namespace std; #define row 3 #define col 3 bool check_square(long double num) { &nbsp; &nbsp;long double val = sqrt(num); &nbsp; &nbsp;return ((val - floor(val)) == 0); } bool check_fibonacci(int num) { &nbsp; &nbsp;return check_square(5 * num * num + 4) || check_square(5 * num * num - 4); } int Fibonacci_cells(int matrix[row][col]) { &nbsp; &nbsp;int count = 0; &nbsp; &nbsp;for (int i = 0; i &lt; row; i++) { &nbsp; &nbsp; &nbsp; for (int j = 0; j &lt; col; j++) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int total = matrix[i][j]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ((i == 0 &amp;&amp; j == 0) || (i == row - 1 &amp;&amp; j == 0) || (i == 0 &amp;&amp; j == col - 1) || (i == row - 1 &amp;&amp; j == col - 1)) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total = total + 2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else if (i == 0 || j == 0 || i == row - 1 || j == col - 1) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total = total + 3; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total = total + 4; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (check_fibonacci(total)) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;return count; } int main() { &nbsp; &nbsp;int matrix[row][col] = {{1, 4,1},{2,0,1},{5,1,1}}; &nbsp; &nbsp;cout &lt;&lt; &quot;Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added are: &quot; &lt;&lt; Fibonacci_cells(matrix); &nbsp; &nbsp;return 0; }</pre><p>If we run the above code it will generate the following output &minus;</p><h2>Output</h2><pre class="result notranslate">Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added are:&nbsp;</pre>
raja
Updated on 29-Jan-2021 08:01:13

Advertisements